//---------------------------------------------------------------- // readdemo.s // // This program illustrates the use of some Linux kernel // system-calls which permit the contents of a text file // (named 'mycookie.dat') to be read into a 'dynamically // allocated' buffer located in our program's heap area, // from which the data can then be sent to the terminal. // // NOTE: For an explanation of the scheme we use here to // do error-handling, refer to our previous demo-program // (named 'out2file.s'). EXERCISE: you supply comments. // // assemble using: $ as readdemo.s -o readdemo.o // and link using: $ ld readdemo.o -o readdemo // // programmer: ALLAN CRUSE // written on: 14 APR 2005 //---------------------------------------------------------------- # manifest constants .equ STDOUT, 1 .equ SEEK_SET, 0 .equ SEEK_END, 2 .equ O_RDONLY, 0 .equ sys_EXIT, 1 .equ sys_READ, 3 .equ sys_WRITE, 4 .equ sys_OPEN, 5 .equ sys_LSEEK, 19 .equ sys_BRK, 45 .data filnam: .asciz "mycookie.dat" handle: .int -1 filesz: .int -1 bufadr: .int -1 .text #----------------------------------------------------------------- _start: call open_file_ro call get_filesize call allocate_ram call read_in_file call display_info call exit_to_bash #----------------------------------------------------------------- open_file_ro: movl $sys_OPEN, %eax movl $filnam, %ebx movl $O_RDONLY, %ecx movl $0, %edx int $0x80 movl %eax, handle orl %eax, %eax jns openx pushl $mlen5 pushl $fail5 pushl $exitmsg openx: ret #----------------------------------------------------------------- fail5: .ascii "Could not open the file \n" .equ mlen5, . - fail5 #----------------------------------------------------------------- get_filesize: movl $sys_LSEEK, %eax movl handle, %ebx xorl %ecx, %ecx movl $SEEK_END, %edx int $0x80 movl %eax, filesz orl %eax, %eax js skerr movl $sys_LSEEK, %eax movl handle, %ebx xorl %ecx, %ecx movl $SEEK_SET, %edx int $0x80 orl %eax, %eax jns seekx skerr: pushl $mlen19 pushl $fail19 pushl $exitmsg seekx: ret #----------------------------------------------------------------- fail19: .ascii "Could not do lseek. \n" .equ mlen19, . - fail19 #----------------------------------------------------------------- allocate_ram: movl $sys_BRK, %eax xorl %ebx, %ebx # we want current 'brk' int $0x80 movl %eax, bufadr movl $sys_BRK, %eax movl bufadr, %ebx addl filesz, %ebx # here we set new 'brk' int $0x80 cmp $-1, %eax jne alocx pushl $mlen45 pushl $fail45 pushl $exitmsg alocx: ret #----------------------------------------------------------------- fail45: .ascii "Could not allocate memory. \n" .equ mlen45, . - fail45 #----------------------------------------------------------------- read_in_file: movl $sys_READ, %eax movl handle, %ebx movl bufadr, %ecx movl filesz, %edx int $0x80 orl %eax, %eax jns readx pushl $mlen3 pushl $fail3 pushl $exitmsg readx: ret #----------------------------------------------------------------- fail3: .ascii "Could not read from file. \n" .equ mlen3, . - fail3 #----------------------------------------------------------------- display_info: movl $sys_WRITE, %eax movl $STDOUT, %ebx movl bufadr, %ecx movl filesz, %edx int $0x80 ret #----------------------------------------------------------------- exitmsg: movl $sys_WRITE, %eax movl $STDOUT, %ebx movl $idmsg, %ecx movl $idlen, %edx int $0x80 movl $sys_WRITE, %eax movl $STDOUT, %ebx popl %ecx popl %edx int $0x80 exit_to_bash: movl $sys_EXIT, %eax movl $0, %ebx int $0x80 #----------------------------------------------------------------- idmsg: .ascii "readdemo: " .equ idlen, . - idmsg #----------------------------------------------------------------- .globl _start .end