Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %define O_RDONLY 0
- %define STDOUT 1
- struc stats
- .dev resd 1
- .ino resd 1
- .mode resw 1
- .nlink resw 1
- .uid resw 1
- .gid resw 1
- .rdev resd 1
- .size resd 1
- .blksize resd 1
- .blocks resd 1
- .atime resd 1
- .atime_nsec resd 1
- .mtime resd 1
- .mtime_nsec resd 1
- .ctime resd 1
- .ctime_nsec resd 1
- .unused4 resd 1
- .unused5 resd 1
- endstruc
- section .data
- fname: db "file.txt",0x00
- fname_len: equ $-fname
- msg: db "Contents:",0x0a,0x00
- msg_len: equ $-msg
- section .bss
- stat: resb stats_size ;// adding _size is essentially the sizeof() 'equivalent'
- orig_brk: resd 1 ;// original brk value
- tmp: resd 1 ;// temp buffer if I need it
- section .text
- global _start
- _start:
- xor eax, eax
- mov al, 106 ;// Syscall for STAT
- mov ebx, fname
- mov ecx, stat
- int 80h
- ;// the stat struct is now populated. stat.size contains the file size...
- xor eax, eax
- xor ebx, ebx
- mov al, 45 ;// Syscall for BRK
- int 80h ;// EAX has BRK value
- mov [orig_brk], eax
- mov ebx, eax
- add ebx, [stat + stats.size] ;// Add the file size to the BRK value and re-BRK
- xor eax, eax
- mov al, 45 ;// Syscall for BRK
- int 80h ;// Extends BRK by FILE SIZE
- xor eax, eax
- mov al, 5 ;// Syscall for OPEN
- mov ebx, fname
- mov cl, O_RDONLY
- int 80h
- push eax ;// Saves the File Descriptor on the stack
- mov ebx, eax
- xor eax, eax
- mov al, 3 ;// Syscall for READ
- mov ecx, [orig_brk]
- ;xor edx, edx
- mov edx, [stat + stats.size]
- int 80h
- xor eax, eax
- mov al, 4 ;// Syscall for WRITE
- mov bl, STDOUT
- mov ecx, [orig_brk]
- ;xor edx, edx
- mov edx, [stat + stats.size]
- int 80h
- xor eax, eax
- mov al, 6 ;// Syscall for CLOSE
- pop ebx
- int 80h ;// Closes the FD
- xor eax, eax
- mov al, 1 ;// Syscall for EXIT
- xor ebx, ebx
- int 80h
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement