Easy way to print the register value in x86 assembly

I need to write a program in assembly 8086, which receives data from the user, performs some mathematical calculations and prints the answer on the screen, I wrote all the parts of the program and everything works fine, but I don’t know how to print the number on the screen.

At the end of all my calculations, the answer is AX, and it is treated as an unsigned integer of 16 bits. How to print decimal (unsigned) value of register AX?

+3
source share
1 answer

you can use the itoa C library function, it's not that complicated, basically you:

while (x){
    buff[n]==x % 10;
    x/=10;
    n++;
}

and then invert the buffer (or print the character back)

void print_number (int x);

print_number:
  buff db 15 dup(0)
  mov ax,[esp+4]
  mov bx,0
itoa_w1:

  mov cx, ax
  mod cx,10
  add cx,30h;'0'
  div ax,10
  mov buff[bx],cl
  cmp ax,0
  jnz itoa_w1

itoa_w2:
  push buff[bx]
  call putchar
  pop  ax
  cmp  bx,0
  jnz itoa_w2

ret
+1

Source: https://habr.com/ru/post/1742684/


All Articles