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

8086汇编中光标位置不更新问题求助

8086汇编字符匹配位置输出问题排查

原始代码

.model small
org 100h

.data 
msg1 db "Enter a letter: $"
word db "BABA$"
word_length db 4 ; length of word
input1 db 10,?,10 dup('$') ; buffer for input
letter_pos db 0 ; counter for letter position

.code
main proc
    
    mov ah, 09h
    lea dx, msg1
    int 21h
    
    mov ah, 0ah
    lea dx, input1
    int 21h
    
    mov ah, 02h ; setting initial cursor position
    mov dh, 1
    mov dl, 0
    int 10h
 
    
    
    ; main code
    
    lea si, input1[2]
    lea di, word
    mov cl, word_length
    
compare:
    cmp cl, 0
    je exit
    mov bl, [si]
    mov bh, [di]
    cmp bl, bh
    je correct_letter
    inc letter_pos
    inc di
    dec cl
    jmp compare
    
    
correct_letter:
    ; set cursor position
    mov ah, 02h
    mov dh, 1
    mov dl, letter_pos
    int 10h
    
    mov ah, 09h
    lea dx, input1+2
    int 21h
    
    inc letter_pos
    mov al, letter_pos
    dec cl
    jmp compare
    


exit:
    MOV ah, 4ch
    int 21h
    
main endp
end main

需求与问题

我编写上述8086汇编代码,期望实现:输入一个字母后,在第1行对应匹配单词"BABA"的位置输出该字母(如输入A时,在第1、3列输出A;输入B时,在第0、2列输出B)。但实际运行时光标位置未按预期更新,代码无法达成目标效果。我是汇编新手,尝试过修改org等设置但无效,请求帮助排查问题以完成学校项目。

问题分析

  1. 匹配后未移动字符串指针:在correct_letter块中,匹配字符后仅更新了letter_pos和循环计数器cl,未移动di指针,导致下一次循环重复匹配同一字符,引发位置错误甚至死循环。
  2. 输出方式错误:使用int 21h 09h输出input1+2时,会附带输入后的回车符和缓冲区的$,导致光标换行,破坏输出位置。
  3. 冗余代码:mov al, letter_pos语句未被使用,属于无效代码。
  4. 字符串定义冗余:word末尾的$会干扰字符匹配逻辑(虽然word_length设为4,但规范上应仅保留目标匹配字符)。

修复后的代码

.model small
org 100h

.data 
msg1 db "Enter a letter: $"
word db "BABA"       ; 移除末尾$,避免干扰字符匹配
word_length db 4     ; 单词长度固定为4
input1 db 10,?,10 dup('$') ; 输入缓冲区
letter_pos db 0      ; 当前字符位置计数器

.code
main proc
    
    mov ah, 09h
    lea dx, msg1
    int 21h
    
    mov ah, 0ah
    lea dx, input1
    int 21h
    
    ; 可选:清屏让输出更整洁
    mov ah, 06h
    mov al, 0
    mov bh, 07h
    mov cx, 0
    mov dx, 184fh
    int 10h
    
    ; 设置初始光标到第1行(BIOS行号从0开始,对应屏幕第二行)第0列
    mov ah, 02h
    mov dh, 1
    mov dl, 0
    int 10h
 
    ; 主逻辑初始化
    lea si, input1[2]  ; 指向输入的字符
    lea di, word       ; 指向单词首字符
    mov cl, word_length; 循环计数器
    
compare:
    cmp cl, 0
    je exit            ; 循环结束则退出
    mov bl, [si]       ; 读取输入字符
    mov bh, [di]       ; 读取单词当前字符
    cmp bl, bh
    je correct_letter  ; 匹配则跳转到输出逻辑
    
    ; 不匹配时更新位置、指针和计数器
    inc letter_pos
    inc di
    dec cl
    jmp compare
    
correct_letter:
    ; 设置光标到匹配位置
    mov ah, 02h
    mov dh, 1
    mov dl, letter_pos
    int 10h
    
    ; 输出单个字符,避免多余内容
    mov ah, 02h
    mov dl, [si]
    int 21h
    
    ; 更新位置、指针和计数器,继续循环
    inc letter_pos
    inc di
    dec cl
    jmp compare

exit:
    mov ah, 4ch
    int 21h
    
main endp
end main

修复说明

  • 移除word末尾的$,确保匹配的是目标单词的4个字符。
  • 将int 21h 09h输出改为int 21h 02h,仅打印单个输入字符,避免多余的回车或符号破坏光标位置。
  • 在correct_letter块中添加inc di,确保每次匹配后移动到单词的下一个字符,避免重复匹配。
  • 删除无意义的mov al, letter_pos语句。
  • 新增可选清屏操作,让输出结果更清晰易读。

内容的提问来源于stack exchange,提问作者PRINCE ROLAND DOBLAS

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 00:42:02