section .data hello: db 'Hello world!',10 ; 'Hello world!' plus a linefeed character helloLen: equ $-hello ; Length of the 'Hello world!' string section .text global _start _start: mov ecx,5 ; display the string 5 times_loop: mov eax,4 ; The system call for write (sys_write) mov ebx,1 ; File descriptor 1 - standard output push ecx ; save ecx as it is gonna be used as param to sys_write mov ecx,hello ; Put the offset of hello in ecx mov edx,helloLen ; helloLen is a constant, so we don't need to say ; mov edx,[helloLen] to get it's actual value int 80h ; Call the kernel pop ecx ; restore ecx (counter) loop _loop mov eax,1 ; The system call for exit (sys_exit) mov ebx,0 ; Exit with return code of 0 (no error) int 80hSteps:
- Save the file as syscall.asm
- Execute: nasm -f elf syscall.asm
- Execute: ld -s -o syscall syscall.o
- run it as: ./syscall
- To check the object file, we can use objdump, elfdump, or readelf. For example:
$ readelf -a ./syscall.o ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: REL (Relocatable file) Machine: Intel 80386 Version: 0x1 Entry point address: 0x0 Start of program headers: 0 (bytes into file) Start of section headers: 64 (bytes into file) Flags: 0x0 Size of this header: 52 (bytes) Size of program headers: 0 (bytes) Number of program headers: 0 Size of section headers: 40 (bytes) Number of section headers: 7 Section header string table index: 3 Section Headers: [Nr] Name Type Addr Off Size ES Flg Lk Inf Al [ 0] NULL 00000000 000000 000000 00 0 0 0 [ 1] .data PROGBITS 00000000 000160 00000d 00 WA 0 0 4 [ 2] .text PROGBITS 00000000 000170 00002b 00 AX 0 0 16 [ 3] .shstrtab STRTAB 00000000 0001a0 000031 00 0 0 1 [ 4] .symtab SYMTAB 00000000 0001e0 000080 10 5 7 4 [ 5] .strtab STRTAB 00000000 000260 000029 00 0 0 1 [ 6] .rel.text REL 00000000 000290 000008 08 4 2 4 $ readelf -a ./syscall.o ELF Header:s: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32unknown) Data: 2's complement, little endianpecific) Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: REL (Relocatable file) Machine: Intel 80386 Version: 0x1 0x290 contains 1 entries: Entry point address: 0x0Value Sym. Name Start of program headers: 0 (bytes into file) Start of section headers: 64 (bytes into file) Flags: 0x0e. Size of this header: 52 (bytes) Size of program headers: 0 (bytes) Number of program headers: 0Vis Ndx Name Size of section headers: 40 (bytes)UND Number of section headers: 7DEFAULT ABS syscall.asm Section header string table index: 3DEFAULT 1 3: 00000000 0 SECTION LOCAL DEFAULT 2 Section Headers: 0 NOTYPE LOCAL DEFAULT 1 hello [Nr] Name Type Addr Off Size ES Flg Lk Inf Al [ 0] NULL 00000000 000000 000000 00 0 0 0 [ 1] .data PROGBITS 00000000 000160 00000d 00 WA 0 0 4 [ 2] .text PROGBITS 00000000 000170 00002b 00 AX 0 0 16 [ 3] .shstrtab STRTAB 00000000 0001a0 000031 00 0 0 1 [ 4] .symtab SYMTAB 00000000 0001e0 000080 10 5 7 4 [ 5] .strtab STRTAB 00000000 000260 000029 00 0 0 1 [ 6] .rel.text REL 00000000 000290 000008 08 4 2 4 Key to Flags: W (write), A (alloc), X (execute), M (merge), S (strings) I (info), L (link order), G (group), x (unknown) O (extra OS processing required) o (OS specific), p (processor specific) There are no section groups in this file. There are no program headers in this file. Relocation section '.rel.text' at offset 0x290 contains 1 entries: Offset Info Type Sym.Value Sym. Name 00000011 00000201 R_386_32 00000000 .data There are no unwind sections in this file. Symbol table '.symtab' contains 8 entries: Num: Value Size Type Bind Vis Ndx Name 0: 00000000 0 NOTYPE LOCAL DEFAULT UND 1: 00000000 0 FILE LOCAL DEFAULT ABS syscall.asm 2: 00000000 0 SECTION LOCAL DEFAULT 1 3: 00000000 0 SECTION LOCAL DEFAULT 2 4: 00000000 0 NOTYPE LOCAL DEFAULT 1 hello 5: 0000000d 0 NOTYPE LOCAL DEFAULT ABS helloLen 6: 00000005 0 NOTYPE LOCAL DEFAULT 2 _loop 7: 00000000 0 NOTYPE GLOBAL DEFAULT 2 _start No version information found in this file.
Monday, August 8, 2011
Assembly in Linux
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment