# ;; dirname.s # in busybox fashion, if you like all of these standalones together, this is the module # tries to figure out which one to start. # # # #include .data eoln: .string "\n" slash: .string "/" period: .string ".\n" # # # .file "dirname.s" .text .globl dirname dirname: cmp $1,(%rsp) # check to see if there are no arguments je done xor %r10,%r10 # we'll use %r10 for our "zero" register xor %r12,%r12 # we'll use %r12 as a "found" flag ; 0 = not found, 1 = found mov 16(%rsp),%rbx # %rbx now has pointer to beginning of first argument (should make sure that # argc is big enough ... ) mov 16(%rsp),%rdx # %rdx also has the beginning for now, but if we find a slash, we update %rdx to the location of the slash movb slash,%cl # put slash into %cl so we can make comparison name_loop: cmpb %r10b,(%rbx) # is the byte a zero? then we are finished looking at the command je finished cmpb %cl,(%rbx) # is it a slash? then we save our position je found_a_slash inc %rbx jmp name_loop found_a_slash: mov %rbx,%rdx # save position in %rdx inc %rbx # increment %rbx inc %r12 # set %r12 as a flag that we found it jmp name_loop finished: # did we find it? if not, then output "." cmp $0,%r12 jne found_last_slash lea period,%rsi # okay, send out a period and eoln mov $1,%rdi call writestr jmp done found_last_slash: movb $0,(%rdx) # need to terminate this string mov 16(%rsp),%rsi # now point %rsi back to the beginning of the string mov $1,%rdi call writestr lea eoln,%rsi # don't forget to send an eoln also mov $1,%rdi call writestr done: mov $0, %rdi # and exit with a zero status mov $__NR_exit,%rax # syscall = 60 is exit syscall