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

DosBox汇编计算器出现“Operand types do not match”错误求助

问题分析与修正方案

你的汇编代码存在几处明确的语法/逻辑错误,和DosBox配置无关,下面逐一说明并给出修正后的代码:

1. 字符串显示时未设置正确的功能号

在输入第二个数字、输入运算符前,你调用int 21h显示提示字符串,但没有先设置ah=09h(DOS字符串显示功能),此时ah保留了上一次调用的01h(字符输入功能),会导致错误的功能调用,同时也会引发后续的操作数不匹配问题。

2. 结果显示时操作数类型不匹配

result是dw(16位字类型),而dl是8位寄存器,直接执行mov dl, result会因操作数位数不匹配触发Operand types do not match错误。由于你的代码仅处理单个数字(0-9)的运算,结果不会超过81(乘法最大值),只需取result的低8位即可匹配dl的宽度。

3. 输入数字时的冗余赋值

num1和num2是dw类型,但你输入的是单个字符转成的8位数值,直接用mov num1, al即可(汇编器会自动将8位值零扩展为16位存入字变量),不需要借助ax,避免ah中残留的垃圾值影响后续运算。


修正后的完整代码

.model small
.stack 100h

.data
    num1 dw ?
    num2 dw ?
    result dw ?
    operator db ?
    msg db 0Dh, 0Ah, "Input your first number: $"
    msg2 db 0Dh, 0Ah, "Input your second number: $"
    msg3 db 0Dh, 0Ah, "Input the operator (+, -, *, /): $"
    msg4 db 0Dh, 0Ah, "Result: $"
    err_msg db 0Dh, 0Ah, "Invalid operator!$"  ; 新增专门的错误提示字符串

.code
    mov ax, @data
    mov ds, ax

    ; Input your first number
    mov ah, 09h
    lea dx, msg
    int 21h

    mov ah, 01h
    int 21h
    sub al, '0'
    mov num1, al  ; 直接用al赋值,自动零扩展为16位

    ; Input your second number
    mov ah, 09h  ; 新增:设置字符串显示功能号
    lea dx, msg2
    int 21h

    mov ah, 01h
    int 21h
    sub al, '0'
    mov num2, al

    ; Input your operator
    mov ah, 09h  ; 新增:设置字符串显示功能号
    lea dx, msg3
    int 21h

    mov ah, 01h
    int 21h
    mov operator, al

    ; Perform arithmetic operation
    cmp operator, '+'
    je addition
    cmp operator, '-'
    je subtraction
    cmp operator, '*'
    je multiplication
    cmp operator, '/'
    je division

    ; Display error message for invalid operator
    mov ah, 09h
    lea dx, err_msg  ; 使用专门的错误提示
    int 21h

    mov ax, 4C00h
    int 21h

addition:
    mov ax, num1
    add ax, num2
    mov result, ax
    jmp display_result

subtraction:
    mov ax, num1
    sub ax, num2
    mov result, ax
    jmp display_result

multiplication:
    mov ax, num1
    mul num2
    mov result, ax
    jmp display_result

division:
    mov ax, num1
    mov bx, num2
    xor dx, dx
    div bx
    mov result, ax
    jmp display_result

display_result:
    mov ah, 09h
    lea dx, msg4
    int 21h

    mov ah, 02h
    mov dl, byte ptr result  ; 取result的低8位,匹配dl的8位宽度
    add dl, '0'
    int 21h

    mov ax, 4C00h
    int 21h
    
end

额外说明

  • 新增了专门的无效运算符提示字符串,避免原代码错误显示结果提示的问题
  • 目前代码仅支持单个数字(0-9)的运算,如果需要处理多位数输入,需要修改输入逻辑(比如循环读取字符并累加数值)
  • 除法运算未处理除数为0的情况,若需要可以在division分支前增加判断逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 19:38:15