【问题标题】:GNU Assembler, function call using stack, seg faultGNU 汇编器,使用堆栈的函数调用,段错误
【发布时间】:2014-06-05 19:43:01
【问题描述】:

我在调用使用堆栈的函数时遇到问题。程序的输出包含很多垃圾,好像字符串长度不对:

.code32

.equ kernel, 0x80
.equ stdout, 0x01
.equ write, 0x04
.equ exit, 0x01

.section .data
sum:
    .ascii "text"
.equ lensum, . - sum

.section .text

    .type writetxt, @function
writetxt:
    movl $write, %eax
    movl $stdout, %ebx
    popl %ecx
    pop %edx
    int kernel
    ret

.global _start

_start:
    pushl $lensum
    pushl $sum
    call writetxt

    movl $exit, %eax
    movl $0, %ebx
    int $kernel

我知道函数返回地址驻留在堆栈上,但我不知道如何修复它

【问题讨论】:

    标签: assembly gnu


    【解决方案1】:

    正如您自己所指出的,当您输入writetxt 时,返回地址将位于堆栈顶部(即(%esp))。您在 call 之前推送的最后一个 32 位值将位于 4(%esp),依此类推。

    所以不是

    popl %ecx
    popl %edx
    

    你应该使用类似的东西

    movl 4(%esp), %ecx
    movl 8(%esp), %edx
    

    之后不要忘记调整堆栈指针。例如,您可以在call writetext 指令之后放置一个addl $8, %esp

    【讨论】:

      猜你喜欢
      • 2016-03-14
      • 1970-01-01
      • 2021-01-28
      • 2019-11-25
      • 2014-07-12
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      相关资源
      最近更新 更多