이번주 구현 목표였던 gets.

만들면 날려먹고 만들면 또 날려먹어서 고생좀 했다.



.globl main
main:
	push %ebp
	mov %esp, %ebp
	sub $0x1000, %esp
	lea -0x1000(%ebp), %eax
	push %eax
	call gets
	lea -0x1000(%ebp), %eax
	push %eax
	call puts
	leave
	ret

gets:
	push %ebp
	mov %esp, %ebp
	sub $0x1000, %esp
	mov $0x03, %eax
	mov $0x00, %ebx
	lea -0x1000(%ebp), %ecx
	mov $0x1000, %edx
	int $0x80
	lea -0x1000(%ebp), %eax
	push $0x0a
	push %eax
	call strchr
	movb $0x00, (%eax)
	lea -0x1000(%ebp), %eax
	push %eax
	mov 0x08(%ebp), %eax
	push %eax
	call strcpy
	leave
	ret

strchr:
	push %ebp
	mov %esp, %ebp
	mov 0x08(%ebp), %eax
	movb 0x0c(%ebp), %bl
	.strchr_loop:
		cmpb (%eax), %bl
		je .strchr_finish
		cmpb $0x00, (%eax)
		je .strchr_notfound
		inc %eax
		jmp .strchr_loop
	.strchr_notfound:
		mov $0x01, %eax
	.strchr_finish:
		leave
		ret


puts:
	push %ebp
	mov %esp, %ebp
	mov 0x08(%ebp), %eax
	push %eax
	call strlen
	mov %eax, %edx
	mov $0x04, %eax
	mov $0x01, %ebx
	mov 0x08(%ebp), %ecx
	int $0x80
	push $0x0a
	lea (%esp), %ecx
	mov $0x04, %eax
	mov $0x01, %ebx
	mov $0x01, %edx
	int $0x80
	add $0x04, %esp
	leave
	ret

strcpy:
	push %ebp
	mov %esp, %ebp
	xor %eax, %eax
	mov 0x08(%ebp), %edi
	mov 0x0c(%ebp), %esi
	.strcpy_loop:
		movb (%esi), %al
		movb %al, (%edi)
		cmpb $0x00, %al
		je .strcpy_finish
		inc %esi
                inc %edi
		jmp .strcpy_loop
	.strcpy_finish:
		leave
		ret


strlen:
	push %ebp
	mov %esp, %ebp
	xor %ecx, %ecx
	mov 0x08(%ebp), %eax
	.strlen_loop:
		movb (%eax), %bl
		cmpb $0x00, %bl
		je .strlen_finish
		inc %eax
		inc %ecx
		jmp .strlen_loop
	.strlen_finish:
		mov %ecx, %eax
		leave
		ret

'Programming' 카테고리의 다른 글

Assembly Programming - atoi  (0) 2015.10.11
Assembly Programming - isAlpha, isNumber  (0) 2015.10.11
Assembly Programming - strchr  (0) 2015.10.10
Assembly Programming - puts  (0) 2015.10.10
Assembly Programming - strcpy  (0) 2015.10.10
블로그 이미지

__미니__

E-mail : skyclad0x7b7@gmail.com 나와 계약해서 슈퍼 하-카가 되어 주지 않을래?

,