//----------------------------------------------------------------- // 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 // revised on: 08 FEB 2009 -- for Linux x86_64 environment //----------------------------------------------------------------- .equ sys_exit, 60 # system-call ID-number .equ sys_write, 1 # 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, %rbx # initialize array-index nxarg: # show the next command-line argument mov $sys_write, %rax # ID-number for service mov $dev_stdout, %rdi # ID-number for device mov 8(%rsp, %rbx, 8), %rsi # argument-string address # in RDX we count the characters that preceed the null-byte mov $0, %rdx # initialize RDX as counter nxchr: cmpb $0, (%rsi, %rdx, 1) # check: null-byte reached? je eoi # yes, counting is done inc %rdx # else increment the value jmp nxchr # and test the next byte eoi: # now everything is setup for the 'write' system-call syscall # request kernel service # write a 'newline' code to move cursor to a new line mov $sys_write, %rax # ID-number for service mov $dev_stdout, %rdi # ID-number for device mov $nl, %rsi # pointer to buffer mov $1, %rdx # number of bytes syscall # request kernel service inc %rbx # advance array-index decq (%rsp) # decrement arg-counter jnz nxarg # nonzero? show another # else terminate this program mov $sys_exit, %rax # ID-number for service mov $0, %rdi # use zero as exit-code syscall # request kernel service .global _start # make entry-point visible .end # nothing more to assemble