Chinaunix首页 | 论坛 | 博客
  • 博客访问: 796196
  • 博文数量: 142
  • 博客积分: 3505
  • 博客等级: 中校
  • 技术积分: 1501
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-30 19:30
文章分类

全部博文(142)

文章存档

2012年(33)

2011年(109)

分类: LINUX

2011-09-05 21:23:41

在系统调用中,寄存器存放参数的位置为:

eax:系统调用号,同时返回结果的地址

ebx:第一个参数

ecx:第二个参数

edx:第三个参数

esi:第四个参数

edi:第五个参数

搜集了些资料,写了一段汇编代码:

  1. ;##nasm -f elf hello.asm
  2. ;##ld -s -o hello hello.o
  3. ;syscall num in the register eax
  4. ;parameter is saved in the ebx,ecx,edx,esi and edi,orderly!!
  5. global _start
  6. _start:
  7. xor eax,eax
  8. jmp short string
  9. code:
  10. ;pop esi ;'Hello,world!' is saved in the esi
  11. mov ebx, 1 ;first parameter is saved in the ebx
  12. pop ecx ;second parameter is saved in the ecx
  13. mov edx, 14 ;third parameter is saved in the edx
  14. ;push byte 15 ;just push in the esp
  15. ;push esi
  16. ;push byte 1
  17. mov eax,4
  18. ;push eax
  19. int 0x80
  20. xor eax,eax
  21. push eax
  22. push eax
  23. mov eax,1
  24. int 0x80
  25. string:
  26. call code
  27. db 'Hello,world!',0x0a


编译运行结果如下:

  1. ./hello
  2. Hello,world!

试验二

最为简单的C语言程序:

  1. #include <stdio.h>
  2. int main()
  3. {
  4. write(1,”Hello,world\n”,15);
  5. exit(0);
  6. }

对应AT&T的汇编代码:

  1. 0x08048414 <+0>: push %ebp ;保存main函数的基址指针
  2. 0x08048415 <+1>: mov %esp,%ebp ;调用函数形式的压栈保存
  3. 0x08048417 <+3>: and $0xfffffff0,%esp ;3个字节的指令,将esp进行指令对齐
  4. 0x0804841a <+6>: sub $0x10,%esp ;在栈上分配空间?
  5. 0x0804841d <+9>: movl $0xf,0x8(%esp) ;调用函数从右往左进行压栈,参数3
  6. 0x08048425 <+17>: movl $0x8048510,0x4(%esp) ;参数2的地址进行压栈
  7. 0x0804842d <+25>: movl $0x1,(%esp) ;参数1 的值压栈
  8. 0x08048434 <+32>: call 0x804832c
  9. 0x08048439 <+37>: movl $0x1,(%esp)
  10. 0x08048440 <+44>: call 0x804834c

对应的Intel形式的汇编语言代码:

  1. 8048414: 55 push ebp
  2. 8048415: 89 e5 mov ebp,esp
  3. 8048417: 83 e4 f0 and esp,0xfffffff0
  4. 804841a: 83 ec 10 sub esp,0x10
  5. 804841d: c7 44 24 08 0f 00 00 mov DWORD PTR [esp+0x8],0xf
  6. 8048424: 00
  7. 8048425: c7 44 24 04 10 85 04 mov DWORD PTR [esp+0x4],0x8048510
  8. 804842c: 08
  9. 804842d: c7 04 24 01 00 00 00 mov DWORD PTR [esp],0x1
  10. 8048434: e8 f3 fe ff ff call 804832c
  11. 8048439: c7 04 24 01 00 00 00 mov DWORD PTR [esp],0x1
  12. 8048440: e8 07 ff ff ff call 804834c

采用gdb调试,在write处打断点:

  1. (gdb) list main
  2. 1 #include
  3. 2
  4. 3 int main()
  5. 4 {
  6. 5
  7. 6 write(1,"Hello,world\n",15);
  8. 7 exit(1);
  9. 8 }
  10. Breakpoint 1 at 0x8048439: file hello.c, line 7.
  11. (gdb) run

查看下面的指令:

  1. (gdb) x/2i $eip
  2. => 0x804841d : mov DWORD PTR [esp+0x8],0xf
  3. 0x8048425 : mov DWORD PTR [esp+0x4],0x8048510

查看0x8048510处的内容:

  1. (gdb) x/13cb 0x8048510
  2. 0x8048510: 72 'H' 101 'e' 108 'l' 108 'l' 111 'o' 44 ',' 119 'w' 111 'o'
  3. 0x8048518: 114 'r' 108 'l' 100 'd' 10 '\n' 0 '\000'

这就是传说中的”Hello,world” :-)


阅读(1742) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~