in the name of zero

December 28, 2005

come. teach.

Filed under: hermetic studies

it’s getting rather pathetic, but today, i made a program that prints one to twenty and says if it’s odd or even.

there has got to be a better way to do it, and i wanna know how

anyway, i know it’s long and it sucks. but this is what i have so far.

%define STDIN 		1
%define SYS_WRITE	4
%define SYS_EXIT	STDIN
%define LIMIT		20
	
%macro print_str 1
        mov ebx, STDIN
        mov ecx, %1
        mov eax, SYS_WRITE
        call_kernel
	xor ecx, ecx	; assures the loop below wont fuck up
%endmacro
	
%macro quit 1
	mov ebx, %1
	mov eax, SYS_EXIT
	call_kernel
%endmacro
	
%macro call_kernel 0
	int 0x80
%endmacro
	
section .data
	oddstr: 	db "is odd",10
	oddlen:		equ $-oddstr
	evenstr:	db "is even",10
	evenlen:	equ $-evenstr
	numtext:	db "00 "
	numlen:		equ $-numtext
	
section .text
	global _start
	
print_int:
	xor dx, dx
        mov edi, numtext
        mov bx, 10
	
	cmp ax, 9		; a two digit number?
	div bx
	
	add dl, '0'		; transform it to ascii
	mov byte [edi+1], dl
	ja do_double
	jmp skip
	
do_double:
	add al, '0'
	mov byte [edi], al
	
skip:
        mov edx, numlen
	print_str numtext
	xor bx, bx
	ret		; end print_int
	
even_or_odd:
	
	xor dx, dx	; zero out DX
	mov bx, 2	; oddness tester
	div bx
	cmp dx, 0	; odd or even?
	je even
	jne odd
	
even:
	mov edx, evenlen
	print_str evenstr
	ret
odd:
	mov edx, oddlen
	print_str oddstr
	ret
	
_start:
	mov ax, 1	; start at one
	mov cx, LIMIT	; end at LIMIT
	
loopify:
	push cx		; save counter value
	push ax		; save current number
	call print_int
	
	pop ax		; there has got to be
	push ax		; a better way to do this
	call even_or_odd
	
	pop ax		; restore current number
	pop cx		; restore counter
	inc ax		; next number
loop loopify		; until LIMIT
	
	quit 0		; exit gracefully

Comments »

The URI to TrackBack this entry is: http://gnurbs.blogsome.com/2005/12/28/come-teach/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>


Get free blog up and running in minutes with Blogsome | Theme designs available here