//----------------------------------------------------------------- // filesize.s // // This program shows how to use the Linux kernel's 'lseek' // system-call to find out how many bytes belong to a file. // // to assemble: $ as filesize.s -o filesize.o // and to link: $ ld filesize.o base10io.o -o filesize // and execute: $ ./filesize // // programmer: ALLAN CRUSE // written on: 28 MAR 2007 //----------------------------------------------------------------- # manifest constants .equ sys_LSEEK, 19 .equ sys_OPEN, 5 .equ sys_WRITE, 4 .equ sys_EXIT, 1 .equ SEEK_END, 2 .equ O_RDONLY, 00 .equ STDOUT, 1 .section .data handle: .int -1 fsize: .int 0 msg: .ascii " filesize (in bytes): " buf: .ascii " \n" len: .int . - msg .section .text _start: # check for the presence of a command-line argument cmpl $1, (%esp) je exit # get the number of characters in the argument-string mov 8(%esp), %ecx xor %edx, %edx again: cmpb $0, (%ecx, %edx) jz finis inc %edx jmp again finis: # display the command-line argument-string mov $sys_WRITE, %eax mov $STDOUT, %ebx int $0x80 # try to open the file named by the argument-string mov $sys_OPEN, %eax mov 8(%esp), %ebx mov $O_RDONLY, %ecx int $0x80 mov %eax, handle cmpl $0, handle jl exit # move the file-pointer to the end of the file mov %eax, handle mov $sys_LSEEK, %eax mov handle, %ebx xor %ecx, %ecx mov $SEEK_END, %edx int $0x80 mov %eax, fsize # format the file-pointer's offset for display mov fsize, %eax lea buf, %edi call eax2asc # display the size of the file (in bytes) mov $sys_WRITE, %eax mov $STDOUT, %ebx lea msg, %ecx mov len, %edx int $0x80 # terminate this program exit: mov $sys_EXIT, %eax xor %ebx, %ebx int $0x80 .global _start .end