--------------------------------------------------------------------------------
section .text
global _start ;must be declared for linker (ld)
msg db "hello, world!",0xa ;our dear string
len equ $ - msg ;length of our dear string
_syscall: ;system call
int 0x25
ret
_start: ;tell linker entry point
push dword len ;message length
push dword msg ;message to write
push dword 1 ;file descriptor (stdout)
mov eax,0x3 ;system call number (sys_write)
call _syscall ;call kernel
add esp,12 ;clean stack (3 * 4)
push dword 0 ;exit code
mov eax,0x3f ;system call number (sys_exit)
call _syscall ;call kernel
;no need to clean stack
--------------------------------------------------------------------------------
2.6 创建可执行代码
创建可执行代码分为两个阶段:编译和链接。按照下面的步骤进行:
--------------------------------------------------------------------------------
$ nasm -f elf hello.asm # this will produce hello.o object file
$ ld -s -o hello hello.o # this will produce hello executable
--------------------------------------------------------------------------------
这样就很容易的产生了可执行代码,然后使用命令./hello就可以执行程序了。
3. 汇编资源
若希望深入了解linux汇编,可以参考站点linux的内容,并且下载asmutils,其包含了很多示例代码。也可以参考linux assembly howto..下面是一些linux汇编的一些资源链接。
工程:
;各种使用汇编编写的linux/unix工程,是很好的示例资源
name short description platform os assembler
asmutils miscellaneous utilities, small libc ia32 linux, freebsd (beos) nasm
libasm assembly library (lots of various routines) ia32 linux nasm
e3 wordstar-like text editor ia32 linux, freebsd, beos nasm
ec64 commodore c64 emulator ia32 linux nasm
elf kickers elf kickers and tiny linux executables ia32 linux nasm
blas basic linear algebra subroutines alpha linux, digital unix, winnt gas
asmix several commandline utilities ia32 linux, freebsd gas