在 x86 程序集中偏移指针

问题描述

先编码,后问题。

这段代码一个我正在调用的数据块file,数据块是这样的:

filename
file_pointer
openas

然后,在 generateFile 中,我们有 -20(%ebp),它是指向数据块中第一个元素的指针,或 -8(%ebp)

我们在 main 内部调用这个函数并偏移指针以便能够打印出 filenameopenas

    .section .data

strLOCK0:
    .asciz "r"

strLOCK1:
    .asciz "hello.txt"

strLOCK2:
    .asciz "%s\n"

strLOCK3:
    .asciz "%d\n"
    .section .text
    .globl _start

_start:
    call main
    movl %eax,%ebx
    movl $1,%eax
    int $0x80

    .type generateFile,@function
generateFile:
    pushl %ebp
    movl %esp,%ebp

    subl $20,%esp
    movl 12(%ebp),%ecx
    pushl %ecx
    movl 8(%ebp),%ecx
    pushl %ecx
    call fopen
    movl %eax,%ecx
    movl %ecx,-4(%ebp)
    movl 8(%ebp),-8(%ebp)
    movl -4(%ebp),-12(%ebp)
    movl 12(%ebp),-16(%ebp)
    leal -8(%ebp),-20(%ebp)
    movl -20(%ebp),%eax
    leave
    ret

    .type main,@function
main:
    pushl %ebp
    movl %esp,%ebp

    subl $4,%esp
    movl $strLOCK0,%ecx
    pushl %ecx
    movl $strLOCK1,%ecx
    pushl %ecx
    call generateFile
    movl %eax,-4(%ebp)
    movl -4(%ebp),%ebx
    movl (%ebx),%ecx
    pushl %ecx
    movl $strLOCK2,%ecx
    pushl %ecx
    call printf
    popl %ebx
    movl -4(%ebp),%ebx
    movl -8(%ebx),%ecx
    pushl %ecx
    movl $strLOCK3,%ecx
    pushl %ecx
    call printf
    popl %ebx
    movl $0,%eax
    leave
    ret

问题不是预期的输出

hello.txt
r

相反,我得到:

hello.txt
-852052

我尝试了不同的东西,例如

4(%ebx)

当我得到一个值,但它不起作用时。

我该如何解决这个问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)