| lidaibin ( @ 2007-07-26 10:18:00 |
| Entry tags: | asm, linux |
汇编学习之系统调用
最近工作比较闲散,因此抽空看了看汇编,基本上从头开始,看了前面几章不知不觉也学会
先看看下面通过系统调用实现的hello world代码:
.section .data
msg:
.ascii "Hello world!\n"
.section .text
.globl _start
_start:
movl $4, %eax
movl $1, %ebx
movl $msg, %ecx
movl $13, %edx
int $0x80
movl $1, %eax
movl $0, %ebx
int $0x80
系统调用是通过int 0x80来实现的,eax寄存器中为调用的功能号,ebx、ecx、edx、esi等
as -o helloworld.o helloworld.s
ld -o helloworld helloworld.o
再看看调用C函数的代码:
.section .data
output:
.asciz "Hello world!\n"
.section .text
.globl _start
_start:
pushl $output
call printf
addl $8, %esp
pushl $0
call exit
这个例子相对来说看起来简单得多,将参数压入堆栈调用相应的函数即可,不过要注意的是:1、C
as -o helloworld2.o helloworld2.s
ld -dynamic-linker /lib/ld-linux.so.2 -lc -o helloworld2 helloworld2.o