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

AT&T 32位汇编ASCII转整数异常:第四个数字起输出为0求助

问题排查:AT&T 32位汇编ASCII转整数异常

问题场景

作为ASM新手,使用AT&T 32位汇编将以下ASCII文本转换为整数(需跳过ASCII值为44的逗号和10的换行符):

1,10,2,2\n2,7,6,7\n3,9,6,5\n4,11,8,6\n5,3,7,6\n6,12,11,3\n7,10,10,7\n8,4,2,1\n9,3,1,4\n10,5,8,8

转换时前三个数字输出正常,但从第四个开始持续输出0,程序提前退出循环。

核心代码

转换循环

conversion_loop:
    xor %ecx, %ecx
    cmpl $0, (%esi)
    je id_start
    cmpl $10, (%esi)
    je newline_conversion_loop
    mov $44, %cl
    call str2int
    movb %al, converted_int(%edi)

    jmp end_loop

newline_conversion_loop:
    movb $10, %cl
    call str2int
    movb %al, converted_int(%edi)

end_loop:
    inc %esi
    inc %edi
    jnp conversion_loop

str2int转换函数

str2int:
    push %ebx              
    push %edx

    xor %eax, %eax          
    xor %ebx, %ebx         

while:
    mov (%esi), %bl        

    cmp $0, %bl             
    je end
    
    cmp %cl, %bl            
    je end

    sub $48, %bl            
    mov $10, %edx          
    mul %edx                
    add %ebx, %eax          

    inc %esi                
    jmp while
end:
    pop %edx                
    pop %ebx
    ret

调试信息

GDB中查看%esi指向的ASCII内容:

(gdb) x/128bd $esi

0x804a194:  49  44  49  48  44  50  44  50

0x804a19c:  10  50  44  55  44  54  44  55

0x804a1a4:  10  51  44  57  44  54  44  53

0x804a1ac:  10  52  44  49  49  44  56  44

0x804a1b4:  54  10  53  44  51  44  55  44

0x804a1bc:  54  10  54  44  49  50  44  49

0x804a1c4:  49  44  51  10  55  44  49  48

0x804a1cc:  44  49  48  44  55  10  56  44

0x804a1d4:  52  44  50  44  49  10  57  44

0x804a1dc:  51  44  49  44  52  10  49  48

0x804a1e4:  44  53  44  56  44  56  10  0

converted_int变量内容:

(gdb) x/8bd &converted_int

0x804a214:  1   10  2   0   0   0   0   0

变量声明:

file_content:       .space 128

converted_int:      .space 48

问题原因分析

  1. 循环退出条件错误
    循环末尾使用jnp conversion_loop,jnp是当奇偶标志位(PF)为0时才跳转。而str2int中的mul %edx指令会修改PF标志位:当转换第三个数字(值为2)后,乘法操作的结果奇偶性导致PF被置1,触发jnp不跳转,循环直接终止,后续数字根本没机会处理,所以从第四个位置开始都是初始的0。

  2. 分隔符处理逻辑错误
    当遇到换行符或逗号时,调用str2int并传入对应分隔符的ASCII值,str2int会直接检查当前字符等于传入的%cl值,立即返回eax=0,随后将0存入converted_int,这不符合“跳过分隔符”的需求。正确逻辑应该是:遇到逗号/换行符时直接跳过(仅递增%esi),不调用str2int,也不往converted_int里写值。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 07:15:27