flat assembler
Message board for the users of flat assembler.
Index
> Projects and Ideas > [DEAD] X to the power of...well Y 
Author 

Madis731
Hi,
another program that demonstrates my bad programming habits  it was the time when I made a lot of unuseful comments like Code: ;this line increases eax by one but if you don't let it disturb, you might even find something useful out of it. What it does it take x^y like 3^4=3*3*3*3=81 etc. but it can handle large numbers like 2^131072 and still show accurate results  I might reprogram it some day to output to a file or a textedit box
Last edited by Madis731 on 08 Sep 2006, 10:00; edited 1 time in total 

28 Feb 2006, 20:56 

Mota
Well, I've just devised a simple recursive pow function that works exclusively for integers. It is guaranteed to work faster than your average loop, because it splits an equation in the most efficient way: down the middle.
Code: pow: ; I: eax = x, ecx = y / O: eax = x^y or ecx, ecx jnz .cont0 mov eax, 1 ret ; x^0 = 1 .cont0: cmp ecx, 1 jnz .cont1 ret ; x^1 = x .cont1: cmp ecx, 2 jnz .cont2 mul eax, eax ; x^2 = x*x ret .cont2: ; Okay, what happens is that (x^y = x^(y >> 1)^2 * x^(y & 1)), ; therefore minimizing the number of necessary operations. test ecx, 1 jz .no_one push ecx eax shr ecx, 1 call pow mul eax, eax mul eax, [esi] pop ecx ecx ret jmp .cont .no_one: push ecx shr ecx, 1 call pow ; (x^(y/2) * x^(y/2)) = (x^(y/2))^2 mul eax, eax ; so there is no need to recalculate the value. pop ecx ret This was good exercise. Another one for the code bank. 

08 May 2006, 23:14 

Madis731
You mean:
Code: pow: ; I: eax = x, ecx = y / O: eax = x^y or ecx, ecx jnz .cont0 mov eax, 1 ret ; x^0 = 1 .cont0: cmp ecx, 1 jnz .cont1 ret ; x^1 = x .cont1: cmp ecx, 2 jnz .cont2 mul eax ; x^2 = x*x ret .cont2: ; Okay, what happens is that (x^y = x^(y >> 1)^2 * x^(y & 1)), ; therefore minimizing the number of necessary operations. test ecx, 1 jz .no_one push ecx eax shr ecx, 1 call pow mul eax mul dword[esp] pop ecx ecx ret ; jmp .cont .no_one: push ecx shr ecx, 1 call pow ; (x^(y/2) * x^(y/2)) = (x^(y/2))^2 mul eax ; so there is no need to recalculate the value. pop ecx ret 

10 May 2006, 08:41 

Mota
No, I mean exactly what I wrote. What you wrote won't even compile.
Anyway, peace out. 

11 May 2006, 22:12 

donkey7
slightly shorter version:
Code: format pe gui power equ 7 base equ 5 mov ecx,power mov eax,base mov ebx,1 .loop: shr ecx,1 jnc .skip imul ebx,eax .skip: imul eax,eax or ecx,ecx jnz .loop int3 result in ebx. should work :) btw: algorithm taken from clr's 'introduction to algorithms'. interesting book! _________________ Keep coding! 

16 May 2006, 17:19 

vid
hmmm... is this really a project?


08 Sep 2006, 08:26 

Madis731
Well, no
a demonstration of some ideas at most... 

08 Sep 2006, 09:59 

< Last Thread  Next Thread > 
Forum Rules:

Copyright © 19992020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.
Website powered by rwasa.