0% found this document useful (0 votes)
22 views3 pages

Read File - Asm

The document contains assembly code for a program that opens a file, retrieves its size, and reads its contents into allocated memory. It utilizes system calls for file operations and memory management, including opening, seeking, reading, and closing the file. Additionally, it prints the file size and memory break information during execution.

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)
22 views3 pages

Read File - Asm

The document contains assembly code for a program that opens a file, retrieves its size, and reads its contents into allocated memory. It utilizes system calls for file operations and memory management, including opening, seeking, reading, and closing the file. Additionally, it prints the file size and memory break information during execution.

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

;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 reading
mov eax, 5 ; sys_open
mov ebx, filename
mov ecx, 0 ; 1 for writing access; 0 for read, 2 for read&write
int 0x80
mov [fd], ax ; file descriptor

;--------------------------------------------------------
push eax
push format1
call printf
pop ebx
pop ebx
;--------------------------------------------------------
;seek the end of the file ;returns the filesize in eax
mov ebx,0
mov bx, [fd] ; file descriptor
mov eax, 19; sys_lseek
mov edx, 2 ; 0 start of file, 1 current position, 2 end of file
mov ecx,0 ; offset
int 0x80
mov [size],eax ; store the file size in size
;--------------------------------------------------------
;print the size of the file
push eax
push format1
call printf
pop ebx
pop ebx
;--------------------------------------------------------
;seek the beginning of the file ;
mov eax, 19; sys_lseek
mov ebx,0
mov bx, [fd] ;file descriptor
mov edx, 0 ; 0 start of file, 1 current position, 2 end of file
mov ecx,0 ; offset
int 0x80
;print the current location
push eax
push format1
call printf
pop ebx
pop ebx
;--------------------------------------------------------
;get the old memory break
mov eax, 45 ; sys_brk() for memory allocation
xor ebx, ebx
int 0x80 ;
mov [mem_brk], eax
push eax
push format1
call printf
pop ebx
pop ebx
;-------------
;create a new break
mov eax, 45 ; sys_brk() for memory allocation
mov ebx, [mem_brk] ; get current break
add ebx , dword [size] ; add number of bytes to allocate to current break
;add ebx , 12 ; add number of bytes to allocate to current break
int 0x80 ;

;-----------------
;show the new memory break
mov eax, 45 ; sys_brk() for memory allocation
xor ebx, 0
int 0x80 ;
push eax
push format1
call printf
pop ebx
pop ebx
;--------------
;read the file
mov edx, [size]; number of bytes to read
xor ebx, ebx
mov bx, [fd] ;file descriptor
mov ecx, [mem_brk] ; read string to be stored in the allocated space
mov eax, 3 ; sys_read
int 0x80
;--------------
mov eax, [mem_brk]
push eax
push format
call printf
pop ebx
pop ebx
;---------------------------------------------------
;free memory
mov ebx, [mem_brk] ; old memory break
mov eax, 45 ; sys_brk() for memory allocation
int 80h
;----------------
;--------------
;close the file
xor ebx,ebx
mov ebx, [fd]
mov eax, 6 ; sys_close
int 0x80

mov eax, 1
mov ebx ,0
int 0x80

section .data

filename db 'test_file.txt',0x0
format1 db 'size =%d',0xa,0x0
format db '%s',0xa,0x0
section .bss
fd resw 1 ; the file descriptor
size resd 1
mem_brk resd 1
section .bss

You might also like