0% found this document useful (0 votes)
23 views1 page

Write File - Asm

The document is an assembly code snippet that demonstrates how to open a file, write two strings into it, and then close the file. It uses system calls for file operations and includes definitions for the strings and file name. The code also prints the file descriptor to the console using the printf function.

Uploaded by

megacharan52
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views1 page

Write File - Asm

The document is an assembly code snippet that demonstrates how to open a file, write two strings into it, and then close the file. It uses system calls for file operations and includes definitions for the strings and file name. The code also prints the file descriptor to the console using the printf function.

Uploaded by

megacharan52
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

;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

You might also like