;open a file and write a few strings into it
extern printf
section .text
global _start ;must be declared for linker (ld)
_start: ;tells linker entry point
;open the file for writing
mov eax, 5 ; sys_open
mov ebx, filename
mov ecx, 1 ; 1 for writing access; 0 for read, 2 for read&write
int 0x80
mov [fd], ax
push eax
push format
call printf
pop ebx
pop ebx
mov ebx,0
mov bx, [fd] ; file descriptor
;seek the end of the file
mov eax, 19; sys_lseek
mov edx, 2 ; end of file, 0, start of file, 1 current position
mov ecx,0 ; offset
int 0x80
;write the messages
mov ecx, msg0
mov edx,msg1;
sub edx, msg0 ;calculate the length of msg0
mov eax, 4 ; sys_write
int 0x80
;write the next msg
mov ecx, msg1
mov edx,len;
mov eax, 4 ; sys_write
int 0x80
;close the file
mov eax, 6 ; sys_close
int 0x80
mov eax, 1
mov ebx ,0
int 0x80
section .data
msg0 db 'This is the first line',0xa;
msg1 db 'This is the second line',0xa
len equ $ - msg1 ;length of the string
filename db 'test_file.txt',0x0
format db 'This is is the file descriptor %d',0xa,0x0
section .bss
fd resw 1 ; the file descriptor