# ;; 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 _found: .double 0 eoln: .string "\n" slash: .string "/" period: .string ".\n" # # # .file "dirname.s" .text .globl dirname dirname: cmp $1,(%esp) # 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 pop %ebx # throw away argc, %esp now points at command pop %ebx # throw away command line, %esp now points at first (and only!) argument mov (%esp),%ebx # %rbx now has pointer to beginning of first argument (should make sure that # argc is big enough ... ) mov (%esp),%edx # %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 $0,(%ebx) # is the byte a zero? then we are finished looking at the command je finished cmpb %cl,(%ebx) # is it a slash? then we save our position je found_a_slash inc %ebx jmp name_loop found_a_slash: mov %ebx,%edx # save position in %rdx inc %ebx # increment %rbx incl _found # set %r12 as a flag that we found it jmp name_loop finished: # did we find it? if not, then output "." cmpb $0,_found jne found_last_slash lea period,%ecx # okay, send out a period and eoln mov $1,%ebx call writestr jmp done found_last_slash: movb $0,(%edx) # need to terminate this string mov $1,%ebx # now point %rsi back to the beginning of the string mov (%esp),%ecx call writestr lea eoln,%ecx # don't forget to send an eoln also mov $1,%ebx call writestr done: mov $0,%ebx # and exit with a zero status mov $__NR_exit,%eax # syscall = 60 is exit int $0x80