You can convert a decimal to hexadecimal using shift left (note this operation takes place in binary).
Just like in a base 10 number system if you take 1 and add a zero to the left - 10 - it becomes 10 add another becomes 100 etc. Since 16 is 2^4 this is easy to do in binary just shift over 4 places and 15 becomes 240
An instruction like shl ax, 4 would move the value in the ax register to the left and add 0000,
making F = 15 = 00001111 into 1111
0000 = 240 = F
0.
CODE
ReadLoop:
_GetCh
cmp al, '0' ; test if number is a digit
jb Done
cmp al, '9'
jbe Digi
cmp al, 'A' ; test if number is a valid hex digit
jb Done
cmp al, 'F'
jbe UHex
cmp al, 'a' ; test if number is a valid hex digit
jb Done
cmp al, 'f'
jbe LHex
cmp al, 13 ; test if user hit enter key
je Done
jmp Done ; if invalid character stop processing
Digi:
mov ah, 0
sub al, 30h ; 30h = 48 decimal
jmp Conv
LHex:
and al, 11011111B ; Converts al to upper case
UHex:
mov ah, 0
sub al, 37h ; 37h = 87 decimal = [A-F] + 10
Conv:
xchg ax, bx
shl ax, 4
add bx, ax; Add number to total
jmp ReadLoop
Done:
mov ax, bx
This post has been edited by curvekiller: 14 May, 2008 - 08:56 PM