; syscall1.asm demonstrate system, kernel, calls ; Compile: nasm -f elf syscall1.asm ; Link gcc -o syscall1 syscall1.o ; Run: syscall1 ; section .data msg: db "syscall1.asm running",10 ; the string to print, 10=crlf len: equ $-msg ; "$" means here, len is a value, not an address msg2: db "syscall1.asm finished",10 len2: equ $-msg2 msg3: db "syscall1.asm opened",10 len3: equ $-msg3 msg4: db "syscall1.asm read",10 len4: equ $-msg4 msg5: db "syscall1.asm open fail",10 len5: equ $-msg5 msg6: db "syscall1.asm another open fail",10 len6: equ $-msg6 msg7: db "syscall1.asm read fail",10 len7: equ $-msg7 name: db "syscall1.asm",0 ; "C" string fd: dd 0 ; file descriptor, int flags: dd 0 ; hopefully read-only section .bss line: resb 8193 ; read/write buffer 16 sectors of 512 lenbuf: resd 1 ; number of bytes read extern open global main section .text main: push ebp ; set up stack frame mov ebp,esp push ebx ; header msg mov edx,len ; arg3, length of string to print mov ecx,msg ; arg2, pointer to string mov ebx,1 ; arg1, where to write, screen mov eax,4 ; write command to int 80 hex int 0x80 ; interrupt 80 hex, call kernel open1: mov edx,0 ; mode mov ecx,0 ; flags, 'r' equivalent O_RDONLY mov ebx,name ; file name to open mov eax,5 ; open command to int 80 hex int 0x80 ; interrupt 80 hex, call kernel mov [fd],eax ; save fd cmp eax,2 ; test for fail jg read ; file open ; file open failed msg5 mov edx,len5 ; arg3, length of string to print mov ecx,msg5 ; arg2, pointer to string mov ebx,1 ; arg1, where to write, screen mov eax,4 ; write command to int 80 hex int 0x80 ; interrupt 80 hex, call kernel anotheropen: push dword [flags] push dword name call open ; "C" library call, not kernel call add esp,8 mov [fd],eax cmp eax,2 ; test for fail jg read ; file open ; another file open failed msg6 mov edx,len6 ; arg3, length of string to print mov ecx,msg6 ; arg2, pointer to string mov ebx,1 ; arg1, where to write, screen mov eax,4 ; write command to int 80 hex int 0x80 ; interrupt 80 hex, call kernel jmp fail ; file not open read: ; file opened msg3 mov edx,len3 ; arg3, length of string to print mov ecx,msg3 ; arg2, pointer to string mov ebx,1 ; arg1, where to write, screen mov eax,4 ; write command to int 80 hex int 0x80 ; interrupt 80 hex, call kernel doread: mov edx,8192 ; max to read mov ecx,line ; buffer mov ebx,[fd] ; fd mov eax,3 ; read command to int 80 hex int 0x80 ; interrupt 80 hex, call kernel mov [lenbuf],eax ; number of characters read cmp eax,0 ; test for fail jg readok ; some read ; read failed msg7 mov edx,len7 ; arg3, length of string to print mov ecx,msg7 ; arg2, pointer to string mov ebx,1 ; arg1, where to write, screen mov eax,4 ; write command to int 80 hex int 0x80 ; interrupt 80 hex, call kernel jmp fail ; nothing read ; file read msg4 readok: mov edx,len4 ; arg3, length of string to print mov ecx,msg4 ; arg2, pointer to string mov ebx,1 ; arg1, where to write, screen mov eax,4 ; write command to int 80 hex int 0x80 ; interrupt 80 hex, call kernel write: mov edx,[lenbuf] ; length of string to print mov ecx,line ; pointer to string mov ebx,1 ; where to write, screen mov eax,4 ; write command to int 80 hex int 0x80 ; interrupt 80 hex, call kernel fail: ; finished msg2 mov edx,len2 ; arg3, length of string to print mov ecx,msg2 ; arg2, pointer to string mov ebx,1 ; arg1, where to write, screen mov eax,4 ; write command to int 80 hex int 0x80 ; interrupt 80 hex, call kernel mov ebx,0 ; exit code, 0=normal mov eax,1 ; exit command to kernel int 0x80 ; interrupt 80 hex, call kernel