You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

32位汇编cmp指令使用报错求助:operand sizes不匹配等问题

汇编代码错误修正:操作数不匹配与逻辑问题

错误分析与修复

1. 操作数大小不匹配(行17、18)

原代码中:

mov eax, byte input
mov ebx, byte yes
  • eax/ebx是32位寄存器,byte是8位数据,二者操作数大小不匹配。
  • 访问内存中的input和yes时,必须用[]表示取内存地址中的内容,而非直接使用符号本身。

修正为:

mov al, byte [input]   ; 取input内存中的1字节到al(eax的低8位)
mov bl, byte [yes]     ; 取yes内存中的1字节到bl(ebx的低8位)

2. 错误的比较逻辑与多余指令

原代码中cmp ax, bx属于多余且逻辑错误的指令:

  • 我们仅需比较单个字节的输入字符,无需使用16位寄存器ax/bx。
  • 原逻辑中,若输入不是'y'就直接跳转到_wrong,无法区分合法输入'n'与其他非法输入。

修正逻辑:

cmp al, bl
je _right              ; 输入是'y'则跳转到正确分支
mov bl, byte [no]      ; 加载正确的'n'到bl
cmp al, bl
je _wrong              ; 输入是'n'则跳转到错误分支
jmp _badinput          ; 其他输入跳转到非法输入分支

3. stdin调用错误

原代码读取输入时mov ebx,1错误:

  • stdin的文件描述符是0,1是stdout的文件描述符,此错误会导致无法正确读取输入。

修正为:

;get answer using stdin
mov eax, 3
mov ebx, 0             ; stdin的文件描述符是0
mov ecx, input
mov edx, 1
int 80h

4. 未定义符号msg的错误

该错误是前面语法错误导致的汇编器连锁误报,修正上述错误后即可自动消失。

修正后的完整代码

section .text
    global _start       
_start:         
    ;print question using stdout
    mov eax, 4
    mov ebx, 1
    mov ecx, question
    mov edx, len_question
    int 80h
    ;get answer using stdin
    mov eax, 3
    mov ebx, 0
    mov ecx, input
    mov edx, 1
    int 80h
    ;compare answer to right answer
    mov al, byte [input]
    mov bl, byte [yes]
    cmp al, bl
    je _right ;jump to right if right
    mov bl, byte [no]
    cmp al, bl
    je _wrong ;jump to wrong if wrong
    jmp _badinput ;jump to badinput if bad input
    
_right:
    ;print right message
    mov eax, 4
    mov ebx, 1
    mov ecx, msg_right
    mov edx, len_msg_right
    int 80h
    jmp _end ;jump to end
    
_wrong:
    ;print message for wrong answer
    mov eax, 4
    mov ebx, 1
    mov ecx, msg_wrong
    mov edx, len_msg_wrong
    int 80h
    jmp _end ;jump to end
    
_badinput:
    ;print message for bad input
    mov eax, 4
    mov ebx, 1
    mov ecx, msg_badinput
    mov edx, len_msg_badinput
    int 80h
    jmp _end ;jump to end
    
_end:
    ;end the program
    mov eax, 1      
    mov ebx, 0
    int 0x80  

section .data
    question db 'Is the world round?',0xa   
    msg_right db 'That´s right!'
    msg_wrong db 'That´s wrong'
    msg_badinput db 'Bad input! Choose yes (y) or no (n)!'
    yes db 'y'
    no db 'n'
    len_question    equ $ - question
    len_msg_right equ $ - msg_right
    len_msg_wrong equ $ - msg_wrong
    len_msg_badinput equ $ - msg_badinput
    
section .bss
    input resb 1

内容的提问来源于stack exchange,提问作者B3d3vtvng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 11:25:07