0

I have this question. When studying assembler in depth, I encountered such a problem! We all know the standard conversion from a number to a string in order to output a number.

mov bx,input+5 mov cx,0 mov x,si nextd: mov dx,0 mov di,10 div di add dl,48 dec bx mov [bx],dl inc cx cmp ax,0 jne nextd

Why do we write +5 in the first line? Why don't we put the length of the string in that case, or the length of the register itself? I don't understand why +5

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 3
    Because the digits are produced backwards (least significant digit first) but humans write them forwards (most significant digit first). A 16 bit number in decimal is at most 5 digits hence the `+5`. Notice the `dec bx`. – Jester May 03 '20 at 16:34
  • [How do I print an integer in Assembly Level Programming without printf from the c library?](https://stackoverflow.com/a/46301894) shows a C version of this loop with explanation of decrementing from the end of a buffer. – Peter Cordes May 03 '20 at 17:08
  • 1
    Is `mov x,si` supposed to be `mov ax, si`? Otherwise this makes no sense. (Or storing SI to memory is just something unrelated, and AX already holds the number.) Also, use triple backticks for a multi-line code *block* – Peter Cordes May 03 '20 at 17:10

0 Answers0