! ASMIX Commands and Utilities
! (c) Sergei Zubkov (Cubbi) 1999-2001
!
! pwd.s - print working directory
!
! $Id: pwd.s,v 1.1 2001/10/04 19:41:43 cubbi Exp $

        .seg "text"
        .proc 04
        .global start
start:
	mov	1,%l7
	sethi	%hi(pt_buffer+1023),%l2
	or	%l2,%lo(pt_buffer+1023),%l2
	mov	0x0A,%o1
	stb	%o1,[%l2]
	sethi	%hi(up_buffer),%l1
	or	%l1,%lo(up_buffer),%l1
	sethi	%hi(0x2E00),%o1
	or	%o1,%lo(0x2E00),%o1
	sth	%o1,[%l1]
	sethi	%hi(st_stat),%o1
	or	%o1,%lo(st_stat),%o1
	sethi	%hi(root_path),%o0
	or	%o0,%lo(root_path),%o0
	mov	38,%g1	
	ta	%g0
	cmp	%o0,0
	bne	error_exit
	nop
	sethi	%hi(st_stat),%o0
	or	%o0,%lo(st_stat),%o0
	lduh	[%o0],%l3		
	add	%o0,4,%o0
	ld	[%o0],%l4		
main_cycpwd_cycle:
	sethi	%hi(st_stat),%o1
	or	%o1,%lo(st_stat),%o1
	sethi	%hi(up_buffer),%o0
	or	%o0,%lo(up_buffer),%o0
	mov	40,%g1			
	ta	%g0
	cmp	%o0,0
	bne	error_exit
	nop
	sethi	%hi(st_stat),%o0
	or	%o0,%lo(st_stat),%o0
	lduh	[%o0],%l5		
	add	%o0,4,%o0
	ld	[%o0],%l6		
	cmp	%l3,%l5
	bne	cycpwd_not_finished
	nop
	cmp	%l4,%l6
	bne	cycpwd_not_finished
	nop
	dec	%l2
	mov	0x2F,%o1
	stb	%o1,[%l2]
        ba	cycpwd_write
	nop
cycpwd_not_finished:
	mov	0x2E,%o1
	stb	%o1,[%l1]
	stb	%o1,[%l1+1]
	stb	%g0,[%l1+2]
	add	%l1,2,%l1
	clr	%o1
	sethi	%hi(up_buffer),%o0
	or	%o0,%lo(up_buffer),%o0
	mov	5,%g1		
	ta	%g0
	cmp	%o0,0
	blt	error_exit
	nop
	mov	%o0,%l0	
	sethi	%hi(st_stat),%o1
	or	%o1,%lo(st_stat),%o1
	mov	62,%g1	
	ta	%g0
	cmp	%o0,0
	bne	error_exit
	nop
	mov	0x2F,%o1
	stb	%o1,[%l1]
	inc	%l1
	clr	%i0	
readdir_cycle:
	cmp	%i0,0
	beq	time_to_getdent
	nop
	clr	%o4
	lduh	[%i0+8],%o3 	
	add	%i0,%o3,%i0
	lduh	[%i0+8],%o4	
	cmp	%o4,%i1  	
	bge	time_to_getdent
	nop
	ld	[%i0+8],%o4	
	cmp	%o4,0
	bne	skip_getdent
	nop
time_to_getdent:
	sethi	%hi(dirent_l),%o2
	or	%o2,%lo(dirent_l),%o2
	sethi	%hi(dirent),%o1
	or	%o1,%lo(dirent),%o1
	mov	%l0,%o0		
	mov	174,%g1		
	ta	%g0
	mov	%o0,%i1 	
	cmp	%o0,0
	beq	notfound
	nop
	sethi	%hi(dirent),%i0
	or	%i0,%lo(dirent),%i0
skip_getdent:
	clr	%o4
	mov	%l1,%o5
	mov	%i0,%i2
	add	%i2,12,%i2 
rep_loop:
	ldub	[%i2],%o3
	cmp	%o3,0
	beq	stop_rep
	nop
	stb	%o3,[%o5]
	inc	%i2
	inc	%o5
	ba	rep_loop
	nop
stop_rep:
	stb	%g0,[%o5]
	sethi	%hi(st_stat),%o1
	or	%o1,%lo(st_stat),%o1
	sethi	%hi(up_buffer),%o0
	or	%o0,%lo(up_buffer),%o0
	mov	40,%g1	! syscall number 40 - lstat()
	ta	%g0
	cmp	%o0,0
    	bne	readdir_cycle
	nop
	sethi	%hi(st_stat),%o0
	or	%o0,%lo(st_stat),%o0
	lduh	[%o0],%o1 
	cmp	%o1,%l5
	bne	readdir_cycle
	nop
	add	%o0,4,%o0
	ld	[%o0],%o1 
	cmp	%o1,%l6
	bne	readdir_cycle
	nop
	stb	%g0,[%l1]
	cmp	%l7,1
	beq	not_first
	nop
	dec	%l2
	mov	0x2F,%o1
	stb	%o1,[%l2]
not_first:
	clr	%l7 	
	add	%i0,12,%i0
	mov	%i0,%o0
len_loop:
	inc	%o0
	ldub	[%o0],%o1
	cmp	%o1,0
	bne	len_loop
	nop
	dec	%o0
	dec	%l2
rep_loop2:
	ldub	[%o0],%o3
	stb	%o3,[%l2]
	dec	%o0
	dec	%l2
	cmp	%o0,%i0
	bge	rep_loop2
	nop
stop_rep2:
	inc	%l2
	mov	%l0,%o0
	mov	6,%g1 	
	ta	%g0
	ba     main_cycpwd_cycle
	nop
cycpwd_write:
	sethi	%hi(pt_buffer+1023),%o2
	or	%o2,%lo(pt_buffer+1023),%o2
	sub	%o2,%l2,%o2	
	add	%o2,1,%o2	
	mov	%l2,%o1 
	mov	1,%o0
	mov	4,%g1	
	ta	%g0
exit:
	clr	%o0
	mov	1,%g1	
	ta	%g0
exit_errno:
exit_ENOENT:
notfound:
error_exit:
	mov	1,%o0
	mov	1,%g1
	ta	%g0


        .seg "data"
pwd_path:
	.ascii  "."
	.byte	0
root_path:
	.ascii  "/"
	.byte	0

        .seg "bss"
        .comm  st_stat, 64 
        .comm  pt_buffer,1024 
        .comm  up_buffer,1024
        .comm  bpt,4
        .comm  dirent,4096 
dirent_l=4096

