Skip to content

Instantly share code, notes, and snippets.

@marlun
Last active December 19, 2015 07:59
Show Gist options
  • Save marlun/5922858 to your computer and use it in GitHub Desktop.
Save marlun/5922858 to your computer and use it in GitHub Desktop.
.include "linux.s"
.include "record-def.s"
.section .data
file_name:
.ascii "test.dat\0"
.section .bss
.lcomm record_buffer, RECORD_SIZE
#Stack offsets of local variables
.equ ST_FILE_DESCRIPTOR, -4
.section .text
.globl _start
_start:
#Copy stack pointer and make room for local variables
movl %esp, %ebp
subl $4, %esp
# Open file for writing and reading
movl $SYS_OPEN, %eax
movl $file_name, %ebx
movl $2, %ecx
movl $0666, %edx
int $LINUX_SYSCALL
movl %eax, ST_FILE_DESCRIPTOR(%ebp)
loop_begin:
pushl ST_FILE_DESCRIPTOR(%ebp)
pushl $record_buffer
call read_record
addl $8, %esp
#Returns the number of bytes read.
#If it isn't the same number we
#requested, then it's either an
#end-of-file, or an error, so we're
#quitting
cmpl $RECORD_SIZE, %eax
jne loop_end
#Increment the age
incl record_buffer + RECORD_AGE
# Reposition the offset of the open file
movl $SYS_LSEEK, %eax
movl ST_FILE_DESCRIPTOR(%ebp), %ebx
movl $RECORD_AGE, %ecx
movl $0, %edx
int $LINUX_SYSCALL
# TODO...Not sure how to write the year now.
movl $SYS_WRITE, %eax
movl ST_FILE_DESCRIPTOR(%ebp), %ebx
movl record_buffer + RECORD_AGE, %ecx
movl $4, %edx
int $LINUX_SYSCALL
jmp loop_begin
loop_end:
movl $SYS_EXIT, %eax
movl $0, %ebx
int $LINUX_SYSCALL
#Common Linux Definitions
#System Call Numbers
.equ SYS_EXIT, 1
.equ SYS_READ, 3
.equ SYS_WRITE, 4
.equ SYS_OPEN, 5
.equ SYS_CLOSE, 6
.equ SYS_BRK, 45
.equ SYS_LSEEK, 19
#System Call Interrupt Number
.equ LINUX_SYSCALL, 0x80
#Standard File Descriptors
.equ STDIN, 0
.equ STDOUT, 1
.equ STDERR, 2
#Common Status Codes
.equ END_OF_FILE, 0
.equ RECORD_FIRSTNAME, 0
.equ RECORD_LASTNAME, 40
.equ RECORD_ADDRESS, 80
.equ RECORD_NICK, 320
.equ RECORD_AGE, 360
.equ RECORD_SIZE, 364
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment