数天来,我一直在努力寻找答案,最后在这里发布以寻求帮助.这项练习对我来说纯粹是学术性的,但是到了某种程度,我只需要了解为什么这行不通或我做错了什么.
section .text
global _start
_start:
pop eax
pop ebx
pop ecx
_exit:
mov eax, 1
mov ebx, 0
int 0x80
编译/链接:
$nasm -f elf -o test.o test.asm
$gcc -o test test.o
在gdb中使用参数“ 5”运行它:
$gdb test
...
(gdb) b _exit
Breakpoint 1 at 0x8048063
(gdb) r 5
Starting program: /home/rich/asm/test 5
Breakpoint 1, 0x08048063 in _exit ()
(gdb) i r
eax 0x2 2
ebx 0xbffff8b0 -1073743696
ecx 0xbffff8c8 -1073743672
edx 0x0 0
esp 0xbffff78c 0xbffff78c
ebp 0x0 0x0
...
所以eax在这里很有意义-它是0x2或2,argc.我的问题是:如何将值“ 5”(或0x5)存入寄存器?据我了解,ecx是指向我的值5的指针,那么如何将其“取消引用”为可用的数字,即我可以做算术运算的数字呢?
解决方法:
您想怎么办?您的理解是正确的:内核将argc计数以相反的顺序推入堆栈的顶部,在其下方为argv [0] … argv [argc-1](即,堆栈的顶部/最低的内存地址保存着第一个论点).您可以在系统上的任何二进制文件中使用gdb进行检查:
$echo "int main(){return 0;}" > test.c
$gcc test.c
$gdb ./a.out
(gdb) b _start
(gdb) r A B C D E
(gdb) p ((void**)$rsp)[0]
$2 = (void *) 0x6
(gdb) p (char*)((void**)$rsp)[1]
$4 = 0x7fffffffeab7 "/home/andy/a.out"
(gdb) p (char*)((void**)$rsp)[2]
$5 = 0x7fffffffeac8 "A"
(gdb) p (char*)((void**)$rsp)[3]
$6 = 0x7fffffffeaca "B"
(gdb) p (char*)((void**)$rsp)[4]
$7 = 0x7fffffffeacc "C"
(gdb) p (char*)((void**)$rsp)[5]
$8 = 0x7fffffffeace "D"
(gdb) p (char*)((void**)$rsp)[6]
$9 = 0x7fffffffead0 "E"
您是否正在问如何解析字符串?这是一个涉及更多的问题.