Skip to content

Instantly share code, notes, and snippets.

@kig
Created September 30, 2008 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kig/13759 to your computer and use it in GitHub Desktop.
Save kig/13759 to your computer and use it in GitHub Desktop.
.equ EXIT, 1
.equ READ, 3
.equ OPEN, 5
.equ BRK, 45
.equ BUF_SZ, 4096
.section .data
filename:
.ascii "my_file\0"
.section .bss
.lcomm buffer, BUF_SZ
.section .text
.globl _start
_start:
movq $buffer + BUF_SZ, %r12 # save bss end address to r12
movq $OPEN, %rax
movq $filename, %rbx
movq $0, %rcx
movq $0666, %rdx
int $0x80
movq %rax, %r13 # save fd to r13
read_loop:
movq $READ, %rax
movq %r13, %rbx
movq $buffer, %rcx
movq $BUF_SZ, %rdx
int $0x80
cmpq $0, %rax # end of file
je read_loop_end
movq %r12, %rbp # copy old end to rbp
addq %rax, %r12 # add read sz to r12
movq $BRK, %rax # allocate to new end
movq %r12, %rbx
int $0x80
cmpq %rax, %r12 # if rax != r12, alloc failed
jne error_exit
# copy buf to newly allocated space
# rcx has the buffer address
copy_loop:
cmpq %r12, %rbp
je end_copy
movb (%rbp), %al
movb %al, (%rbp)
incq %rcx
incq %rbp
jmp copy_loop
end_copy:
jmp read_loop
read_loop_end: # ok exit
movq $EXIT, %rax
movq $0, %rbx
int $0x80
error_exit: # error exit
movq $EXIT, %rax
movq $1, %rbx
int $0x80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment