Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Probably a really dumb question displaying hex number

Status
Not open for further replies.

skullblock

Programmer
Mar 4, 2003
8
US
I am sorry to bother everyone but I am struggling with this how do I get a hex value from the user input and display the hex value. This is my display code for the hex value.

mov bl, al

shr al, 4
cmp al, 10
sbb al, 105
das
_PutCh al

mov al, bl

and al, 00001111b
cmp al, 10
sbb al, 105
das
_PutCh al

This works for entering a decimal number and displaying the hex value. But if I enter the hex value it returns a decimal value. I guess I am asking how do you have a user enter 4 byte hex value and get that value and retrun the same value as hex to the screen? Thanks :)
 
you have to do cmp sbb and das for each bit I think.

How about this:

mov bl, al

shr al, 4 ; get high order hex digit
xor ah, ah ; clear ah
add al, 48 ; add ascii code for '0'
cmp al, 58 ; check if ascii code is beyond '9'
cmc ; if it is beyond '9', carry will be clear, so flip it.
rcl ah, 4 ; if ascii code is beyond, the carry bit is
; inserted at the 4th bit giving a value of 8.
; zero otherwise.
sub ah, 1 ; we need to add 7 to get 'A', not 8, so sub one
adc al, ah ; add ah with value 7 or -1 (-1 + carry bit = 0)
_PutCh al

mov al, bl
and al, 15
xor ah, ah
add al, 48
cmp al, 58
cmc
rcl ah, 4
sub ah, 1
adc al, ah
_PutCh al

Above should work,
example: al = 5C hex

mov bl, al ; bl = 5C hex

shr al, 4 ; al = 5
xor ah, ah ; ah = 0
add al, 48 ; al = 53 dec (ASCII '5')
cmp al, 58 ; carry = 1
cmc ; carry = 0
rcl ah, 4 ; ah = 0
sub ah, 1 ; ah = -1, carry = 1
adc al, ah ; al = 53(al) + -1(ah) + 1(carry) = 53 dec (ASCII '5')
_PutCh al

mov al, bl ; al = 5C hex
and al, 15 ; al = 0C hex or 12 dec
xor ah, ah ; ah = 0
add al, 48 ; al = 60 dec
cmp al, 58 ; carry = 0
cmc ; carry = 1
rcl ah, 4 ; ah = 8
sub ah, 1 ; ah = 7, carry = 0
adc al, ah ; al = 60(al) + 7(ah) + 0(carry) = 67 dec (ASCII 'C')
_PutCh al


Lemme know if it works or not. I just made this up off the top of my head just now.

And make sure you use "sub ah, 1" and not "dec ah" cuz as we all know dec doesn't update the carry flag.

A programmer is a device for converting Coke into software.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top