首页 > 解决方案 > 偏移 x86 程序集中的指针

问题描述

先编码,后问题。

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

filename
file_pointer
openas

然后,generateFile我们有-20(%ebp)which 是指向数据块中第一个元素的指针,或者-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), %ecx
    movl %ecx, -8(%ebp)
    movl -4(%ebp), %ecx
    movl %ecx, -12(%ebp)
    movl 12(%ebp), %ecx
    movl %ecx, -16(%ebp)
    leal -8(%ebp), %ecx
    movl %ecx, -20(%ebp)
    movl -20(%ebp), %ecx
    movl %ecx, %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, %ecx
    movl %ecx, -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, %ecx
    movl %ecx, %eax
    leave
    ret

问题不是预期的输出:

hello.txt
r

相反,我得到:

hello.txt
-852052

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

4(%ebx)

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

我怎样才能解决这个问题?

标签: pointersassemblyx86att

解决方案


推荐阅读