views:

211

answers:

1

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?

A: 

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
itoa isn't standard, it's a microsoft extension.
Tom Dignan
i know, but searching the web will turn up some open source implementations.
flownt
if I could use c code I wouldn't have a problem. But I have to write everything in 8086 Assembly code.
Bob
i hope this is better, i might have made some syntactic mistakes though, it has been some time since i last did assembly...
flownt