# Read records previously written in file.dat, by write-records software .include "linux.s" .include "record-def.s" .section .data filename: .ascii "test.dat\0" .section .bss .lcomm record_buffer, RECORD_SIZE .section .text .globl _start _start: # Stack locations for INPUT and OUTPUT FDs .equ ST_INPUT_DESCRIPTOR, -8 .equ ST_OUTPUT_DESCRIPTOR, -16 movq %rsp, %rbp subq $16, %rsp # Save space in the stack for FDs # Open data file movq $SYS_OPEN, %rax movq $filename, %rdi movq $0, %rsi # Open for read only movq $0666, %rdx syscall # Save FD movq %rax, ST_INPUT_DESCRIPTOR(%rbp) # Yes, STDOUT is always 1, but if I want to change the output location # later, I don't need to change everywhere... movq $STDOUT, ST_OUTPUT_DESCRIPTOR(%rbp) record_read_loop: pushq ST_INPUT_DESCRIPTOR(%rbp) pushq $record_buffer call read_record addq $16, %rsp # Cleanup stack # All records are RECORD_SIZE size, if we didn't get this amount of # bytes from read function, we either are at EOF or we hit an error. cmpq $RECORD_SIZE, %rax jne finished_reading # We are ok, so print the first name in the record pushq $RECORD_FIRSTNAME + record_buffer # Location of firstname # record in the buffer # we just read call count_chars addq $8, %rsp # Cleanup stack # Write name to OUTPUT movq %rax, %rdx # Returned record size, used as argument to # write() movq $RECORD_FIRSTNAME + record_buffer, %rsi movq ST_OUTPUT_DESCRIPTOR(%rbp), %rdi movq $SYS_WRITE, %rax syscall pushq ST_OUTPUT_DESCRIPTOR(%rbp) call write_newline addq $8, %rsp jmp record_read_loop finished_reading: movq $SYS_EXIT, %rax movq $0, %rdi syscall