2

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!

Renato
  • 193
  • 1
  • 4
  • 16

2 Answers2

7

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
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 1
    +1 The same method applies for other bases (though the `c - '0'` part gets more complicated for bases > 10) – user786653 Jul 31 '11 at 23:04
1

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

Brett Walker
  • 3,566
  • 1
  • 18
  • 36