在汇编中比较输入和字符时出错



我正在尝试输入c的字符。然后根据打印出的消息,然而,当我尝试比较我存储在变量内的输入值(conv)的值时,它不会产生预期的结果,我不知道为什么。我将在下面附上该特定部分的代码,并解释其输出。

;Read and store the user input
mov eax, 3
mov ebx, 0
mov ecx, conv  
mov edx, 4 
int 0x80

;Output the letter entered
mov eax, 4
mov ebx, 1
mov ecx, conv
mov edx, 4
int 80h

cmp ecx,66h ;comparing with ascii char that has hex val 66h i.e 'f'
je toFarenheit
toCelcius:
;display test message for C
mov edx, lenTestC    ;message length
mov ecx, testMsgC    ;message to write
mov ebx, 1      ;file descriptor (stdout)
mov eax, 4      ;system call number (sys_write)
int 0x80        ;call kernel
jmp exit  
toFarenheit:
;display test message for F
mov edx, lenTestF    ;message length
mov ecx, testMsgF    ;message to write
mov ebx, 1      ;file descriptor (stdout)
mov eax, 4      ;system call number (sys_write)
int 0x80        ;call kernel
exit:
mov eax, 1      ;system call number (sys_exit)
int 0x80        ;call kernel
section .bss
res resb 4
conv resb 4

虽然ecx中的输入是f并且它与66h进行比较,66h也是f,但对于je语句,它的值不会为true,因此不会跳转到华氏温度,因此toCelcius块运行,然后程序跳转退出,然后结束。

ecx或低字节cl中的值不对应任何ASCII码点。ecx中的值用于将指向内存的指针传递给系统调用。因此,ASCII码可以在conv缓冲区的第一个字节中找到。此外,您不必自己将字母转换为数字代码,NASM允许在单引号中指定字母以自动插入数字代码。因此,你的比较应该如下所示:

cmp byte [conv], 'f'

最新更新