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

NASM开发x86引导程序cmp指令报操作数组合无效错误

问题描述

编写x86架构汇编引导程序测试cmp指令使用效果时,编译阶段抛出如下错误:

boot.asm:9: error: invalid combination of opcode and operands

代码逻辑为将bx寄存器的值与16位立即数0x0107做比较,无法定位错误来源,完整出错代码如下:

mov ah, 0x0e            ; int 10/ ah = 0eh -> scrolling teletype BIOS routine

mov bp, 0x8000          ; Set the base of the stack a little above where BIOS
mov sp, bp              ; loads our boot sector - so it won't overwrite us. sp -> 0x8000, bp -> 0x8000

push 0x0107             ; Pushes 0x0107 onto the stack (Test number)

pop bx                  ; Gets the value and stores it in bx
cmp 0x0107, bx          ; Compares the value in bx to 0x0107
je  equal               ; If they are equal, jump to equal
jmp not_equal           ; If they aren't equal, jump to not_equal

equal:               
        mov al, 'E'     ;
        int 0x10        ; Prints a 'E' as for Equal
        jmp end         ; Skips not_equal

not_equal:
        mov al, 'N'     ;
        int 0x10        ; Prints a 'N' as for Not Equal

end:

jmp $           ; Jump forever.

; Padding and magic BIOS number.
times 510 -( $ - $$ ) db 0
dw 0xaa55
错误原因

x86架构汇编中,cmp属于双操作数指令,对操作数的顺序和类型有强制要求:

  • 第一个操作数(目标操作数)只能是寄存器或内存位置,不允许使用立即数
  • 第二个操作数(源操作数)可以是立即数、寄存器或内存位置
  • 指令执行逻辑为用目标操作数减去源操作数,根据运算结果设置CPU标志位,不会修改任意一个操作数的原值

出错代码中cmp 0x0107, bx将立即数0x0107放在了目标操作数位置,属于非法的操作数组合,因此触发编译错误。

修复方案

调换两个操作数的顺序即可,将出错行修改为:

cmp bx, 0x0107          ; 比较bx寄存器的值与立即数0x0107

修改后指令语义完全匹配预期的比较逻辑,即可正常编译运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 09:18:18