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

	.section ".text"
	.globl _start
_start:
	ld	[%sp+0x40],%o5	
	cmp	%o5,1
	beq	error_exit
	nop
	ld	[%sp+0x48],%o1
	mov	%o1,%l1
1:	ldub	[%l1],%l5
	inc	%l1
	cmp	%l5,0
	bne	1b
	nop
1:	dec	%l1
	cmp	%l1,%o1
	beq	empty_string
	nop
	ldub	[%l1-1],%o3
	cmp	%o3,0x2f
	beq	1b
	nop
	mov	%l1,%o3
1:	dec	%o3
	ldub	[%o3-1],%o4
	cmp	%o4,0x2f
	bne	1b
	cmp	%o3,%o1
	ble	end_parse
	nop
	mov	%o3,%o1
end_parse:
	cmp	%o5,2	
	beq	_write
	nop
	ld	[%sp+0x4C],%o2	
	mov	%o2,%l2
1:	ldub	[%l2],%l5
	inc	%l2
	cmp	%l5,0
	bne	1b
	nop
	mov	%l1,%l3
1:	dec	%l3
	dec	%l2
	ldub	[%l2-1],%o3
	ldub	[%l3],%o4
	cmp	%o3,%o4
	beq	1b
	nop
	dec	%l2
	cmp	%l2,%o2	
	beq	_write
	nop
	inc	%l3
	cmp	%l3,%o1
	beq	_write
	nop
	mov	%l3,%l1	
	ba	_write
	nop
empty_string:
	mov	0x2f,%o3
	stub	%o3,[%l1]
	inc	%l1
_write:
	mov	0xA,%o3		
	stub	%o3,[%l1]
	inc	%l1
	sub	%l1,%o1,%o2
	mov	1,%o0
	mov	4,%g1	
	ta	8
exit:
	clr	%o0
	mov	1,%g1	
	ta	8
error_exit:
	mov	1,%o0
	mov	1,%g1
	ta	8
