# Coded by Sha0,  BadChecksum tm
# Ejemplo de uso de la syscall __NR_readdir los virus suelen hacer algo similar para sacar los ficheros huesped,
# obvianmente tambien tiene otros fines mas productivos


.globl main


.directorio:
	.string ".";

main:
	push %ebp
	movl %esp, %ebp
	subl $80, %esp

	movl $05, %eax		# __NR_open
	movl $.directorio, %ebx	# dir to open
	xorl %ecx, %ecx		# flags 0
	xorl %edx, %edx		# flags 0
	int $0x80
	movl %eax, %esi

	movl $20, %ecx		# como maximo que muestre 20

files:
	push %ecx
	movl %esi, %ebx		# descriptor
	movl $89, %eax		# __NR_readdir
	movl %ebp, %ecx		
	subl $40, %ecx		# address
	movl $1, %edx		# count 
	int $0x80		# ya tenemos un dirent
	test %eax, %eax
	jz end



	leal 0x0a(%ecx), %eax	# dirent.d_name
	call sizeof
	movl %ecx, %edx		# sizeof d_name
	movl %eax,%ecx		# dirent.d_name
	movl $4, %eax		# __NR_write
	movl $1, %ebx		# stdout
	int $0x80
	

	pop %ecx
	loop files

end:
	pop %ecx	# counter
	movl %esi, %ebx	# descriptor	
	movl $6, %eax	# close
	int $0x80


	movl $1, %eax	# exit
	xorl %ebx, %ebx # 0
	int $0x80





sizeof:				# se envia por eax el string a medir, se retorna por cl la longitud.
	push %ebp
	movl %esp, %ebp
	subl $4, %esp

	xorl %ecx, %ecx		# inicio contador
	xorl %ebx, %ebx		# para comparar con 0

loopsizeof:
	push %eax
	add %ecx, %eax
	cmpb (%eax), %bl
	je gotsizeof
	popl %eax
	inc %cl

	cmpb $0xff, %cl
	je gotsizeof

	jmp loopsizeof

gotsizeof:
	popl %eax
	leave
	ret
