- 寫一段源程序
; ----------------------------------------------------------------------------------------
; Writes "Hello, World" to the console using only system calls. Runs on 64-bit Linux only.
; To assemble and run:
;
; nasm -felf64 hello.asm && ld hello.o && ./a.out
; ----------------------------------------------------------------------------------------
global _start
section .text
_start: mov rax, 1 ; system call for write
mov rdi, 1 ; file handle 1 is stdout
mov rsi, message ; address of string to output
mov rdx, 13 ; number of bytes
syscall ; invoke operating system to do the write
mov rax, 60 ; system call for exit
xor rdi, rdi ; exit code 0
syscall ; invoke operating system to exit
section .data
message: db "Hello, World", 10 ; note the newline at the end
查看64位ELF文件的Header格式。
; ----------------------------------------------------------------------------------------
; Writes "Hello, World" to the console using only system calls. Runs on 32-bit Linux only.
; To assemble and run:
;
; nasm -felf hello.asm && ld -m elf_i386 -s -o a.out hello.o
; ----------------------------------------------------------------------------------------
global _start
section .text
_start: mov eax, 1 ; system call for write
mov edi, 1 ; file handle 1 is stdout
mov esi, message ; address of string to output
mov edx, 13 ; number of bytes
syscall ; invoke operating system to do the write
mov eax, 60 ; system call for exit
xor edi, edi ; exit code 0
syscall ; invoke operating system to exit
section .data
message: db "Hello, World", 10 ; note the newline at the end
(此文件能編譯鏈接团秽,但在86-64運(yùn)行時(shí)報(bào)錯(cuò):)
fht@ubuntu:~$ ./hello32
Illegal instruction (core dumped)
查看32位ELF文件的Header格式主胧。
32為的程序起始位置為0x08048000,64位為0x0000000000400000。 程序的開(kāi)始真正執(zhí)行的入口地址习勤,32位的為偏移地址0x80處踪栋。64位的為偏移地址0xb0