
	format ELF executable
	entry start

	segment readable executable

start:
	mov eax, 4
	mov ebx, 1
	mov ecx, _msg
	mov edx, LEN_msg
	int 80h

	mov eax, 3
	mov ebx, 0
	mov ecx, _msg
	mov edx, LEN_msg
	int 80h

	mov eax, 0
	mov esi, ecx
.atoi:
	mov bl, [esi]
	cmp bl, '0'
	jb .atoi_done
	cmp bl, '9'
	ja .atoi_done
	sub bl, '0'
	imul eax, eax, 10
	add eax, ebx
	inc esi
	jmp .atoi
.atoi_done:
	mov [_nprimes], eax
	cmp eax, 0
	jz .done
	mov eax, 4
	mov ebx, 1
	mov ecx, _one
	mov edx, 2
	int 80h
	dec [_nprimes]
	jz .done
	mov eax, 4
	mov ecx, _two
	int 80h
	dec [_nprimes]
	jz .done
	mov [_prime], 3
	mov ecx, 3
.check_prime:
	mov eax, [_prime]
	cmp ecx, eax
	jae .isprime
	mov edx, 0
	div ecx
	cmp edx, 0
	jz .noprime
	add ecx, 2
	jmp .check_prime
.noprime:
	add [_prime], 2
	mov ecx, 3
	jmp .check_prime
.isprime:
	mov eax, [_prime]
	push 0
	mov ecx, 0
	mov ebx, 10
.pushing:
	mov edx, 0
	div ebx
	inc ecx
	or dl, '0'
	push edx
	cmp eax, 0
	jnz .pushing
	mov edi, _msg
.popping:
	pop eax
	stosb
	cmp eax, 0
	jnz .popping
	mov al, ' '
	stosb
	inc ecx
	inc ecx
	mov eax, 4
	mov ebx, 1
	mov edx, ecx
	mov ecx, _msg
	int 80h

	add [_prime], 2
	mov ecx, 3

	dec [_nprimes]
	jnz .check_prime

.done:
	mov eax, 4
	mov ebx, 1
	mov ecx, _endl
	mov edx, 1
	int 80h

	mov eax, 1
	mov ebx, 0
	int 80h

	segment readable writable
_nprimes dd 0
_prime dd 0
_msg db 'How many primes to print: '
LEN_msg = $-_msg
_one db '1 '
_two db '2 '
_endl db 10


