Modifying the Records


In this section, we will write a program that:

  • Opens an input and output file

  • Reads records from the input

  • Increments the age

  • Writes the new record to the output file

Like most programs we've encountered recently, this program is pretty straightforward.[3]

   .include "linux.s"   .include "record-def.s"  .section .data input_file_name:  .ascii "test.dat\0" output_file_name:  .ascii "testout.dat\0"  .section .bss  .lcomm record_buffer, RECORD_SIZE  #Stack offsets of local variables  .equ ST_INPUT_DESCRIPTOR, -4  .equ ST_OUTPUT_DESCRIPTOR, -8  .section .text  .globl _start _start:  #Copy stack pointer and make room for local variables  movl  %esp, %ebp  subl  $8, %esp  #Open file for reading  movl  $SYS_OPEN, %eax  movl  $input_file_name, %ebx  movl  $0, %ecx  movl  $0666, %edx  int   $LINUX_SYSCALL  movl  %eax, ST_INPUT_DESCRIPTOR(%ebp)  #Open file for writing  movl  $SYS_OPEN, %eax  movl  $output_file_name, %ebx  movl  $0101, %ecx  movl  $0666, %edx  int   $LINUX_SYSCALL  movl  %eax, ST_OUTPUT_DESCRIPTOR(%ebp) loop_begin:  pushl ST_INPUT_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  #Write the record out  pushl ST_OUTPUT_DESCRIPTOR(%ebp)  pushl $record_buffer  call  write_record  addl  $8, %esp  jmp   loop_begin loop_end:  movl  $SYS_EXIT, %eax  movl  $0, %ebx  int   $LINUX_SYSCALL 

You can type it in as add-year.s. To build it, type the following[4]:

 as add-year.s -o add-year.o ld add-year.o read-record.o write-record.o -o add-year 

To run the program, just type in the following[5]:

 ./add-year 

This will add a year to every record listed in test.dat and write the new records to the file testout.dat.

As you can see, writing fixed-length records is pretty simple. You only have to read in blocks of data to a buffer, process them, and write them back out. Unfortunately, this program doesn't write the new ages out to the screen so you can verify your program's effectiveness. This is because we won't get to displaying numbers until Chapter 8 and Chapter 10. After reading those you may want to come back and rewrite this program to display the numeric data that we are modifying.

[3]You will find that after learning the mechanics of programming, most programs are pretty straightforward once you know exactly what it is you want to do. Most of them initialize data, do some processing in a loop, and then clean everything up.

[4]This assumes that you have already built the object files read-record.o and write-record.o in the previous examples. If not, you will have to do so.

[5]This is assuming you created the file in a previous run of write-records. If not, you need to run write-records first before running this program.




Programming from the Ground Up
Programming from the Ground Up
ISBN: 0975283847
EAN: 2147483647
Year: 2006
Pages: 137

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net