0

Let's say we got the number "98" stored in %r11, using movzbq i store the first digit "9" in %r12 as ascii. I subtract $48 from ascii to get the integer "9" and store it in %r12. I add %r12 to %r13 to get the value "9" or "0x9" in %r13 Then I multiply %r13 by 10 to get the value "90" stored as "0x5a" Now that %r13 contains "0x5a". I cannot do...

movq %r13, %rdi
call putchar.  

as this would print the character "Z" (value 90 in ascii table). How do I go by making it print the number "90" instead?

subq $48, %r12      # Subtract $48 to convert ascii to integer.
add %r12, %r13      # add 9 to %r13
imulq $10, %r13     # multiply by 10
add %r12, %r13      # add to return value
movq %r13, %rdi 
call putchar
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Vini
  • 25
  • 7
  • You get the 9 digit with the subtract so print/putchar it, then print a second 0 without doing any math. Just place ascii "0" in the register after the first print and call again. Remember that putchar will only output a single character as a time. – Michael Dorgan Oct 26 '20 at 16:38
  • It's unusual to have multiple ASCII digits in a register at once. Like you would have had to do a word load from a string / buffer, so you could have just `movzbl` loaded 1 byte from memory in the first place. If you do, you just need to `movzbl` (movzx) and right-shift by 8 unpack the bytes separately. – Peter Cordes Oct 26 '20 at 16:51
  • If you want to print them both, it would be more efficient to store them to memory and pass a pointer to `fwrite(buf, 1, 2, stdout)`. stdio locking overhead is higher than the store/reload of storing the data and passing a pointer to it. – Peter Cordes Oct 26 '20 at 16:52
  • Re: ASCII to/from integer: [NASM Assembly convert input to integer?](https://stackoverflow.com/a/49548057) / [Printing an integer as a string with AT&T syntax, with Linux system calls instead of printf](https://stackoverflow.com/a/45851398) – Peter Cordes Oct 26 '20 at 16:53
  • I'm supposed to store the value "90" in a registery and then print it out. However "90" is stored in hex "0x5a", I want it to print out "0x5a" which should actually print out "90" and not the character "Z" (ascii code for 90). – Vini Oct 26 '20 at 20:47

0 Answers0