在x86中使用cmpq操作数时出现分段错误

时间:2018-11-30 20:07:46

标签: recursion assembly segmentation-fault x86-64

所以我试图从asm x86中的查询字符串中找到一些用户输入。字符串“ m1 = 9&m2 = 6”中的2个输入分别是“ 9”和“ 6”。

在第14行,我一直遇到分段错误。我似乎不知道为什么,甚至尝试调试,但是在尝试移动时似乎找不到问题。

    .section .rodata
test: 
    .string "m1=9&m2=6"
    .text
    .globl   main
    .type    main, @function
main:
    pushq   %rbp
    movq    %rsp, %rbp
    movq    $test,%rbx
    movq    %rbx,%rax
while3:
   cmpq     $'=',(%rax)          
   incq     %rax          
   jne while3   
innerwhile1:
    cmpq    $'&',(%rax)
    incq    %rax
    jne innerwhile1
    movq    %rax,%r10
while4:
    cmpq     $'=',(%rbx)          
    incq     %rax
    jne while4
innerwhile4:
    cmpq    $'\n',(%rax)
    incq    %rax
    jne innerwhile1
    movq    %rax,%r11
    ret  

如果这是一个基本问题,很抱歉,我对asm很陌生。

谢谢您的时间。

2 个答案:

答案 0 :(得分:3)

incq %rax设置标志,因此jne while3根据inc的结果而不是比较的结果循环。这也是其他循环中的问题。

或者更好,先inc然后cmpb $imm8, -1(%rax),允许将cmp / jcc宏融合到单个比较分支uop中。

此外,cmpq $'\n'仅在整个QWORD的值为0x000000000000000a时才设置ZF。使用cmpb来获得操作数大小=字节。

答案 1 :(得分:1)

标签innerwhile4检查\n后看起来像是几行,但是值.string "m1=9&m2=6"中不存在该符号,因此循环枚举了内存空间,并最终到达了分配给导致分割错误的过程。