# ASMIX Commands and Utilities
# (c) Sergei Zubkov (Cubbi) 1999-2001
#
# pwd.s - print working directory
#
# $Id: pwd.s,v 1.3 2001/10/03 18:17:40 cubbi Exp $

	.globl _start
_start:
        movl    $pt_buffer+1024-1,%ebp
        movl    $0x0A,(%ebp)
        movl    $up_buffer,%esi
        movw    $0x002E,(%esi)
        pushl   $st_stat
        pushl   $root_path
	pushl	$2
	pushl   %eax
        movl    $123,%eax
	lcall	$7,$0
        addl    $16,%esp
        testl   %eax,%eax
        jnz     error_exit
        movl    st_stat,%eax		
        movl    %eax,root_dev
        movl    st_stat+16,%eax		
        movl    %eax,root_ino
main_cycpwd_cycle:
        pushl   $st_stat
        pushl   $up_buffer
	pushl	$2
        pushl   %eax
        movl	$124,%eax		
	lcall	$7,$0
        addl    $16,%esp
        testl   %eax,%eax
        jnz     error_exit
        movl    st_stat,%eax	
        movl    %eax,dev
        movl    st_stat+16,%ebx	
        movl    %ebx,ino
        cmpl    %eax,root_dev
        jne     cycpwd_not_finished
        cmpl    %ebx,root_ino
        jne     cycpwd_not_finished
        decl    %ebp
        movb    $0x2F,(%ebp)
        jmp     cycpwd_write
cycpwd_not_finished:
        movw    $0x2E2E,(%esi)
        incl    %esi
        incl    %esi
        movb    $0,(%esi)
        pushl   $0
        pushl   $up_buffer
        pushl   %eax
        movl   $5,%eax		
	lcall	$7,$0
        addl    $12,%esp
        testl   %eax,%eax
        jl      error_exit
        movl    %eax,%edi	
        pushl   $st_stat
        pushl   %eax
	pushl	$2
        pushl   %eax
        movl   $125,%eax		
	lcall	$7,$0
        addl    $12,%esp
        testl   %eax,%eax
        jnz     error_exit
        movb    $0x2F,(%esi)
        incl    %esi
        xorl    %ebx,%ebx
readdir_cycle:
        testl   %ebx,%ebx
        jz      time_to_getdent
        xorl    %eax,%eax
        movw    8(%ebx),%ax	
        addl    %eax,%ebx
        xorl    %eax,%eax
        movw    8(%ebx),%ax	
        cmpl    dirent_filled,%eax
        jge     time_to_getdent
        movw    8(%ebx),%ax	
        testl   %eax,%eax
        jnz     skip_getdent
time_to_getdent:
        pushl   $dirent_l
        pushl   $dirent
        pushl   %edi
	pushl	%eax
        movl    $81,%eax	 	
	lcall	$7,$0
        addl    $16,%esp
        movl    %eax,dirent_filled
        testl   %eax,%eax
        je      notfound
        movl    $dirent,%ebx
skip_getdent:
        xorl    %ecx,%ecx
        pushl   %esi
        pushl   %edi
        movl    %esi,%edi
        movl    %ebx,%esi
        addl    $10,%esi	
        movw    8(%ebx),%cx	
        incl    %ecx
        rep
        movsb
        popl    %edi
        popl    %esi
        pushl   %ebx
        pushl   $st_stat
        pushl   $up_buffer
	pushl	$2
	pushl	%eax
        movl	$124,%eax	
	lcall	$7,$0
        addl    $16,%esp
        popl    %ebx
        testl   %eax,%eax
        jnz     readdir_cycle
        movl    dev,%eax
        cmpl    %eax,st_stat	# st_dev
        jne     readdir_cycle
        movl    ino,%eax
        cmpl    %eax,st_stat+16	# st_ino
        jne     readdir_cycle
        movl	$0,(%esi)
        cmpb    $1,first
        je      not_first
        decl    %ebp
        movb    $0x2F,(%ebp)
not_first:
        movb    $0,first
        xorl    %edx,%edx
        pushl   %esi
        xorl    %ecx,%ecx
        movl    %ebx,%esi
        addl    $10,%esi		
seek_zero:
        incl    %ecx
        movb    (%esi,%ecx,1),%al
        testb   %al,%al
        jnz     seek_zero
        movl    %ecx,%edx
        popl    %esi
        subl    %edx,%ebp
        xorl    %ecx,%ecx
        pushl   %esi
        pushl   %edi
        movl    %ebp,%edi
        movl    %ebx,%esi
        addl    $10,%esi		
        movl    %edx,%ecx
        rep
        movsb
        popl    %edi
        popl    %esi
        pushl   %edi
	pushl	%eax
        movl   $6,%eax		
	lcall	$7,$0
        addl    $8,%esp
        jmp     main_cycpwd_cycle
cycpwd_write:
        movl    $pt_buffer+1024,%edx
        subl    %ebp,%edx
        pushl   %edx		
        pushl   %ebp		
        pushl   $1
	pushl	%eax
        movl   $4,%eax		
	lcall	$7,$0
        addl    $16,%esp
	xorl	%eax,%eax
	pushl	%eax
	pushl	%eax		
	incl	%eax		
	lcall	$7,$0
exit_errno:
exit_ENOENT:
notfound:
error_exit:
	xorl	%eax,%eax
	incl	%eax
	pushl	%eax		
	pushl	%eax		
	lcall	$7,$0		

	.data
pwd_path:
        .ascii  ".\000"
root_path:
        .ascii  "/\000"
first:
        .byte   1

	.bss
        .lcomm  st_stat,136
        .lcomm  pt_buffer,1024
        .lcomm  up_buffer,1024
        .lcomm  root_dev,4
        .lcomm  dev,4
        .lcomm  root_ino,4
        .lcomm  ino,4
        .lcomm  bpt,4
        .lcomm  dirent,4096 # the larger, the faster
dirent_l=4096
        .lcomm  dirent_filled,4
