Coder's Guild Mailing List

Printig Long Ints in Assembly

Posted by Ali Bhai on 1999-05-07

Hi senior assembly coders,

I am sure that you people must have faced the same problem ie printing
double words in assembly. The limit of div instruction doesn't allow us to
do so. I don't want to use math coprocessor. I want to write pure 8086 code
that prints long ints. The algorithm I have implemented repeatedly divides a
number by 10 and stores the remainders. When the quotient becomes 0, I
reprint the values after popping them off the stack. This works for short
ints (word in pascal and assembly 0 - 65535).

However, the same algo doesn't work for double word greater than 0009FFFFh
ie dx:ax = 0009:FFFFh. Division by zero (ie overflow occurs when division of
this number by 10 takes place). Can you help me out?

TITLE Double_Word
.MODEL SMALL
.STACK 100h
.DATA
   num1         dd      0009FFFEh
   num2         dd      01h

.CODE
main proc
   mov ax, @data
   mov ds, ax

   mov ax, word ptr num1
   mov dx, word ptr num1+2
   call show_decimal
   mov ah, 2
   mov dl, '+'
   int 21h
   mov ax, word ptr num2
   mov dx, word ptr num2+2
   call show_decimal
   mov ah, 2
   mov dl, '='
   int 21h

   mov ax, word ptr num1
   mov bx, word ptr num2
   add ax, bx
   push ax
   mov ax, word ptr num1+2
   mov bx, word ptr num2+2
   adc ax, bx
   mov dx, ax
   pop ax
   call show_decimal

   mov ah, 4Ch
   int 21h
main endp


;-----------------------------------------------------------------------
;                               Show Decimal
;
; INPUT: DX:AX - Value to print
; OUTPUT: None
; PROCESS: Display the value stored in AX
;-----------------------------------------------------------------------
show_decimal proc
   push ax              ; store the register values on stack
   push bx
   push cx
   push dx

   mov bx, 10           ; divisor
   xor cx, cx           ; cx = 0 will contain the number of divisions

@repeat:
   div bx               ; dx:ax / 10
   inc cx               ; one more division took place
   push dx              ; the proc will reverse the remainders when printing
   xor dx, dx
   cmp ax, 0            ; is ax = 0?
   jne @repeat          ; if no then do another division

   mov ah, 2
@Show:
   pop dx               ; start reverse displaying the remainders
   add dl, '0'
   int 21h
   loop @Show


   pop dx               ; restore the register values from stack
   pop cx
   pop bx
   pop ax
@end:
   ret
show_decimal endp
;-----------------------------------------------------------------------
end main


Ali Bhai
BCS III - FAST Institute of Computer Science, Karachi
email: mashah@xxxxx.xxx.xx
web page: www.pak.org/mufta
<----------------------------------------->
[To err is human but to really mess up the things you need a computer.]