in the name of zero

December 30, 2005

gcc inline

Filed under: hermetic studies

ok. so i know a little c. and i know a little asm. where do i go from here? basic gcc inline assembly.

int main(int argc, char **argv)
{
        int ctr;
        ctr = 1;
        do {
                printf("%d\n", ctr);    
	
                /*
                 * try changing ctr++ to inline asm
                 * something like an incrementor
                 */
                __asm__("inc %%ecx"             /* ctr ++ */
                                :"=c" (ctr)     /* ctr => ecx */
                                :"c" (ctr)      /* ecx <= ctr */
                        );
	
        } while (ctr < 11);
	
        /* this time, i'll try exchanging values */
	
        int first = 1;
        int second = 2;
	
        __asm__(""
                : "=b" (first), "=a" (second)
                : "a" (first), "b" (second)
               );
	
        printf("inverted : %d  %d\n", first, second);
        return 0;
}

fantastic. learning c and assembly is fun. i promise to experiment with _real life_ activities next time. you know, like dating and flirting. stuff like that.

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

December 25, 2005

happy birthday jesus

“Come and behold Him, Born the King of Angels! O come, let us adore Him, Christ the Lord!

merry christmas to everyone on earth. but most of all, merry christmas, stephanie my dear.

December 24, 2005

lust for enlightenment

[thoughts]
at irc.dal.net #linux today,

<Scruffypoo> it’s a well-established fact that women are evil and are not to be trusted. do we really have to keep rehashing that?
<Interex> thats true
<p3nguin> Did I tell you about the peanut butter cookies Kate made for me?
<Scruffypoo> if you want a dead horse to beat, we can always argue about text editors.
<Interex> no, thats gross though

girls. perhaps they’re evil. but we love them anyway.

[linux assembly 101]
ok, so i gotta admit. “hello world” doesn’t cut it anymore. this time, i delved deeper into doing some basic bit manipulations and functions. the code below uppercase-ifies a string.

; uppercase.asm
	
section .data
	string:	db "stephanie",10
	strlen:	equ $-string
	
section .text
	global	_start
	
_start:
	mov ecx, string
	call _write
	
	mov edi, string
	mov ecx, strlen-1	; loop counter
	
_upperfy:
	mov al, [edi]	; first letter
	cmp al, 97	; capitalize small
	
	jb skip		; letters only
	cmp al, 122
	ja skip
	sub al, 32	; capitalize letter
	mov [edi], al	; store back
skip:
	inc edi		; move to next letter
	loop _upperfy	; capitalize every letter
	
	mov ecx, string ; capitalized form
	call _write
	call _quit
	
_write:
	push byte 4
	push byte 1
	push byte strlen
	
	pop edx
	pop ebx
	pop eax
	int 80h
	ret	
	
_quit:
	mov eax, 1
	mov ebx, 0
	int 80h

and the output?

amerei@heaven ~/git/null/public/asm $ nasm -f elf uppercase.asm
amerei@heaven ~/git/null/public/asm $ ld -s -o uppercase uppercase.o
amerei@heaven ~/git/null/public/asm $ ./uppercase
stephanie
STEPHANIE

[life and love]
recently, when i get bored or frustrated, i’d look for my pet dog, hug him so tight and play with him like how girls play with their teddy bears. (usually involves lots and lots of hugging)

last year i gave steph a cute, blue, hello kitty pillow for christmas. probably not the most suitable gift for the holidays. i wonder if she hugs it everynight? i wonder if she liked it? i never asked.

[haiku]

as it snows up north.
december rain pours.
with it, cold breeze.

update: a blessed happy birthday miss ann christ eve!

December 22, 2005

linux asm 101

Filed under: hermetic studies

it makes some sense to me now.

section .data
        hello:          db 'Hello world!',10
        hellolen:       equ $-hello
	
section .text
        global  _start		; obj entry
				; ld(1) looks for this by default
	
_start:
	
        mov eax, 4		; write() system call
        mov ebx, 1		; standard output
        mov ecx, hello		; my dear string
        mov edx, hellolen	; length of my dear string
        int 0x80		; call kernel
	
        mov eax, 1		; exit() system call
        mov ebx, 0		; because things worked
        int 0x80		; exit gracefully

at first though (which is to say, sometime in the past), even with a set of comments, i still couldn’t understand anything even remotely close as to why that silly arrangement of numbers and instructions worked.

these are invaluable

/usr/include/asm/unistd.h
man 2 [sys_call] e.g. man 2 write

i’m still learning but let me just share what i’ve learned so far. in linux assembly:

1) you put the system call number in the eax register.
2) you set the arguments to _that_ system call (respectively) in ebx, ecx, edx, esi, edi and ebp
3) if (arguments > 6), ebx will contain the memory location where the arguments are stored.
4) the relevant interrupt is 80h (0x80) (in dos, it is int 21h)
5) the result is usually returned in register eax

thanks to :
linux assembly dot org
google dot com

more on this topic as i progress.

December 17, 2005

cutting ties

it’s pouring like a bitch as i’m typing this. but there’s moderate promise of peaceful weather ahead. that’s it for the situation here right now.

i highly recommend reading this entry while listening to hajime no ippo - yuuzora no kamihikouki

anyway, it reminded me of what happened to a three year old friendship with a girl that i recently blew off. a spontaneous decision from yours truly. and with good reason. though i guess it is as shallow as any reason i can ever come up with. the thing is, i got mad at her because she always seems to end the conversation, our conversation, with silence. what i mean is, she leaves me hanging-waiting for her reply on most times. it wasn’t always like this. back then, we would even have conversations in friendster, thru text, thru email, irc. you name it. trading personality quiz results was the trend. i got to know a lot about her as she about me. i’d even randomly joke about the two of us getting married and having children. sometimes, even role-play that we’re already parents and tell her that i’m going home, asking sweetly, “honey. what’s for dinner”? it was a joke of course, but i wasn’t about to kid myself, because deep down inside, i somehow knew that i really wanted things to be that way.

the turning point in that happy, honest to goodness friendship for me was when she got addicted playing some sort of an online game. up until recently, i used to just dissmiss other people’s complaints, rants, whatever… as just some ploy to gain attention or be used as basis for legal action against rich video game companies. they apparently fell victim to a special someone’s addiction to gaming. some cases even extreme as persons actually dying. basically just trying to make a living out of seeking financial compensation for their loss. or so i thought.

needless to say, her replies dwindled slowly. until of course it reached to a point where she replies only to say “sorry, i’m busy playing [game]”. no more meaningful conversation. just like that. i feel my vigor being sucked up always as the clock ticked and i get the cold reply many freaking minutes later. i’d think that “hey! at last, the game’s finished. she still managed to reply. that’s the good thing. the thought matters most. who am i to be expecting more from her afterall?” the next few moments are spent with her just sending smileys, because i guess she’s busy alt-tabbing between the dektop and the [game] window. …and just when the conversation’s starting to have life again, bang, the whole cycle repeats itself. and everytime it does, her reply curve simply got steeper and steeper. i tried doing the same thing to her, as immature as that may sound, (this whole sulking thing is immature anyway) making her feel just how much it sucks whenever she leaves me to do something else, while i’m there waiting… hanging… clueless. i thought i’d let her know that i already hate the treatment she has been giving me, in the most subtle way so as not to hurt her feelings flat out.

but then it became clear that another approach was needed. too bad she didn’t get the message. perhaps my actions were a little to stealthy. either that, or she’s completely insensitive to the feelings of a boy like me, who’s only dream is to have a girlfriend and who’s fetish include computers, talking about his way of urinating and sometimes, women’s apparel. i tried so hard not to initiate any chat conversations with her, thinking that she’s busy playing or chatting with some other person she met online while playing. the rationale is that it will be less painful if she becomes silent again because afterall, i didn’t start the conversation (if ever we get to chat).

i can’t take it anymore though. it’s hopeless. i hate the fact that our friendship has sunk to this level of patheticness. each conversation i’m having with her turns into a fight to get her attention. not that it matters. i feel so left out compared to the other people talking to her. but then as always, i’m just thinking for myself. she has been very good to me. infact, she was never a bad friend. never. but i guess i just want something she can never give me.

so i told her that it is in the best interest for the both of us if we stop talking to each other. she would have time to focus more on things that appeal to her fancy. and i won’t get hurt because she’s human and cannot help herself from committing mistakes such as, forgetting to reply.

i’ll be prompt today and simply just end this entry with an infamous buzz line.

“they didn’t live happily ever after.”

December 12, 2005

i stare

the art and it’s matching haiku.

afternoon,
on the third floor,
students walking down the isle.

December 11, 2005

literature

i wrote a haiku today!

sunday picnic time
dateless at home
scratching my scrotum

— niel

December 10, 2005

still, on paolo’s puzzle

Filed under: hermetic studies

well, i just minimized the number of loops.

the ones place of the first digit must not be one. hint: identity property of multiplication.

the tens place must be something not greater than 4. why? because numbers 5-9 will generate two digit products if multiplied to any number except one. right? the largest possible equation you can make using the range 1 to 9 that will yield a single digit product is 4 multiplied to 2 which is eight. exlcusive from multiplying by one of course.

that limits the first number to 49. still, excluding the numbers 1 ,11 , 21, 31, 41, 22, 33, 44 i have set 41 possible hard limit combinations to the first, two digit number. (or so, i honestly think)

the third number (the multiplier), can’t possibly be one. again because of identity property of multiplication.

then i thought that i need to construct a condition statement that prevents the program from descending deeper into the code in case the product:

a) is a three digit number or mathematically speaking, product/100 = true, (remembering that “not zero” is always “true”, (x) digit numbers will always yield zero when integer divided by (x+1) or more digit divisors.)
b) has zero for it’s tens place e.g. 10, 20, 30 … or simply !(product%10)
d) has digits that are already taken

then, i can remove two more nested loops and some codition statements in the old program because i can already make another set of logic block comparisons using the generated product.

instinctively of course, i only needed to determine if the final answer’s tens place digit and ones place digit are not taken for the program to complete.

whereas the old program did 2745 loops to solve the magic order, the new one solves the same puzle in a relatively smaller number of iterations.

see?

amerei@heaven ~/git/null/public $ ./puzzle
  17
*  4
 ----
  68
+ 25
 ----
  93
	
it took 422 loops.

i therefore conclude that i suck at math.

December 9, 2005

paolo’s puzzle (of death)

Filed under: hermetic studies

i solved it! i solved it! on the third day too! gosh, i feel like jesus!

you know you’re screwed when you need to use three or more indentations in your program. in my case, it’s 7 fucking nested for loops.

i _would_ post the code i concocted in all of it’s unadulterated monstrosity but, let the following command spare you from wasting your time.

amerei@heaven ~/git/null $ wc -l puzzle.c
101 puzzle.c

compilation is a bit ricer-ish. and i also had my fingers crossed.

amerei@heaven ~/git/null $ gcc -funroll-all-loops -fomit-frame-pointer \
-march=pentium3 -o puzzle puzzle.c

and finally, i took the plunge.

amerei@heaven ~/git/null $ time ./puzzle
  17
*  4
 ----
  68
+ 25
 ----
  93
	
it took 2745 loops.
	
real    0m0.003s
user    0m0.000s
sys     0m0.002s

there you go. all digits from 1 to 9 arranged in the magic order. no repetitions. thanks man! i learned a lot from this activity. the three days i spent trying to finish the code was never boring! i remember waking up at around 3 am to code because my brain refuses stop working on it. i was also weary that if i won’t codify the idea that woke me up in the first place, i’d forget it soon and i’ll lose my rhythm again.

“make your code work first before putting emphasis on optimizations”

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