I think there's a bit of a misunderstanding here that hasn't been addressed. A number as stored in computer memory can't be printed as-is. In assembly, the number must be converted to a string, and you need to print that string. Thankfully, this is fairly simple, as ASCII was designed so that the string representation of any single-digit base 10 number 0 through 9 is its value plus 0x30
. That is to say, '0' = 30h
, '1' = 31h
, etc. The only problem is that by default a number is stored as binary, which means that it's far easier to print a number as hexadecimal than as decimal. The conversion factor for a single hex digit of the form 0x0n
to its ASCII value is quite easy:
; AL = the digit you wish to print, ranges from 0x00 to 0x0F
cmp al,0Ah
jb noCorrectHex
add al,7 ; converts 0x0A to 0x41 = 'A' , 0x0B to 0x42 = 'B', etc.
noCorrectHex:
add al,'0' ; evaluates to "add al,30h"
If you start with the leftmost digit of your word (after loading it from memory into a register) and proceed from left to right you can print the digits in order using this method. I use the DOS interrupt for printing a single character, not a string.
PrintHex:
; AL = the byte you wish to print.
; this prints the hexadecimal representation of a byte to the screen.
push ax
shr al,1
shr al,1
shr al,1
shr al,1
call PrintHexChar
pop ax
and al,0Fh
;fallthrough is intentional
PrintHexChar:
cmp al,0Ah
jb noCorrectHex
add al,7 ; converts 0x0A to 0x41 = 'A' , 0x0B to 0x42 = 'B', etc.
noCorrectHex:
add al,'0' ; evaluates to "add al,30h"
mov ah,02h
mov DL,AL
int 21h
ret
And here's another way to perform the math to convert a single hex digit to ASCII, it's a bit hard to follow what's going on but it doesn't require any branching (I'm not sure if this is better or worse than just doing the more readable version, but you get the same result)
PrintHexChar:
and al,0Fh ;clear C and A flags. Required even if top 4 bits are already 0.
daa
add al,0F0h
adc al,40h
mov ah,02h
mov DL,AL
int 21h
ret