
; Writeint (from Irvine library) ---------------------
; Caution: source code given in Irvine 9.6.1 is faulty
;
; Writes a 16-bit unsigned binary integer to standard 
; output. Input parameters: AX = value, BX = radix.
;
;-----------------------------------------------------
.data
xtable    db "0123456789ABCDEF"
buffer    db  16 dup(' ')      ; buffer to hold chars
bufferEnd label byte

.code
Writeint proc
     pusha
     mov   cx,0         ; digit counter
     mov   di,offset bufferEnd
     cmp   bx,2         ; BX must be between
     jb    L5           ; 2 and 16.
     cmp   bx,16
     ja    L5

L3:  mov   dx,0         ; clear dividend to zero
     div   bx           ; divide AX by the radix

     xchg  ax,dx       ; exchange quotient, remainder
     push  bx              
     mov   bx,offset xtable; translate table
     xlat                  ; look up ASCII digit
     pop   bx              
     dec   di              ; back up in buffer
     mov   [di],al         ; move digit into buffer
     xchg  ax,dx           ; swap quotient into AX

     inc   cx              ; increment digit count
     or    ax,ax           ; quotient = 0?
     jnz   L3              ; no: divide again

     ; Display the buffer using CX as a counter.

L4:  mov   ah,2              ; function: display character
     mov   dl,[di]           ; character to be displayed 
     int   21h               ; call DOS
     inc   di                ; point to next character
     loop  L4

L5:
     popa
     ret
Writeint endp


