//----------------------------------------------------------------- // reorder.s (Revised version of the 'myargs.s' demo) // // This program displays each of its command-line strings on // a separate lines, but in reversed order. (Hence it shows // one possible way to answer Question V on Midterm Exam I.) // // assemble using: $ as reorder.s -o reorder.o // and link using: $ ld reorder.o -o reorder // // programmer: ALLAN CRUSE // written on: 25 FEB 2009 //----------------------------------------------------------------- .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 (%rsp), %ebx # index for null pointer nxarg: dec %rbx # back up by one entry # show the next command-line string 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 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