Hi I'm having trouble with this code. It will print 0-9 fine but AF or af it just gives me a smiley face which isn't even an ASCII symbol atleast according to the list of symbols my professor has provided us. Here is a my code. Please help. I'm using NASM and DosBox.
org 100h
; hexadecimal to base 10 conversion program
section .data
MSG1 dw " Enter a hexadecimal digit ", '$'
MSG2 dw " In decimal it is: ", '$'
MSG3 dw " Do you want to do it again? ", '$'
EMSG dw " Illegal character, enter 0..9 or A..F: ", '$'
section .text
main:
; print user prompt
mov dx, MSG1 ; get message
mov ah, 09h ; display string function
int 21h ; display it
jmp userin ; jumps to user input so if illegal character is entered message won't display again
userin:
; input a hexidecimal number then compare to ASCII codes
mov ah, 01h ; keyboard input function
int 21h ; character input, copies character into al
mov bh, al ; moves al into bh to avoid al being messed with
cmp bh, 30h ; compares input to ASCII code for 0
jl error ; if input is less than 0 jump to main
cmp bh, 39h ; compares input to ASCII code for 9
jle print1 ; if input is less than or equal to 9 jump to print
cmp bh, 41h ; compares input to ASCII code for 'A'
jl error ; if input is less than 'A' jump to main
cmp bh, 46h ; compares input to ASCII code for 'F'
jle convert1 ; if input is less than or equal to 'F' jump to convert1
cmp bh, 61h ; compares input to ASCII code for 'a'
jl error ; if input is less than 'a' jump to main
cmp bh, 66h ; compares input to ASCII code for 'f'
jle convert2 ; if input is less than of equal to 'f' jump to convert2
jg error ; if input is greater than 'z' jump to main
error:
; displays error message then jumps back
mov dx, EMSG ; moves error message into display string register
mov ah, 09h ; display string function
int 21h ; displays it
jmp userin ; jumps back to user input
convert1:
; converts input from hexadecimal A-F to decimal
sub bh, 16 ; subtracts 0Fh from input to get decimal
jmp print2 ; jumps to print
convert2:
; converts input from hexidecimal a-f to decimal
sub bh, 48 ; subtracts 30h from the input to get
jmp print2 ; jumps to print
print1:
; prints the input in decimal form
mov dx, MSG2 ; moves MSG2 into display string register
mov ah, 09h ; display string function
int 21h ; display MSG2 "In decimal it is:"
mov dl, bh ; moves input into display character register
mov ah, 02h ; display character function
int 21h ; display input in decimal form
jmp again ; jumps to again
print2:
;prints the input in decimal form
mov dx, MSG2 ; moves MSG2 into display string register
mov ah, 09h ; display string function
int 21h ; display MSG2 "In decimal it is:"
mov dl, 1d ; will add 10 to the converted ASCII code
mov ah, 02h ; display character function
int 21h ; display 1 in front of input in decimal form
mov dl, bh ; moves converted input to display character register
mov ah, 02h ; display character function
int 21h ; displays converted input
jmp again ; jumps to again
again:
; asks if user wants to do it again
mov dx, MSG3 ; moves MSG2 into display string register
mov ah, 09h ; display string function
int 21h ; display MSG3 "Do you want to do it again?"
mov ah, 01h ; keyboard input function
int 21h ; character input, copies into al
cmp al, 59h ; compares input to ASCII code for 'Y'
je main ; if input is equal to 'Y' jump to main
jl exit ; if input is less than 'Y' jump to exit
cmp al, 79h ; compares input to ASCII code for 'y'
je main ; if input is equal to 'y' jump to main
jne exit ; if input is not equal to 'y' jump to exit
exit:
;exit to DOS
mov ah, 04Ch ; DOS function: Exit program
mov al, 0 ; Return exit code value
int 21h ; Call DOS. Terminate program
The calculations are correct, but the code which should print leading '1' is wrong. You need to put character code in DL, not the number. So instead of
mov dl, 1d ; will add 10 to the converted ASCII code
mov ah, 02h ; display character function
int 21h ; display 1 in front of input in decimal form
it should be
mov dl, 49 ; will add 10 to the converted ASCII code
mov ah, 02h ; display character function
int 21h ; display 1 in front of input in decimal form
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.