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

x86汇编项目求助:单条名称删除失败+索引显示异常

x86汇编程序问题排查与修复建议

问题1:仅输入一个名称时无法执行删除操作

问题定位

删除逻辑中更新numePointer的方式错误。原代码在执行rep movsb后,将numePointer设为被删除元素的起始地址di,这仅在删除最后一个元素时生效;当删除唯一元素时,虽然逻辑上看似正确,但实际因cx=0(无数据移动),后续依赖numePointer判断列表结束的逻辑会出现异常,且该写法本质不符合“删除元素后总长度减少6字节”的核心逻辑。

另外,维护名称数量的numes变量未在添加名称时递增,导致数量统计错误,但这并非删除失败的直接原因。

修改建议

  1. 在scrienume段末尾添加inc numes,每次添加名称后正确递增数量:
scrienume:
  mov  dx, offset prompt
  mov  ah, 09h
  int  21h
  mov  cx, 5
  mov  si, numePointer
read_char:
  mov  ah, 01h
  int  21h
  mov  [si], al
  inc  si
  loop read_char
  mov  byte ptr [si], '$'
  inc  si
  mov  numePointer, si ; numePointer += 6
  inc numes ; 新增:维护名称数量
  jmp  bucla
  1. 修改sterge段中更新numePointer的代码,替换为直接减去6字节(每个名称的固定长度):
sterge:
    ; Prompt user for position of name to delete
    mov dx, offset string
    mov ah, 09h
    int 21h
    ; Read position from user
    mov ah, 01h
    int 21h
     sub  al, 49      ; AL=["1","9"] 1-based input -> AL=[0,8] 0-based index
  mov  ah, 6
  mul  ah          ; -> AX = {0,6,12,18,24,30,36,42,48}
  add  ax, offset nume
  cmp  ax, numePointer
  jnb  invalidPosition
  mov  di, ax
  lea  si, [di + 6]
  mov  cx, numePointer
  sub  cx, si
  cld
  rep movsb
  ; mov numePointer, di  ; 注释原错误代码
  mov ax, numePointer
  sub ax, 6               ; 删除一个元素,总长度减少6字节
  mov numePointer, ax
  dec  numes
  jmp  bucla

问题2:名称数量超过10个时索引显示异常

问题定位

当前索引存储在numeIndex的单个字节中(numeIndex db 13,10,49,46,36),仅能处理0-9的单个数字索引。当名称数量超过10时,索引递增到':'(ASCII 58,'9'的下一个字符),无法正确显示两位数字(如10、11等)。

修改建议

  1. 扩展numeIndex结构,支持两位数字索引:
numeIndex db 13, 10, '0', '1', '.', '$' ; 十位初始为'0',个位初始为'1'
  1. 修改lista段的索引递增逻辑,处理进位(个位从9到0时,十位加1):
lista:
mov  byte ptr [numeIndex + 2], '0' ; 十位重置为'0'
mov  byte ptr [numeIndex + 3], '1' ; 个位重置为'1'
mov  dx, offset nume
print_names:
  push dx                        ; (1)
  mov  dx, offset numeIndex
  mov  ah, 09h
  int  21h
  ; 处理索引递增
  inc  byte ptr [numeIndex + 3]  ; 个位加1
  cmp  byte ptr [numeIndex + 3], '9'
  jbe  index_ok                  ; 个位<=9,无需进位
  mov  byte ptr [numeIndex + 3], '0' ; 个位重置为0
  inc  byte ptr [numeIndex + 2]  ; 十位加1
index_ok:
  pop  dx                        ; (1)

  mov  ah, 09h
  int  21h
  add  dx, 5 + 1
  cmp  dx, numePointer ; check if the current name is the last one
  jb   print_names
  jmp  bucla           ; return to main loop

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 12:15:25