Simple way to print value of a register in x86 assembly
I need to write a program in 8086 Assembly that receives data from the user, does some mathematical calculations and prints the answer on the screen, I have written all parts of the program and all work fine but I don't know how to print the number to the screen.
At the end of all my calculation the answer is AX and it is treated as an unsigned 16 b开发者_运维百科it integer. How do I print the decimal (unsigned) value of the AX register?
you could use the C-library function itoa, implementing it isn't that hard, basicaly, you do:
while (x){
buff[n]==x % 10;
x/=10;
n++;
}
and then invert the buffer (or print character-wise backwards)
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
精彩评论