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

8086汇编爱情计算器结果不符求助:预期输出yes却显示no

汇编爱情计算器调试问题

我尝试编写可复用的汇编过程实现“爱情计算器”,统计两个姓名中包含T、R、U、E、L、O、V、E(大小写均可)的字母次数。该过程统计指定数组字母在姓名数组中的出现次数,将countT(true数组统计值)乘10后加countL(love数组统计值)得到percent,若percent>54则输出“yes”,否则输出“no”。

针对姓名Cinderella和Prince charming,预期countT为6、countL为5,percent为65应输出“yes”,但实际输出“no”,调试无果,恳请帮忙排查错误。

原代码

IDEAL
MODEL small
STACK 100h
DATASEG
    countT db 0
    countL db 0
    percent db 0
    len1 db 10
    len2 db 15
    name1 db 'C','i','n','d','e','r','e','l','l','a'
    name2 db 'P','r','i','n','c','e',' ','C','h','a','r','m','i','n','g'
    true db 't','r','u','e','T','R','U','E'
    love db 'l','o','v','e','L','O','V','E'
    yes_msg db "yes$"
    no_msg db "no$"
    string equ [bp+10]
    len equ [bp+8]
    count equ [bp+6]
    array equ [bp+4]
CODESEG
proc CmpArray
    push bp
    mov bp, sp
    mov si, array
    mov cx, 8 
    ThroughString:
        push cx
        mov di, string
        mov cx, len   ; Reload the length of the string into CX
        ThroughArray:
            mov al, [si]
            mov bl, [di]
            cmp al, bl
            jne continue
            mov ah, count
            inc ah
            mov count, ah ; Store the updated count back to memory
        continue:
            inc di
            loop ThroughArray
        pop cx
        inc si
    loop ThroughString
    pop bp
    ret 8
endp CmpArray
start:
    mov ax, @data
    mov ds, ax
    
    push offset name1 ; Pass the offset of name1
    push offset len1  ; Pass the offset of len1
    push offset countT ; Pass the offset of countT
    push offset true ; Pass the offset of true
    add [countT], ah
    call CmpArray
    
    push offset name2
    push offset len2
    push offset countT
    push offset true
    add [countT], ah
    call CmpArray
    
    push offset name1 ; Pass the offset of name1
    push offset len1  ; Pass the offset of len1
    push offset countL ; Pass the offset of countT
    push offset love ; Pass the offset of true
    add [countL], ah
    call CmpArray
    
    push offset name2
    push offset len2
    push offset countL
    push offset love
    add [countL], ah
    call CmpArray
    
    mov al, 10
    mov bl, [countT]
    mul bl
    add al, [countL]
    mov [percent], al
    cmp [percent], 54
    jg is_greater
    jmp is_not_greater

is_greater:
    mov dx, offset yes_msg
    mov ah, 9
    int 21h
    jmp exit

is_not_greater:
    mov dx, offset no_msg
    mov ah, 9
    int 21h
    jmp exit
    
exit:
    mov ax, 4c00h
    int 21h
END start

错误排查与修正

1. 参数寻址错误

  • 长度参数读取错误:len equ [bp+8]是len变量的偏移地址,原代码mov cx, len会把偏移地址值(而非实际长度数值)加载到CX,导致内层循环次数完全错误。应改为先xor cx, cx清空CX(确保高位为0),再mov cl, [len]读取实际长度到CL。
  • count变量读写错误:count equ [bp+6]是count变量的偏移地址,原代码mov ah, count取的是偏移地址值,而非count的当前统计值,应改为mov ah, [count];更新时mov [count], ah才是正确的内存写入操作。

2. 冗余语句破坏初始值

调用CmpArray前的add [countT], ah和add [countL], ah完全多余,此时AH是未初始化的随机值,会直接覆盖countT/countL的初始0值,导致统计结果错误,必须删除这些语句。

3. 修正后的完整代码

IDEAL
MODEL small
STACK 100h
DATASEG
    countT db 0
    countL db 0
    percent db 0
    len1 db 10
    len2 db 15
    name1 db 'C','i','n','d','e','r','e','l','l','a'
    name2 db 'P','r','i','n','c','e',' ','C','h','a','r','m','i','n','g'
    true db 't','r','u','e','T','R','U','E'
    love db 'l','o','v','e','L','O','V','E'
    yes_msg db "yes$"
    no_msg db "no$"
    string equ [bp+10]
    len equ [bp+8]
    count equ [bp+6]
    array equ [bp+4]
CODESEG
proc CmpArray
    push bp
    mov bp, sp
    mov si, array
    mov cx, 8 
ThroughString:
    push cx
    mov di, string
    xor cx, cx          ; 清空CX,确保高位为0
    mov cl, [len]       ; 读取实际长度到CL
ThroughArray:
    mov al, [si]
    mov bl, [di]
    cmp al, bl
    jne continue
    mov ah, [count]     ; 读取当前count值
    inc ah
    mov [count], ah     ; 写回更新后的count值
continue:
    inc di
    loop ThroughArray
    pop cx
    inc si
    loop ThroughString
    pop bp
    ret 8
endp CmpArray
start:
    mov ax, @data
    mov ds, ax
    
    push offset name1
    push offset len1
    push offset countT
    push offset true
    call CmpArray
    
    push offset name2
    push offset len2
    push offset countT
    push offset true
    call CmpArray
    
    push offset name1
    push offset len1
    push offset countL
    push offset love
    call CmpArray
    
    push offset name2
    push offset len2
    push offset countL
    push offset love
    call CmpArray
    
    mov al, 10
    mov bl, [countT]
    mul bl
    add al, [countL]
    mov [percent], al
    cmp [percent], 54
    jg is_greater
    jmp is_not_greater

is_greater:
    mov dx, offset yes_msg
    mov ah, 9
    int 21h
    jmp exit

is_not_greater:
    mov dx, offset no_msg
    mov ah, 9
    int 21h
    jmp exit
    
exit:
    mov ax, 4c00h
    int 21h
END start

修正后,countT会正确统计为6,countL为5,percent=6*10+5=65,大于54,输出“yes”。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 02:07:13