;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Print the value in DX. ;
;Print the value of DX to the current cursor possition.;
; ;
;Input: ;
;DX = Any value ;
; ;
;Note, The following subroutines are used: ;
;'HEXtoBCD' ;
;'BCDtoASCII' ;
;Int 10h ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
PrintDX:
push ax
push bx
push dx
push cx
call PrnDX
pop cx
pop dx
pop bx
pop ax
ret
PrnDX: mov ax,dx ;Save AX
xor dx,dx ;Clear BX
push dx ;Save DX
call HEXtoBCD ;convert to BCD
pop dx ;Restore DX
push ax ;save AX
mov ax,bx ;Set up for routine
call PrnDX0 ;Print 4 upper diggits
pop ax ;Restore AX
PrnDX0: push ax ;Save 4 lowest digits
mov al,ah ;Print high digit first
call PrnDX1 ;print
pop ax ;get low digit
PrnDX1: call BCDtoASCII ;to ASCII
push ax ;save digits
mov al,ah ;print high digit first
call PrnDX2 ;Print
pop ax ;get back digits
PrnDX2: mov ah,0Eh ;then print low digit
int 10h ;call video IO
ret ;Return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;HEX to BCD. ;
;converts a HEX number in DX and BX to ;
;a BCD number in AX and BX. ;
; ;
;input: ;
;AX = Low HEX input ;
;DX = High HEX input ;
; ;
;output: ;
;AX = Low BCD output (4 digits, one per nibble) ;
;BX = High BCD output (4 digits, one per nibble);
;OF set if output greater than 99999999 ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
HEXtoBCD:
push cx
push dx
mov cx,ax ;Get low input
push dx ;Save high input
sub ax,ax ;prepare ax
sub bx,bx ;and bx
jcxz HEXBCD2 ;Skip if zero (output is already zeroed)
HEXBCD0: ;Add first the
inc ax ;increase low BCD output.
; Digit 1, ones, low nibble of AL
mov dx,ax ;make a working copy
and dx,0Fh ;mask off nibble
cmp dx,0Ah ;digit=10d or above?
jb HEXBCD1 ;no, try next number
add ax,06h ;convert to BCD
; Digit 2, tens, high nibble of AL
mov dx,ax
and dx,0F0h
cmp dx,0A0h
jb HEXBCD1
add ax,060h
; Digit 3, hundreds, low nibble of AH
mov dx,ax
and dx,0F00h
cmp dx,0A00h
jb HEXBCD1
add ax,0600h
; Digit 4, thousands, high nibble of AH
mov dx,ax
and dx,0F000h
cmp dx,0A000h
jb HEXBCD1
add ax,06000h
inc bx ;Number > than 9999; Increase high BCD output then.
; Digit 5, tens of thousands, high nibble of DH
mov dx,bx
and dx,0Fh
cmp dx,0Ah
jb HEXBCD1
add bx,06h
; Digit 6, hundred of thousands, high nibble of DL
mov dx,bx
and dx,0F0h
cmp dx,0A0h
jb HEXBCD1
add bx,060h
; Digit 7, millions, low nibble of DH
mov dx,bx
and dx,0F00h
cmp dx,0A00h
jb HEXBCD1
add bx,0600h
; Digit 8, tens of millions, high nibble of DH
mov dx,bx
and dx,0F000h
cmp dx,0A000h
jb HEXBCD1
add bx,06000h
HEXBCD1:
loop HEXBCD0 ;Add one more
HEXBCD2:
pop cx ;Get high input
jcxz HEXBCD3 ;Skip if number < 64K
jmp HEXBCD4 ;Else, add more
HEXBCD3:
jmp HEXBCD7 ;Return
HEXBCD4:
push cx ;Save high input loop counter
sub cx,cx ;Add 64K for each number in high input
HEXBCD5:
inc ax
; Digit 1, ones, low nibble of AL
mov dx,ax
and dx,0Fh
cmp dx,0Ah
jb HEXBCD6
add ax,06h
; Digit 2, tens, high nibble of AL
mov dx,ax
and dx,0F0h
cmp dx,0A0h
jb HEXBCD6
add ax,060h
; Digit 3, hundreds, low nibble of AH
mov dx,ax
and dx,0F00h
cmp dx,0A00h
jb HEXBCD6
add ax,0600h
; Digit 4, thousands, high nibble of AH
mov dx,ax
and dx,0F000h
cmp dx,0A000h
jb HEXBCD6
add ax,06000h
inc bx
; Digit 5, tens of thousands, high nibble of DH
mov dx,bx
and dx,0Fh
cmp dx,0Ah
jb HEXBCD6
add bx,06h
; Digit 6, hundred of thousands, high nibble of DL
mov dx,bx
and dx,0F0h
cmp dx,0A0h
jb HEXBCD6
add bx,060h
; Digit 7, millions, low nibble of DH
mov dx,bx
and dx,0F00h
cmp dx,0A00h
jb HEXBCD6
add bx,0600h
; Digit 8, tens of millions, high nibble of DH
mov dx,bx
and dx,0F000h
cmp dx,0A000h
jb HEXBCD6
add bx,06000h
HEXBCD6:
loop HEXBCD5 ;Try next number
pop cx ;Get high input loop counter
loop HEXBCD4 ;Loop if more to add
HEXBCD7:
pop dx
pop cx
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;BCD to ASCII. ;
;converts a BCD number in AL to;
;two ASCII numbers in AX. ;
; ;
;input: ;
;AL = BCD input ;
; ;
;output: ;
;AX = ASCII output ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
BCDtoASCII: push cx
mov cl,04h
sub ah,ah ;clear ah
shl ax,cl ;move upper digit into ah
shr al,cl ;and return lower digit to al
add ax,3030h ;ajust to ASCII
pop cx
ret