通过 x86 程序集从 argv 读取文件名 [英] Reading filename from argv via x86 assembly
问题描述
我正在尝试读取传递给我的可执行文件的文件名并使用程序集写入该文件.它编译没有错误,但在执行时失败.我的代码到底有什么问题?
I'm and trying to read a filename passed to my executable and write to that file using assembly. It compiles without error but fails when executed. What is wrong with my code exactly?
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
推荐答案
由于您是从 libc 调用的,因此您还必须记住您有返回地址,以便您可以返回那里.如果你只有一个简单的汇编程序(就像很多教程一样!),这与你得到的不同.考虑到这一点:
Since you're being called from libc you also have to recall that you have return address so you could return there. This is unlike what you get if you just have a bare-bones assembly program (like a lot of tutorials!). With that in mind:
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
这是打印第一个参数的方法.你应该可以从那里开始(注意:我可能犯了错误):
Here's how you can print the first argument. You should be able to go from there (beware: I might have made mistakes):
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
这篇关于通过 x86 程序集从 argv 读取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!