assembly - 如何让用户的十六进制输入在 x86 程序集(16 位 DOS)中打印为十进制?

标签 assembly x86 hex nasm

我向我的教授寻求帮助。他对自己的任期很深,并不真正关心,所以他只是给了我一个模糊的解决方案。基本任务是获取用户输入(十六进制值),将值转换为十进制,然后将其打印出来。这是我的代码:

; SECOND ASSIGNMENT

    org 100h

;equates
cr equ  0dh         ;carriage return
lf equ  0ah         ;line feed

section .data
prompt0: db 0dh, 0ah, "My name is Brandon Copeland. Prepare to enter data! $"
prompt1: db 0dh, 0ah, "Enter a hex digit: $"
prompt2: db 0dh, 0ah, "In decimal it is: $"
prompt3: db 0dh, 0ah, "Do you want to do it again? Press 'y' or 'Y' to continue $"
prompt4: db "Illegal entry: must be 0 - 9 or A - F $"

section .text
start:
    mov ah,9        ;Display string
    mov dx,prompt0  ;Greeting
    int 21h     ;System call

    mov ah,9        ;Display string
    mov dx,prompt1  ;Prompt for first number
    int 21h     ;System call

    mov     bx,0        ;bx holds input value
    mov     ah,1        ;Reads keyboard character
    int 21h     ;System call

    cmp al, '9'     ;compares input to '9'
    je  print       ;if 9, jump to print
    jl  print       ;if less than 9, jump to print
    ja  decConvert  ;if greater than 9, convert A - F to 10 - 15

decConvert:
    and al,11011111b    ; force uppercase
    sub al,65       ; convert 'A'-'F' to 10-15
    pop bx

    mov ah,9
    mov dx,prompt2
    int 21h

    mov ah,2        ;print char
    mov dl,'1'      ;print '1'
    int 21h

    mov ah,2
    mov dl,bl
    int 21h

    jmp repeat

print:
    mov ah,9        
    mov dx, prompt2
    int 21h

    mov ah,2
    mov     dl,al
    int 21h

repeat:
    mov ah,9
    mov dx, prompt3 ;asks user if wants to do again
    int 21h

    mov bx,0        ;gets user answer
    mov ah,1
    int 21h

    cmp al,'y'      ;if y, restart
    je  start
    cmp al,'Y'      ;if Y, restart
    je  start
    jmp exit        ;otherwise, terminate program       ;

exit:
    mov ah,04ch     ;DOS function: exit
    mov al,0        ;exit code
    int 21h     ;call DOS, exit

在走开之前,我的教授提到,由于所有十六进制值 A - F 都以“1”开头,所以我可以只打印一次“1”,然后要打印下一个数字,我必须弹出al 的内容到另一个寄存器中。如果你查看标签“decConvert”,我会将 al 弹出到 bx 中,然后尝试打印 bl。

数字 0 - 9 的输出很好。然而,每当我尝试输入 A - F 时,每次输出都只是“1”。我到底做错了什么?

最佳答案

显然,你不知道 pop 是做什么的。请参阅指令集引用。

提示:您希望在具有 pop bx 的位置使用 push ax,并且在具有 mov dl 的位置希望使用 pop dx ,bl.

此外,您的 sub al,65 是错误的,因为它转换为 0..5 而不是 '0'..'5' (即您想要 sub al, 17)

关于assembly - 如何让用户的十六进制输入在 x86 程序集(16 位 DOS)中打印为十进制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33114775/

相关文章:

assembly - 确定寄存器值是否等于零的最简单方法是什么?

python -\x 和 unicode 代码点之间的关系

c - 将 1-100 int 乘以 -1 或将所述 int 设置为零是否需要更多时间?

gcc - mfence 和 asm volatile 的区别 ("": : : "memory")

linux - x86-64 位汇编 Linux 输入

linux - ENQCMD 指令的好处和微操作是什么?

c - 在 C 中打印最大十六进制精度

c++ - 将十六进制转换为十进制 C++

c - Shellcode C 程序线束

memory - 获取 x86 AT&T 汇编中数据变量的地址