//----------------------------------------------------------------- // myargs.s // // This program displays each of its command-line arguments. // // assemble using: $ as myargs.s -o myargs.o // and link using: $ ld myargs.o -o myargs // // programmer: ALLAN CRUSE // written on: 30 JAN 2008 //----------------------------------------------------------------- .equ sys_exit, 1 # system-call ID-number .equ sys_write, 4 # system-call ID-number .equ dev_stdout, 1 # device-file ID-number .section .data nl: .ascii "\n" # newline output-string .section .text _start: # a program-loop will display each command-line argument mov $0, %esi # initialize array-index nxarg: # show the next command-line argument mov $sys_write, %eax # ID-number for service mov $dev_stdout, %ebx # ID-number for device mov 4(%esp, %esi, 4), %ecx # argument-string address # in EDX we count the characters that preceed the null-byte mov $0, %edx # initialize EDX as counter nxchr: cmpb $0, (%ecx, %edx, 1) # check: null-byte reached? je eoi # yes, counting is done inc %edx # else increment the value jmp nxchr # and test the next byte eoi: # now everything is setup for the 'write' system-call int $0x80 # request kernel service # write a 'newline' code to move cursor to a new line mov $sys_write, %eax # ID-number for service mov $dev_stdout, %ebx # ID-number for device mov $nl, %ecx # pointer to buffer mov $1, %edx # number of bytes int $0x80 # request kernel service inc %esi # advance array-index decl (%esp) # decrement arg-counter jnz nxarg # nonzero? show another # else terminate this program mov $1, %eax # ID-number for service mov $0, %ebx # use zero as exit-code int $0x80 # request kernel service .global _start # make entry-point visible .end # nothing more to assemble