【发布时间】:2014-03-19 04:56:28
【问题描述】:
我发现了很多关于这个问题的问题,但是,我无法让我的代码运行。我的程序应该取一个十六进制值,检查它是否是一个有效的十六进制字符,然后将十六进制值显示为十进制值。如果是小写十六进制字符,则需要将其转换为大写。所有这些都需要循环。
除了将十六进制转换为十进制之外,我已经完成了所有这些工作。我在程序中有我认为应该转换它的代码,但它不会编译。我将在代码下方列出编译器错误。如果标签 convert: 中的代码被注释掉(除了最后一行,“jmp display”),那么程序将正常运行,但显然不会将值显示为小数。
使用 nasme 编译:“nasm -fbin getChar.asm -o getChar.com -l getChar.lst”
我在 dosbox 中运行程序。
; This program gets a char from user and prints it out as a decimal
org 100h ; program start point
section .data
msgIn: DB 13, 10, "Enter a Hex Digit: $"
msgOut: DB 13, 10, "Decimal Value: $"
msgOpt: DB 13, 10, "Enter another?(y or n): $"
errMsg: DB 13, 10, "Illegal character, Enter 0..9 or A..F: $"
HNUM: DB 19H
NUM: DB 0
D: DB 10h
H: DB 16
CNT: DB 0
section .text
continue: ; start of loop
mov dx, msgIn ; offset address of message to display
mov ah, 9 ; print string function
int 21h
mov ah, 1 ; keyboard input sub-program
int 21h ; read character into al
mov cl, al
legal: ; compare input to see if valid
cmp cl, 48 ; cl < 0
jl end_if ; yes, error msg
cmp cl, 70 ; cl > F
jg check_case ; yes, error msg
jmp prntMsg2 ; print value of input
check_case: ; check case of input
cmp cl, 97 ; cl < a
jl end_if ; yes, error msg
cmp cl, 102 ; cl > f
jg end_if ; yes, error msg
jmp to_upper ; need to send to function to convert to upper case
; then pass to prntMsg2
to_upper:
and al, 223 ; convert to upper case(0DFh)
jmp prntMsg2
end_if: ; error message if invalid input
mov ah, 9
mov dx, errMsg ; print error message
int 21h
jmp continue ; get a new value
prntMsg2: ; print second message*****
mov dx, msgOut ; offset of second message
mov ah, 9 ; print string function
int 21h ; display message
convert:
mov cx, 00
mov dx, 00
L6: mov ax, 00
mov al, [HNUM]
div word [D]
mov [HNUM], al
mov bx, ax
mov cl, 0
mov ax, 1
L5:
cmp cl, 00
je L7
mul word [H]
sub cl, 1
jmp L5
L7:
mul bh
add dx, ax
add word [CNT], 1
cmp word [HNUM], 0
jg L6
mov [NUM], dl
jmp display
display: ; display character
mov dl, al
mov ah, 2 ; print char function
int 21h
mov ah, 9 ; see if user wants to do it again
mov dx, msgOpt
int 21h
mov ah, 1
int 21h
mov bl, al
cmp bl, 'y' ; bl = y
jne exitPrg ; no, end
jmp continue ; get a new value
exitPrg: ; exit program
mov ah, 4ch ; exit to DOS function
int 21h ; see you later!
上面的代码已经过编辑,现在可以编译和运行了。但是,它仍然没有正确地进行从十六进制到十进制的转换。它根本不显示值,只是空白。如果输入字母,即使是字母 a-f,它也会挂断。一个数字不挂,但还是不显示值。
现在我已经运行它,至少我可以修复它,但是,任何指导都值得赞赏。感谢 Gene 让我开始工作。
【问题讨论】:
标签: assembly hex decimal x86-16