通过x86程序集从argv读取文件名

eva*_*njs 2 x86 nasm argv

我正在尝试读取传递给我的可执行文件的文件名并使用程序集写入该文件.它编译时没有错误但在执行时失败.我的代码到底有什么问题?

BITS 32
segment .data
text db "text"

segment .text

global main

main:
pop ebx 
pop ebx
pop ebx    ; pop pointer to filename into ebx
mov eax,0x5  ;syscall open
mov ecx,0x2   ;flag read/write
int 0x80   ;call kernel
mov ebx,eax   ;save returned file descriptor
mov eax,0x4 ; write syscall
mov ecx,text ;mov pointer to text into ecx
mov edx,0x4 ;string length
int 0x80  ;exit syscall
mov eax,0x1
int 0x80
Run Code Online (Sandbox Code Playgroud)

use*_*653 5

由于你是从libc调用的,你还必须记得你有返回地址,所以你可以返回那里.这与你得到的东西不同,如果你只有一个简单的汇编程序(就像很多教程!).考虑到这一点:

pop ebx    ;; pops return address to libc caller (_start usually)
pop ebx    ;; pops argc
pop ebx    ;; pops argv !!WAS!!: ; pop pointer to filename into ebx
Run Code Online (Sandbox Code Playgroud)

以下是打印第一个参数的方法.你应该可以从那里去(注意:我可能犯了错误):

    BITS 32

    section .text
    global main
    extern strlen

main:
    pop ecx ; Return address
    pop ecx ; argc
    pop ecx ; argv 
    mov ecx, [ecx+4] ; argv[1]

    push ecx
    call strlen
    mov edx, eax ; count
    pop ecx ; buf

    mov eax, 4 ; sys_write
    mov ebx, 1 ; stdout
    int 0x80

    mov eax, 1 ; sys_exit
    mov ebx, 0 ; status
    int 0x80
Run Code Online (Sandbox Code Playgroud)