用于比较的汇编 ASCII 整数

Assembly ASCII integer being used in comparison

我有一个小的汇编程序,当我排除它时,它不会跳转到标签。我怀疑这是由于 ASCII 值和整数之间的比较所致。

section .bss    
    nvalue: resb 4
    value: resb 4

section .data 

    inputPromptNValue db 'Enter a integer: '
    inputPromptNValueLen equ $-inputPromptNValue

    inputPrompt db 'Enter an integer: '
    inputPromptLen equ $-inputPrompt

    msg db 'msg one', 0xa
    msgLen equ $-caseOneMsg

section .text 
global _start   
_start: 

    ;prompt user
    mov eax, 4      
    mov ebx, 1      
    mov ecx, inputPrompt 
    mov edx, inputPromptLen 
    int 80h 

    ;read and store the user input 
    mov eax, 3      
    mov ebx, 0      
    mov ecx, nvalue 
    mov edx, 5       
    int 80h 

    ;
    mov ecx, nvalue
    or ecx, 0x30
    cmp ecx, 0x1    ; <--- this part isn't working
    je someLabel    ; <--- 

    ;.... more labels 

someLabel:
    ;other instructions here

这个想法是基于用户输入的一些整数 (0-9),将选择一个选项(将跳转到某个标签)。

当用户输入值“1”时,我希望跳转到上面的标签 (someLabel)。我该怎么做才能获得这种行为?

The idea is that based on user input of some integer (0-9)

所以根据这个需要,你一次只需要输入一个数字。请注意,我特别使用了 digit 这个词,因为没有小数部分的一个或多个数字仍然是整数。

mov eax, 3      
mov ebx, 0      
mov ecx, nvalue 
mov edx, 1                
int 80h 

现在您已准备好读取内存位置 nvalue 的值

mov cl, [nvalue]

如果您没有按其他键,它将在 39H -> 30H 范围内。在您的程序中检查这些类型的错误是个好主意。不用做任何其他事情,你的测试就可以了;

cmp cl,31H
jz  somelabel

cmp cl,'1'
jz  somelabel

甚至

and cl,0xF
cmp cl, 1
jz  somelabel

这些场景中的每一个都会跳转到 somelabel

仅使用任务所需的数据宽度 byte、word、dword、qword,但如果内存位置 nvalues 的内容为;

32 00 00 00

然后

mov ecx, [nvalue]
cmp ecx, 32H

... even ...

cmp ecx, '2'

仍然可以工作,但你会不必要地强迫汇编程序生成更大的代码。

尽管 ECX 被认为是通用寄存器,但具有额外的功能,因此除非有原因,大多数情况下最好使用 AL AX 或 EAX 进行计算和比较。