Quick viewing(Text Mode)

NASM for Linux

NASM for Linux

1 NASM for Microprocessors II 2 NASM for Linux Microprocessors II NASM Package nasm package available as source or as Typically /usr/bin/nasm and /usr/bin/ndisasm Assembly NASM Linux requires elf format for object files ELF = and Linking Format Typical header size = 330h bytes for nasm −f elf [−o ] Linking Linux Object files can be linked with gcc gcc [−options] [other_files.o] Disassembly View executable as 32-bit assembly code ndisasm −e 330h –b 32 a.out | less objdump –d a.out | less

Fall 2007 Hadassah College Dr. Martin Land Fall 2007 Hadassah College Dr. Martin Land

3 NASM for Linux Microprocessors II 4 NASM for Linux Microprocessors II gcc Stages Example — 1 Stages of Gnu compilation factorial2.c #include main #include sets j = 12 main() Source Translation Assembly Object Executable calls factorial 10,000,000 times Code Unit Code Code File { int times; prog.c prog.i prog.s prog.o a.out int i , j = 12; preprocess compile assemble link for (times = 0 ; times < 10000000 ; ++times){ i = factorial(j); gcc -E } gcc -S printf("%d\n",i); gcc -c } gcc int factorial(n) int n; factorial calculates n! by recursion { if (n == 0) return 1; else return n * factorial(n-1); }

Fall 2007 Hadassah College Dr. Martin Land Fall 2007 Hadassah College Dr. Martin Land 5 NASM for Linux Microprocessors II 6 NASM for Linux Microprocessors II Example — 2 Example — 3

~/gcc$ gcc factorial2.c Compile program as separate files

produces executable a.out factorial2a.c ~/gcc$ time a.out main() { 479001600 int times; int i,j=12; for (times = 0 ; times < 10000000 ; ++times){ real 0m9.281s i = factorial(j); factorial2b.c } #include printf("%d\n",i); user 0m8.339s #include } sys 0m0.008s int factorial(n) int n; { Program a.out runs in 8.339 seconds on 300 MHz if (n == 0) Pentium II return 1; else return n * factorial(n-1); }

Fall 2007 Hadassah College Dr. Martin Land Fall 2007 Hadassah College Dr. Martin Land

7 NASM for Linux Microprocessors II 8 NASM for Linux Microprocessors II Example — 4 Example — 5 Assembly output (AT&T gas assembly) from gcc –S factorial2a.c ~/gcc$ gcc -c factorial2a.c .file "factorial2a.c" .L3: .section .rodata subl $12, %esp produces linkable object file factorial2a.o .LC0: pushl -4(%ebp) .string "%d\n" call factorial ~/gcc$ gcc -c factorial2b.c .text addl $16, %esp .globl main movl %eax, -8(%ebp) produces linkable object file .type main, @function leal -12(%ebp), %eax factorial2b.o main: incl (%eax) pushl %ebp .L2: movl %esp, %ebp cmpl $9999999, -12(%ebp) subl $24, %esp jle .L3 ~/gcc$ gcc factorial2a.o factorial2b.o andl $-16, %esp subl $8, %esp movl $0, %eax pushl -8(%ebp) addl $15, %eax pushl $.LC0 produces executable a.out addl $15, %eax call printf shrl $4, %eax addl $16, %esp Identical to previous version sall $4, %eax leave subl %eax, %esp ret movl $12, -4(%ebp) movl $0, -12(%ebp) jmp .L2 Utility program intel2gas can convert: gas to nasm (Intel) nasm to gas nasm to gas inline assembler for C programs

Fall 2007 Hadassah College Dr. Martin Land Fall 2007 Hadassah College Dr. Martin Land 9 NASM for Linux Microprocessors II 10 NASM for Linux Microprocessors II Output from intel2gas Example — 6 ;FILE "factorial2a.c" L3: Assembly version of factorial function written for nasm SECTION .rodata sub esp,12 .LC0: push dword [ebp-4] Uses “register variables” (no memory accesses) db '%d',10,'' call factorial Exploits advantages of Intel imul and loop instructions SECTION .text add esp,16 GLOBAL main mov [ebp-8],eax section .text2 GLOBAL main:function lea eax, [ebp-12] global factorial main: inc dword [eax] push ebp L2: factorial: mov ebp,esp cmp dword [ebp-12],9999999 push ebp sub esp,24 jle L3 and esp,-16 sub esp,8 mov ebp,esp mov eax,0 push dword [ebp-8] mov ecx,[ebp+8] add eax,15 push dword .LC0 add eax,15 call printf mov eax,1 shr eax,4 add esp,16 L1: imul ecx sal eax,4 leave loop L1 sub esp,eax ret mov dword [ebp-4],12 mov esp,ebp mov dword [ebp-12],0 pop ebp jmp L2 ret

Fall 2007 Hadassah College Dr. Martin Land Fall 2007 Hadassah College Dr. Martin Land

11 NASM for Linux Microprocessors II 12 NASM for Linux Microprocessors II Example — 7 General NASM Template for Linux nasm -f elf -o factorial2c.o factorial2c.asm ; extern standard_library_function produces linkable object file factorial2c.o section .data ; define initialized data structures here gcc factorial2a.o factorial2c.o section .bss produces executable file a.out ; define uninitialized data structures here ~/gcc$ time a.out ; minimum allocation size is 1 dword = 32 bits 479001600 ; cannot pass BSS pointers to all system calls real 0m4.964s section .text user 0m4.287s global main ; or other line label sys 0m0.009s main: ; matching line label ; place code here ; C only version of a.out runs in 8.339 seconds on 300 MHz Pentium II mov eax, 0 ; or other exit code C + assembly version runs in 4.287 seconds on 300 MHz Pentium II ret ; return to program wrapper Speed-up of 8.339 / 4.287 ~ 2

Fall 2007 Hadassah College Dr. Martin Land Fall 2007 Hadassah College Dr. Martin Land 13 NASM for Linux Microprocessors II 14 NASM for Linux Microprocessors II Linux System Calls Exit NASM programs can invoke exit (terminate ) Any standard C library call EAX ← 1 Declare external library functions before EBX ← exit code Push parameters onto stack in proper order INT 0x80 Call function by name Clean up stack after return Works like DOS version Any standard Linux system call MOV AH,4C Similar to DOS system calls Load parameters into EAX, EBX, ECX, EDX MOV AL,exit code INT 0x21 Call Linux kernel with INT 0x80 References: http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html http://www.lxhp.in-berlin.de/

Fall 2007 Hadassah College Dr. Martin Land Fall 2007 Hadassah College Dr. Martin Land

15 NASM for Linux Microprocessors II 16 NASM for Linux Microprocessors II Create File Open File creat open EAX ← 8 EAX ← 5 EBX ← pointer to ASCIIZ pathname EBX ← pointer to ASCIIZ pathname ECX ← file permissions ECX ← file access mode INT 0x80 0x00 = read only 0x01 = write only return 0x02 = read/write EAX ← integer file descriptor EDX ← file permissions INT 0x80

return EAX ← integer file descriptor

Fall 2007 Hadassah College Dr. Martin Land Fall 2007 Hadassah College Dr. Martin Land 17 NASM for Linux Microprocessors II 18 NASM for Linux Microprocessors II Write to File Read from File write read EAX ← 4 EAX ← 3 EBX ← file descriptor EBX ← file descriptor ECX ← pointer to output buffer ECX ← pointer to input buffer EDX ← number of bytes to write EDX ← number of bytes to read INT 0x80 INT 0x80 return return EAX ← number of bytes actually written EAX ← number of bytes actually read

Note Note Write to screen using stdout descriptor = 1 Read from keyboard using stdin descriptor = 0

Fall 2007 Hadassah College Dr. Martin Land Fall 2007 Hadassah College Dr. Martin Land

19 NASM for Linux Microprocessors II 20 NASM for Linux Microprocessors II Close File Program Example — 1 close section .data path: db "filename.txt",0 EAX ← 6 ; ASCIIZ pathname EBX ← file descriptor str1: db 'abcdefghijklmnopqrstuvwzyz',10,10 INT 0x80 ; 10 = "\n" len1 equ $-str1 ; len1 ← length of str1 str2: db 'ABCDEFGHIJKLMNOPQRSTUVWZYZ',10,10 len2 equ $-str2 ; len2 ← length of str2 buff: times 256 db 0 ; 256 zeros as buffer

section .bss desc: resd 1 ; d = dword = 32-bit integer buff2: resd 1 ; minimum BSS allocation is dword ;buff: resb 256 ; cannot pass BSS pointer to ; read system call

Fall 2007 Hadassah College Dr. Martin Land Fall 2007 Hadassah College Dr. Martin Land 21 NASM for Linux Microprocessors II 22 NASM for Linux Microprocessors II Program Example — 2 Program Example — 3

section .text get: mov eax,3 ; read from call global main mov ebx,0 ; file descriptor for stdin main: mov ecx,buff ; pointer to input buffer (in data segment) create: mov eax,8 ; create file system call mov edx,256 ; accept up to 265 bytes from stdin mov ebx,path ; pointer to pathname int 0x80 ; invoke Linux kernel mov ecx,0 ; no access restrictions ; int 0x80 ; invoke Linux kernel write2: mov esi,buff ; point ESI at input buffer from stdin mov [desc],eax ; save file descriptor ; w2: lodsb ; AL ← [ESI] , ESI ← ESI + 1 write: mov eax,4 ; write to file system call and eax,0x000000ff ; zero EAX except AL mov ebx,[desc] ; file descriptor cmp al,0 ; if AL = 0 then stop writing mov ecx,str1 ; pointer to string 1 je close mov edx,len1 ; number of bytes to write mov [buff2],eax ; move EAX to memory at buff2 (BSS) int 0x80 ; invoke Linux kernel mov eax,4 ; write to file system call ; mov ebx,[desc] ; file descriptor mov eax,4 ; write to file system call mov ecx,buff2 ; pointer to buffer mov ebx,[desc] ; file descriptor mov edx,1 ; number of bytes to write mov ecx,str2 ; pointer to string 2 int 0x80 ; invoke Linux kernel mov edx,len2 ; number of bytes to write jmp w2 ; continue int 0x80 ; invoke Linux kernel

Fall 2007 Hadassah College Dr. Martin Land Fall 2007 Hadassah College Dr. Martin Land

23 NASM for Linux Microprocessors II 24 NASM for Linux Microprocessors II Program Example — 4 Program Example — 5 close: mov eax,6 ~/nasm/programs_linux$ nasm −f elf create.asm mov ebx,[desc] ~/nasm/programs_linux$ gcc create.o int 0x80 ~/nasm/programs_linux$ a.out ; I am writing this sentence. exit: mov eax,1 ~/nasm/programs_linux$ cat filename.txt mov ebx,0 abcdefghijklmnopqrstuvwzyz int 0x80 ABCDEFGHIJKLMNOPQRSTUVWZYZ

I am writing this sentence. ~/nasm/programs_linux$

Fall 2007 Hadassah College Dr. Martin Land Fall 2007 Hadassah College Dr. Martin Land 25 NASM for Linux Microprocessors II 26 NASM for Linux Microprocessors II Program Example — 6 Using C Functions

extern printf ~/nasm/programs_linux$ ndisasm –e 330h –b 32 a.out section .data a: dd 5 00000000 B808000000 mov eax,0x8 0000005A BEE5950408 mov esi,0x80495e5 00000005 BBA0950408 mov ebx,0x80495a0 0000005F AC lodsb fmt: db "a=%d, eax=%d", 10, 0 ; printf format string 0000000A B900000000 mov ecx,0x0 00000060 25FF000000 and eax,0xff ; printf("a=%d, eax=%d\n", a, a+2) 0000000F CD80 int 0x80 00000065 3C00 cmp al,0x0 00000011 A3EC960408 mov [0x80496ec],eax 00000067 741E jz 0x87 section .text 00000016 B804000000 mov eax,0x4 00000069 A3F0960408 mov [0x80496f0],eax 0000001B 8B1DEC960408 mov ebx,[0x80496ec] 0000006E B804000000 mov eax,0x4 global main 00000021 B9AD950408 mov ecx,0x80495ad 00000073 8B1DEC960408 mov ebx,[0x80496ec] main: mov eax, [a] ; EAX ← value of a 00000026 BA1C000000 mov edx,0x1c 00000079 B9F0960408 mov ecx,0x80496f0 0000002B CD80 int 0x80 0000007E BA01000000 mov edx,0x1 add eax, 2 ; EAX ← EAX + 2 0000002D B804000000 mov eax,0x4 00000083 CD80 int 0x80 00000032 8B1DEC960408 mov ebx,[0x80496ec] 00000085 EBD8 jmp short 0x5f push eax ; value of a + 2 00000038 B9C9950408 mov ecx,0x80495c9 00000087 B806000000 mov eax,0x6 push dword [a] ; value of a 0000003D BA1C000000 mov edx,0x1c 0000008C 8B1DEC960408 mov ebx,[0x80496ec] 00000042 CD80 int 0x80 00000092 CD80 int 0x80 push dword fmt ; pointer to format string 00000044 B803000000 mov eax,0x3 00000094 B801000000 mov eax,0x1 call printf ; call C library function 00000049 BB00000000 mov ebx,0x0 00000099 BB00000000 mov ebx,0x0 0000004E B9E5950408 mov ecx,0x80495e5 0000009E CD80 int 0x80 add esp, 12 ; clean up stack 00000053 BA00010000 mov edx,0x100 00000058 CD80 int 0x80 ; (3 pushes of 4 bytes) mov eax,0 ; exit code ret ; return

Fall 2007 Hadassah College Dr. Martin Land Fall 2007 Hadassah College Dr. Martin Land

27 NASM for Linux Microprocessors II 28 NASM for Linux Microprocessors II Assembly and Linking Another printf Example ~/nasm/programs_linux$ nasm −f elf printf1.asm extern printf ~/nasm/programs_linux$ gcc printf1.o section .data msg: db "Hello world: %c %s of length %d %d %X",10,0 ~/nasm/programs_linux$ a.out char1: db 'a' ; character a a=5, eax=7 str1: db "string",0 ; ASCIIZ string ~/nasm/programs_linux$ len: equ $-str1 ; len = length of str1 inta1: dd 1234567 ; integer 1234567 hex1: dd 0x6789ABCD ; hex constant section .text global main main: push dword [hex1] ; %X - hex constant push dword [inta1] ; %d - integer data push dword len ; %d – constant (equate) push dword str1 ; %s – pointer to "string" push dword [char1] ; %c – the character 'a' push dword msg ; pointer to format string call printf ; call C library function add esp, 24 ; pop stack 6*4 = 24 bytes mov eax, 0 ; exit code ret

Fall 2007 Hadassah College Dr. Martin Land Fall 2007 Hadassah College Dr. Martin Land 29 NASM for Linux Microprocessors II 30 NASM for Linux Microprocessors II Assembly and Linking Getting Smaller Programs ~/nasm/programs_linux$ nasm −f elf printf2.asm Reference: ~/nasm/programs_linux$ gcc printf2.o http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html ~/nasm/programs_linux$ a.out Hello world: a string of length 7 1234567 6789ABCD Small C program — c1.c ~/nasm/programs_linux$ int main(void) { return 42; }

~/nasm/programs_linux$ gcc c1.c ~/nasm/programs_linux$ a.out ; echo $? 42 ~/nasm/programs_linux$ wc –c a.out 6502 a.out

Fall 2007 Hadassah College Dr. Martin Land Fall 2007 Hadassah College Dr. Martin Land

31 NASM for Linux Microprocessors II 32 NASM for Linux Microprocessors II Why So Big? Same Program in Assembly Program file contains 14 code sections ; c2.asm <_init>: 23 bytes GLOBAL main <__libc_start_main@plt-0x10>: 14 bytes SECTION .text <__libc_start_main@plt>: 16 bytes main: <__gmon_start__@plt>: 16 bytes mov eax, 42 <_start>: 36 bytes ret : 44 bytes <__do_global_dtors_aux>: 48 bytes ~/nasm/programs_linux$ nasm −f elf c2.asm : 36 bytes ~/nasm/programs_linux$ gcc c2.o

: 28 bytes ~/nasm/programs_linux$ a.out ; echo $? <__libc_csu_fini>: 16 bytes 42 <__libc_csu_init>: 105 bytes ~/nasm/programs_linux$ wc –c a.out <__i686.get_pc_thunk.bx>: 7 bytes 6472 a.out <__do_global_ctors_aux>: 40 bytes <_fini>: 28 bytes Most of the volume is standard Linux startup routines Remaining volume is data strings

Fall 2007 Hadassah College Dr. Martin Land Fall 2007 Hadassah College Dr. Martin Land 33 NASM for Linux Microprocessors II 34 NASM for Linux Microprocessors II Removing Startup Files Removing Library Files ; c3.asm ; c4.asm EXTERN _exit ; standard library function GLOBAL _start ; default EIP at start GLOBAL _start ; default EIP at start ; does not link to C library ; does not link to C library SECTION .text SECTION .text _start: _start: mov eax,1 ; Linux exit code push dword 42 mov ebx,42 ; exit code call _exit int 0x80 ; call Linux ~/nasm/programs_linux$ nasm −f elf c3.asm ~/nasm/programs_linux$ nasm −f elf c4.asm ~/nasm/programs_linux$ gcc –nostartfiles c3.o ~/nasm/programs_linux$ gcc –nostdlib c4.o ~/nasm/programs_linux$ a.out ; echo $? ~/nasm/programs_linux$ a.out ; echo $? 42 42 ~/nasm/programs_linux$ wc –c a.out ~/nasm/programs_linux$ wc –c a.out 1859 a.out 671 a.out gcc option –nostartfiles prevents linking C start-up routines gcc option –nostdlib prevents linking Linux library routines

Fall 2007 Hadassah College Dr. Martin Land Fall 2007 Hadassah College Dr. Martin Land

35 NASM for Linux Microprocessors II 36 NASM for Linux Microprocessors II Program Header Program Code

0000000: 7f45 4c46 0101 0100 0000 0000 0000 0000 .ELF...... 0000080: bb2a 0000 00b8 0100 0000 cd80 0000010: 0200 0300 0100 0000 8080 0408 3400 0000 ...... 4... 0000020: d800 0000 0000 0000 3400 2000 0100 2800 ...... 4. ...(. 0000030: 0600 0300 0100 0000 0000 0000 0080 0408 ...... 0000080: bb2a000000 mov ebx, 0x0000002a 0000040: 0080 0408 8c00 0000 8c00 0000 0500 0000 ...... 0000085: b801000000 mov eax, 0x00000001 0000050: 0010 0000 0000 0000 0000 0000 0000 0000 ...... 000008A: cd80 int 0x80 0000060: 0000 0000 0000 0000 0000 0000 0000 0000 ...... 0000070: 0000 0000 0000 0000 0000 0000 0000 0000 ......

Fall 2007 Hadassah College Dr. Martin Land Fall 2007 Hadassah College Dr. Martin Land 37 NASM for Linux Microprocessors II 38 NASM for Linux Microprocessors II Program Tail Function Example — 1

0000080: 0054 6865 .*...... The 0000090: 204e 6574 7769 6465 2041 7373 656d 626c Netwide Assembl extern disp 00000a0: 6572 2030 2e39 382e 3338 0000 2e73 796d er 0.98.38...sym 00000b0: 7461 6200 2e73 7472 7461 6200 2e73 6873 tab..strtab..shs 00000c0: 7472 7461 6200 2e74 6578 7400 2e63 6f6d trtab..text..com section .data 00000d0: 6d65 6e74 0000 0000 0000 0000 0000 0000 ment...... 00000e0: 0000 0000 0000 0000 0000 0000 0000 0000 ...... str1: db 'abcdefghijklmnopqrstuvwzyz',10,10 00000f0: 0000 0000 0000 0000 0000 0000 0000 0000 ...... 0000100: 1b00 0000 0100 0000 0600 0000 8080 0408 ...... 0000110: 8000 0000 0c00 0000 0000 0000 0000 0000 ...... 0000120: 1000 0000 0000 0000 2100 0000 0100 0000 ...... !...... 0000130: 0000 0000 0000 0000 8c00 0000 1f00 0000 ...... section .text 0000140: 0000 0000 0000 0000 0100 0000 0000 0000 ...... 0000150: 1100 0000 0300 0000 0000 0000 0000 0000 ...... 0000160: ab00 0000 2a00 0000 0000 0000 0000 0000 ....*...... global main 0000170: 0100 0000 0000 0000 0100 0000 0200 0000 ...... 0000180: 0000 0000 0000 0000 c801 0000 b000 0000 ...... main: 0000190: 0500 0000 0700 0000 0400 0000 1000 0000 ...... 00001a0: 0900 0000 0300 0000 0000 0000 0000 0000 ...... 00001b0: 7802 0000 2700 0000 0000 0000 0000 0000 x...'...... push str1-1 00001c0: 0100 0000 0000 0000 0000 0000 0000 0000 ...... 00001d0: 0000 0000 0000 0000 0000 0000 8080 0408 ...... call disp 00001e0: 0000 0000 0300 0100 0000 0000 0000 0000 ...... 00001f0: 0000 0000 0300 0200 0000 0000 0000 0000 ...... 0000200: 0000 0000 0300 0300 0000 0000 0000 0000 ...... 0000210: 0000 0000 0300 0400 0000 0000 0000 0000 ...... 0000220: 0000 0000 0300 0500 0100 0000 0000 0000 ...... exit: mov eax,1 0000230: 0000 0000 0400 f1ff 0800 0000 8080 0408 ...... 0000240: 0000 0000 1000 0100 0f00 0000 8c90 0408 ...... 0000250: 0000 0000 1000 f1ff 1b00 0000 8c90 0408 ...... mov ebx,0 0000260: 0000 0000 1000 f1ff 2200 0000 8c90 0408 ...... "...... 0000270: 0000 0000 1000 f1ff 0063 342e 6173 6d00 ...... c4.asm. int 0x80 0000280: 5f73 7461 7274 005f 5f62 7373 5f73 7461 _start.__bss_sta 0000290: 7274 005f 6564 6174 6100 5f65 6e64 000d rt._edata._end.. 00002a0: 0a .

Fall 2007 Hadassah College Dr. Martin Land Fall 2007 Hadassah College Dr. Martin Land

39 NASM for Linux Microprocessors II 40 NASM for Linux Microprocessors II Function Example — 2 Function Example — 3

section .text 08048350:

6867950408 push 0x8049567 global disp 08048355 E816000000 call near +0x16 (0x8048370:disp) disp: 0804835A: B801000000 mov eax, 0x1 push ebp ; esp = parameter + 8 (4 for eip, 4 for ebp) 0804835F BB00000000 mov ebx, 0x0 push edi ; esp = parameter + 12 08048364 CD80 int 0x80 push esi ; esp = parameter + 16 ... 08048370: 55 push ebp mov ebp,esp 08048371 57 push edi mov edi,[ebp+0x10] ; esi <-- pointer to parameter on stack 08048372 56 push esi L1: inc edi 08048373 89E5 mov ebp, esp mov eax,4 08048375 8B7D10 mov edi, dword [ebp+0x10] mov ebx,1 08048378: 47 inc edi mov ecx,edi 08048379 B804000000 mov eax, 0x4 mov edx,1 0804837E BB01000000 mov ebx, 0x1 int 0x80 08048383 89F9 mov ecx, edi cmp byte [edi],10 08048385 BA01000000 mov edx, 0x1 jne L1 0804838A CD80 int 0x80 clean_up: 0804838C 803F0A cmp byte [edi], 0xa mov esp,ebp ; restore esp to value from after push esi 0804838F 75E7 jne +0xe7 (0x8048478) pop esi 08048391: 89EC mov esp, ebp pop edi 08048393 5E pop esi 08048394 5F pop edi pop ebp 08048395 5D pop ebp ret 08048396 C3 retn

Fall 2007 Hadassah College Dr. Martin Land Fall 2007 Hadassah College Dr. Martin Land 41 NASM for Linux Microprocessors II 42 NASM for Linux Microprocessors II Forbidden Access — 1 Forbidden Access — 2

extern disph extern disph section .data section .data str1: times 512 db 10 str1: times 512 db 10 section .text section .text global main global main main: mov eax,cr0 ; Segmentation fault (core dumped) main: sgdt [str1] mov [str1],eax push str1-1 mov eax,cr2 call disph mov [str1+4],eax mov ebx,[str1+2] mov eax,cr3 mov [str1+8],eax mov eax,[ebx] ; Segmentation fault (core dumped) mov eax,cr4 mov [str1],eax mov [str1+12],eax push str1-1 push str1-1 call disph call disph

exit: mov eax,1 exit: mov eax,1 martin@martin:~/nasm/programs_linux/teaching_examples$ ex1 mov ebx,0 martin@martin:~/nasm/programs_linux/teaching_examples$ ex2 mov ebx,0 FF 00 00 80 4F C1 int 0x80 Segmentation fault (core dumped) Segmentation fault (core dumped) martin@martin:~/nasm/programs_linux/teaching_examples$ int 0x80 martin@martin:~/nasm/programs_linux/teaching_examples$

Fall 2007 Hadassah College Dr. Martin Land Fall 2007 Hadassah College Dr. Martin Land

43 NASM for Linux Microprocessors II 44 NASM for Linux Microprocessors II Forbidden Access — 3 Forbidden Access — 4

extern disph section .data section .data str1: times 512 db 10 str1: times 512 db 10

section .text section .text global main main: global main L1: mov ax,ds ; 66 8C D8 main: mov ds,ax ; 8E D8 mov ax,gs L1: mov eax,[0] ; Segmentation fault (core dumped) mov ds,ax mov ax,es exit: mov eax,1 mov ds,ax mov eax,[cs:L1] mov ebx,0 mov [str1],eax int 0x80 push str1-1 call disph mov [cs:L1],eax ; Segmentation fault (core dumped) exit: mov eax,1 martin@martin:~/nasm/programs_linux/teaching_examples$ ex3 martin@martin:~/nasm/programs_linux/teaching_examples$ ex4 mov ebx,0 66 8C D8 8E Segmentation fault (core dumped) int 0x80 Segmentation fault (core dumped) martin@martin:~/nasm/programs_linux/teaching_examples$ martin@martin:~/nasm/programs_linux/teaching_examples$

Fall 2007 Hadassah College Dr. Martin Land Fall 2007 Hadassah College Dr. Martin Land