//---------------------------------------------------------------- // trymmap.s // // This program demonstrates use of the 'mmap' system-call. // It opens an existing textfile and obtains its size, then // requests the kernel to create a 'memory-mapping' of that // file into the program's virtual address-space, so it can // be dirctly accessed. The file contents are then written // to the standard output device, for viewing by a user. // // assemble using: $ as trymmap.s -o trymmap.o // and link using: $ ld trymmap.o -o trymmap // // programmer: ALLAN CRUSE // written on: 25 NOV 2003 //---------------------------------------------------------------- # symbolic constants .equ sys_EXIT, 1 # from .equ sys_WRITE, 4 # from .equ sys_OPEN, 5 # from .equ sys_LSEEK, 19 # from .equ sys_MMAP, 90 # from .equ sys_MUNMAP, 91 # from .equ O_RDONLY, 00 # from .equ SEEK_END, 2 # from .equ PROT_READ, 0x1 # from .equ MAP_PRIVATE, 02 # from .equ STDOUT_FILENO, 1 # from .section .data fname: .asciz "mycookie.dat" # filename mmaddr: .int -1 # mmap-arguments (6) mmargs: .int 0 # address fsize: .int 0 # filesize mmprot: .int PROT_READ # protection mmflag: .int MAP_PRIVATE # mmap flags handle: .int -1 # file descriptor fstart: .int 0 # region offset .section .text _start: # open the file for reading movl $sys_OPEN, %eax # service id-number movl $fname, %ebx # pointer to filename movl $O_RDONLY, %ecx # desired access-mode int $0x80 # enter Linux kernel movl %eax, handle # save file's handle # get the filesize movl $sys_LSEEK, %eax # service id-number movl handle, %ebx # specify which file movl $0, %ecx # the desired offset movl $SEEK_END, %edx # specify seek mode int $0x80 # enter the kernel movl %eax, fsize # save file's size # request kernel to 'map' the file movl $sys_MMAP, %eax # service id-number movl $mmargs, %ebx # pointer to arguments int $0x80 # enter the kernel movl %eax, mmaddr # save map's base-address # display the textfile's contents movl $sys_WRITE, %eax # service id-number movl $STDOUT_FILENO, %ebx # device id-number movl mmaddr, %ecx # buffer's address movl fsize, %edx # buffer's length int $0x80 # enter the kernel # request kernel to 'unmap' the file movl $sys_MUNMAP, %eax # service id-number movl mmaddr, %ebx # map's base-address movl fsize, %ecx # size of mapped region int $0x80 # enter the kernel # exit to Linux shell movl $sys_EXIT, %eax # service id-number movl $0, %ebx # exit code int $0x80 # enter the kernel .globl _start # makes entry visible