You need to enable JavaScript to run this app. 汇编语言循环超9次异常及双字节变量比较问题求助
优惠活动
大模型
产品
解决方案
定价
更多

汇编语言循环超9次异常及双字节变量比较问题求助

问题修复方案

一、解决循环次数超过9时的无限循环问题

循环陷入无限循环的核心原因是:用户输入的size1、size2存储的是ASCII字符而非十进制数值。比如输入20,size1里实际存的是'2'(ASCII 0x32)和'0'(ASCII 0x30),组合成双字节是0x3230(十进制12848),而循环计数器counter1是从0开始递增的数值(比如20),两者比较时counter1永远小于size1的ASCII值,导致循环无法终止。

需要新增一个ASCII转十进制数值的函数,将输入的2位ASCII字符串转换成16位整数:

; 函数:将ecx指向的2位ASCII字符串转换为双字节数值,存储到ax中
ascii_to_word:
    xor ax, ax          ; 初始化ax为0
    mov bl, [ecx]       ; 取十位字符
    sub bl, '0'         ; 转成数值
    mov cl, 10
    mul cl              ; ax = 十位*10
    mov bl, [ecx+1]     ; 取个位字符
    sub bl, '0'         ; 转成数值
    add ax, bx          ; ax = 十位*10 + 个位
    ret

然后在读取size1和size2后调用这个函数,把ASCII转成数值:

; 读取size1后转换
mov ecx,size1 
mov edx,2
call readthevalue
call ascii_to_word
mov word [size1], ax   ; 把转换后的数值存回size1

; 读取size2后转换
mov ecx,size2 
mov edx,2
call readthevalue
call ascii_to_word
mov word [size2], ax   ; 把转换后的数值存回size2

这样size1和size2就存储十进制数值(比如20),和计数器的数值类型一致,循环就能正常终止。

二、双字节变量的比较与累加问题

1. 双字节变量比较

双字节(16位)变量的比较直接用cmp指令即可,注意使用16位寄存器操作:

mov ax, word [size1]
cmp word [size2], ax   ; 比较size2和size1的数值
je equal_branch        ; 如果相等跳转到对应分支
jle less_or_equal_branch ; 小于等于跳转

2. 解决累加变成50的问题

累加后变成50的问题,同样是因为size2存储的是ASCII字符。比如输入05,size2里是'0'(0x30)和'5'(0x35),直接累加会把0x3035当成数值处理,而非十进制的5。

使用上面的ascii_to_word函数将size2转换成数值5后,再进行累加操作:

; 让size2从0累加至size1的数值(比如size1是5)
mov word [size2], 0
mov ax, word [size1]
.accumulate_loop:
cmp word [size2], ax
jge .accumulate_end
add word [size2], 1
jmp .accumulate_loop
.accumulate_end:

这样处理后,size2会正确累加至size1的数值(比如5),不会出现ASCII导致的错误。

完整修正后的关键代码片段

;function to draw rectnagle
DrawtheRectangle:

;take space between 
call takespaces

;store amount of rows for rectangle
mov ecx,size1 
mov edx,2
call readthevalue
call ascii_to_word
mov word [size1], ax

;take space between 
call takespaces

;store amount of rows for rectangle
mov ecx,size2 
mov edx,2
call readthevalue
call ascii_to_word
mov word [size2], ax

;Display rectangle name
mov eax,4            
mov ebx,1            
mov ecx,shapename1      
mov edx,shapenameLen1     
int 80h     

mov word [counter1], 0 ; reset counter

.loop1:
mov word [counter2], 0 ; start loop and reset counter
mov ax, word [size1] 
cmp word [counter1], ax ; check if we are at the end of loop
jge endRectangle ; if we are end the function

.loop2:
mov ax, word[size2]
cmp word [counter2], ax ; start the second loop
jge .loop2end

mov eax, 4 ; write
mov ecx, star ; star's address
mov ebx, 1 ; to stdout
mov edx, 1 ; msg length is 1
int 0x80 ; write

add word [counter2], 1 ; add row count
jmp .loop2 ; loop

.loop2end:

mov eax, 4 ; write
mov ecx, newline ; newline's address
mov ebx, 1 ; to stdout
mov edx, 1 ; msg length is 1
int 0x80 ; write

add word [counter1], 1 ; add column count
jmp .loop1 ; loop

endRectangle:
ret ; or do something else

; 新增的ASCII转数值函数
ascii_to_word:
    xor ax, ax          
    mov bl, [ecx]       
    sub bl, '0'         
    mov cl, 10
    mul cl              
    mov bl, [ecx+1]     
    sub bl, '0'         
    add ax, bx          
    ret

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 21:10:38