# Modify records in a file and write them to the output file .include "linux.s" .include "record-def.s" .section .data input_filename: .ascii "test.dat\0" output_filename: .ascii "modified.dat\0" .section .bss .lcomm record_buffer, RECORD_SIZE .section .text .globl _start _start: # Stack local vars .equ ST_INPUT_DESCRIPTOR, -8 .equ ST_OUTPUT_DESCRIPTOR, -16 # Copy stack pointer and make room for local variables movq %rsp, %rbp subq $16, %rsp # Open file for reading movq $SYS_OPEN, %rax movq $input_filename, %rdi movq $0, %rsi movq $0666, %rdx syscall cmpq $0, %rax jl continue_processing # We've got an error... continue_processing: movq %rax, ST_INPUT_DESCRIPTOR(%rbp) .section .data no_open_file_code: .ascii "0001: \0" no_open_file_msg: .ascii "Can't open input file\0" .section .text pushq $no_open_file_msg pushq $no_open_file_code call error_exit # Open file for writing movq $SYS_OPEN, %rax movq $output_filename, %rdi movq $0101, %rsi movq $0666, %rdx syscall movq %rax, ST_OUTPUT_DESCRIPTOR(%rbp) loop_begin: pushq ST_INPUT_DESCRIPTOR(%rbp) pushq $record_buffer call read_record addq $16, %rsp # Return number of bytes read. If it's not the same as # $RECORD_SIZE, then we are either at EOF or we hit an error. cmpq $RECORD_SIZE, %rax jne loop_end # Increment age... incq record_buffer + RECORD_AGE # Write record to the output file pushq ST_OUTPUT_DESCRIPTOR(%rbp) pushq $record_buffer call write_record addq $16, %rsp jmp loop_begin loop_end: movq $SYS_EXIT, %rax movq $0, %rbx syscall