汇编:将十六进制转换为十进制

时间:2015-10-14 16:13:37

标签: assembly hex decimal nasm dosbox

我正在尝试获取十六进制数字(0-9或AF)的用户输入并将结果输出为十进制,并且如果用户放入十六进制范围之外的某些内容时显示错误消息,并重复整个根据用户的选择进行处理。

当尝试通过NASM编译时,程序会警告无法打开该文件。我的代码是否有问题导致此错误?

最终我试图在DOS BOX中运行并在NASM中成功编译

*编辑:代码已经更新,但是如果输入非十六进制值并且继续,检查退出和退出功能无法正常工作/不向用户显示,则会导致infinte循环输入 这是代码:

;this program takes input of hex and converts it to decimal format and repeats based on user input
org 100h
section .data

prompt0: db "Hello, my name is ####.    Let's do this....." 
prompt1: db "Please enter a hexadecimal digit: $"
prompt2: db 0Dh,0Ah, "In decimal it is:    $"
prompt3: db 0Dh,0Ah, "Do you want to do it again?: $"
prompt4: db 0Dh,0Ah, "Illegal character. -- Enter 0..9 or A..F: $"
buffer:  db "xxxxx"
endBuf:  db      ".$"
ten:     db      10

section .text   

start:  
mov     ah,9        ; print hello message
mov     dx,prompt0
int     21h

mov     ah,9        ; print prompt asking for hex
mov     dx,prompt1
int     21h
;input hex value 
mov     bx,0        ; bx holds input value
mov     ah,1        ; input char function
int     21h     ; read char into al


check1:
cmp     al, '0'     ; check if digit is 0-9
jl       top1       ; no? jump to chek if char
cmp     al, '9'     ; check if digit is withing 0-9
jg       top1       ; no? jump to check if char
sub     al, '0'     ; convert to decimal value
jmp     print_decimal       ; jump to print out in decimal

top1:   
cmp     al, 'A'     ; is char upercase?
jl      check_lower ; no? jump to check for lowercase
cmp     al, 'F'     ; is char within A-F?
jg      check_lower ; no? jump to check for lowercase
sub al, 'A'-10       ; convert to decimal
jmp     print_decimal       ; jump to print in decimal

check_lower:
cmp     al,'a'      ; is char lower case a-f?
jl      error       ; no? jump to error
cmp     al,'f'      ; is char lowercase a-f?
jg       error      ; no? jump to error
sub     al,'a'-10   ; convert to decimal

print_decimal:          ; print al as decimal 0-255
mov        di,endBuf    ; set buffer pointer to char after digit

next_digit:
dec     di      ; advance buffer pointer to next char
xor     ah, ah      ; clear highest byte ax for division
div     byte [ten]  ; ah = ax % 10
add    ah,'0'       ; convert ah to code
mov    [di], ah     ; copy to buffer
or     al, al       ; set condition codes to al
jnz    next_digit   ; jump if more digits available

print:  
mov     dx,prompt2  ; assign address for prompt2
mov     ah,9        ; print char fcn
int     21h     ; print it
mov     dx,di       ; offset address for converted decimal
mov     ah,9        ; print char fcn
int     21h     ; print it all
jmp     continue    ; repeat process

error:
mov     dx,prompt4  ; assign address to prompt4 
int      21h        ; print it all
jmp     continue    ; repeat process
continue:
mov      dx,prompt3
cmp      al,'Y'
je       start
cmp      al,'y'
je       start

;check for quit
cmp     al, 'N'     ; handle no/ quit character
je      Exit
cmp     al, 'n'
je      Exit

; return to DOS
Exit: 
    mov     ah, 04Ch      ; DOS function: Exit program 
    mov     al, 0         ; Return exit code value 
    int     21h           ; Call DOS. Terminate program 

0 个答案:

没有答案