// ASMIX Commands and Utilities
// (c) Sergei Zubkov (Cubbi) 1999
//
// basename.s - return non-directory portion of a pathname
//
// $Id: basename.s,v 1.3 2001/10/11 19:52:44 cubbi Exp $

	.globl _start
_start:
	popl	%ecx
	decl	%ecx
	jz	error_exit
	popl	%eax
	popl	%ebp
	movl	%ebp,%esi 
	xorl	%eax,%eax
strlen_1:
	cmpb	%al,(%esi)
	je	end_of_string
	incl	%esi
	jmp	strlen_1
end_of_string:
scan_slash:
	cmpl	%esi,%ebp
	je	empty_string
	decl	%esi
	cmpb	$0x2F,(%esi)
	je	scan_slash
	movl	%esi,%edx
scan_nonslash:
	cmpl	%edx,%ebp
	je	end_nonslash
	decl	%edx
	cmpb	$0x2F,(%edx)
	jne	scan_nonslash
	incl	%edx
end_nonslash:
	movl	%edx,%ebp  
	decl	%ecx
	jz	output	
	popl	%ebx	
	movl	%ebx,%edi
strlen_2:
	cmpb	%al,(%edi)
	je	end_of_suffix
	incl	%edi
	jmp	strlen_2
end_of_suffix:
	movl	%edi,%ecx
	subl	%ebx,%ecx
	decl	%edi
	pushl	%esi
	std
	repz
	cmpsb
        jne     1f
        incl    %esi
        cmpl    %esi,%ebp
        jne     output2
1:      popl    %esi
        jmp     output
empty_string:
        movb    $0x2F,%al
        movb    %al,(%ebp)
output:
        incl    %esi
output2:
	movb	$012,(%esi)
	incl	%esi
	subl	%ebp,%esi
	pushl	%esi
	pushl	%ebp	
        pushl   $1
        movl    $4,%eax	
	pushl	%ebx
	lcall	$7,$0
exit:
	xorl	%ebx,%ebx
real_exit:
	pushl	%ebx
	movl	$1,%eax	
	pushl	%ebx
	lcall	$7,$0
error_exit:
	xorl	%ebx,%ebx
	incl	%ebx
	jmp	real_exit
