//----------------------------------------------------------------- // seehosts.s // // This assembly language program reads the 'etc/hosts' file // and displays its contents on the standard output device. // // assemble using: $ as seehosts.s -o seehosts.o // and link using: $ ld seehosts.o -o seehosts // then run using: $ ./seehosts // // programmer: ALLAN CRUSE // written on: 24 MAR 2008 //----------------------------------------------------------------- .equ sys_exit, 1 .equ sys_read, 3 .equ sys_write, 4 .equ sys_open, 5 .equ O_RDONLY, 0 .equ STDOUT, 1 .equ BUFLEN, 4096 .section .data path: .asciz "/etc/hosts" # null-terminated pathname handle: .int -1 # for storing file-handle inbuf: .space BUFLEN # for storing file contents inlen: .int BUFLEN # for storing input length .section .text _start: # open the file for reading mov $sys_open, %eax # system-call ID-number lea path, %ebx # address of pathname mov $O_RDONLY, %ecx # specify access-mode int $0x80 # invoke kernel service mov %eax, handle # save the file-handle # read the file's contents mov $sys_read, %eax # system-call ID-number mov handle, %ebx # file-handle lea inbuf, %ecx # address of input-buffer mov $BUFLEN, %edx # length of input-buffer int $0x80 # invoke kernel service mov %eax, inlen # save the character count # display the file's contents mov $sys_write, %eax # system-call ID-number mov $STDOUT, %ebx # device-file ID-number lea inbuf, %ecx # address of output-buffer mov inlen, %edx # length of output string int $0x80 # invoke kernel service # terminate this program mov $sys_exit, %eax # system-call ID-number mov $0, %ebx # use zero as exit-code int $0x80 # invoke kernel service .global _start # make entry-point visible .end # nothing else to assemble