【问题标题】:Why does pushing and printing the value in a register affect later comparisons of that register to another?为什么推送和打印寄存器中的值会影响以后将该寄存器与另一个寄存器的比较?
【发布时间】:2014-02-14 03:41:14
【问题描述】:

我正在尝试编写一个简短的汇编程序来比较 2 个字符串。我正在使用 AT&T 语法 并用 gcc 和 -m32 标志组装我的程序。

当我运行代码时,我总是在提示时输入“矩形”。

我编写了以下代码。使用有问题的代码块(包含在 cmets 中) 激活,如果我在提示时输入“矩形”,程序不会打印“您输入了矩形”。 事实上,程序在第一次迭代时就跳出了循环。 如果我注释掉所述代码块,程序确实打印“你输入了矩形”。从此 代码块只打印%eax和%ebx中的值,为什么会影响跳转条件?

.section .data
format:
    .asciz "%s"
test_str:
    .asciz "\n%c%c"
equal_str:
    .asciz "You entered rectangle"
rectangle:
    .asciz "rectangle"

.section .bss
.lcomm element, 100

.section .text
.globl main

main:
    pushl $element
    pushl $format
    call scanf
    addl $8, %esp

    movl $0, %edi
    loop:
    movb element(,%edi,1), %al
    movb rectangle(,%edi,1), %bl

    #BEGIN QUESTIONABLE BLOCK
    pushl %eax
    pushl %ebx
    pushl $test_str
    call printf
    addl $8, %esp
    #END QUESTIONABLE BLOCK

    cmpb %al, %bl
    jne end
    incl %edi
    cmpb $0, %al
    jne loop

pushl $equal_str
call printf
addl $4, %esp
jmp end

end:
    call exit

我想以下信息都不重要,但我真的不知道什么会或不会影响组装,所以我的 CPU 是 Intel Xeon E5-2650,我使用的是 CentOS 6.5。提前致谢!

【问题讨论】:

    标签: string assembly compare att


    【解决方案1】:

    printf%eax 中返回结果,从而破坏你的角色。你这样做:addl $8, %esp。这还不够——你已经推送了三个参数。碰巧的是,这会将您的旧 %eax 留在堆栈中,而 popl %eax 可能会解决您的问题。

    【讨论】:

      猜你喜欢
      • 2019-12-24
      • 1970-01-01
      • 1970-01-01
      • 2019-08-18
      • 2021-03-25
      • 2012-02-10
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      相关资源
      最近更新 更多