3

Iam learning assembly and I found out how to get user input with

mov al, 3    ; system call number (sys_read)
xor bl, bl   ; file descriptor 0 (stdin)
mov rcx, buf ; buffer to store input
mov dl, 4    ; Lenght of buffer
int 0x80     ; interrupt

but that actually gets a string right? my question is how do i get a integer value... so if i type 100 how do i get the value 64h so i can add, subtract etc instead of a string with each byte being the ascii representation of the number and then how do i output a value like 64h to the screen so that it shows 100? i dont need code just some guidance

Thanks!

3
  • It looks like x86. Is this so? Commented Jul 31, 2011 at 22:43
  • 1
    Lets hope the upper parts of rax, rbx and rdx are 0... Commented Jul 31, 2011 at 23:02
  • The other way around: integer to string: stackoverflow.com/questions/4117422/… Commented May 25, 2015 at 6:48

2 Answers 2

8

Once you have the ASCII representation, you can just build up the result digit by digit, using the fact that the numerals are encoded in order. In pseudo-code, reading from left to right (i.e. starting with the most significant digit):

  • initialize result to 0
  • for each digit c, result *= 10; result += (c - '0');
  • result holds the numeric value of the string
Sign up to request clarification or add additional context in comments.

1 Comment

+1 The same method applies for other bases (though the c - '0' part gets more complicated for bases > 10)
1

Look at binary coded decimals BCD. It can do this a little more efficiently

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.