3

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 bit integer. How do I print the decimal (unsigned) value of the AX register?

sepp2k
  • 363,768
  • 54
  • 674
  • 675
Bob
  • 2,586
  • 7
  • 20
  • 33
  • 1
    possible duplicate of http://stackoverflow.com/questions/2504974/outputting-variable-values-in-x86-asm – danben Apr 25 '10 at 17:04

1 Answers1

1

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
flownt
  • 760
  • 5
  • 11
  • itoa isn't standard, it's a microsoft extension. –  Apr 25 '10 at 17:54
  • i know, but searching the web will turn up some open source implementations. – flownt Apr 25 '10 at 18:00
  • 1
    if I could use c code I wouldn't have a problem. But I have to write everything in 8086 Assembly code. – Bob Apr 25 '10 at 18:07
  • i hope this is better, i might have made some syntactic mistakes though, it has been some time since i last did assembly... – flownt Apr 25 '10 at 18:26