Wednesday, August 21, 2013

What do you do if you wanna be true?

In my previous post about powers there was an error because we could only do even powers because we would raise the power twice per loop. Well in this post I'm going to show you a program that you can do any power you want because it only raises the power of 3 once. (if you haven't read my post called the powers you should read it before you read this post.) The way this will work is you divide count by 2 and you don't need to worry about what the answer will be because what we really care about is the remainder which will be in edx.  We compare edx to 1 and if it is = to 1 we je to the multiplication and if it is less than we jl to the addition part. And I advise you that one teeny tiny mistake can throw off the whole program but that's no reason to race through the program and think there's a problem when there's really nothing wrong. (that's the mistake I made.) There are a few ways you can see a fake problem, here I'll list them for you: looking at the wrong thing and thinking it's something else,and getting mixed up with names. And there's one more thing dividing is just like multiplying because you can't use immediate values and you can't type the name of the implied register (eax) in the instruction.



.model flat, c
.stack 100h
.data
powerof3 dword 1
count dword 0
two dword 2
.code
doit proc
mov ecx, 3
mov ebx, 1
again:
call calculateNextPower
mov eax,count
xor edx
div two
cmp edx,1
je multiplication
jl addition

multiplication:
; Multiplying the power of 3 and the sum
mov eax,powerof3
mul ebx
mov ebx,eax
jmp hop


addition:
; adding the sum and the power of 3
add ebx,powerof3


hop:
; Repeat if neccesary
cmp count,5
jl again

ret
doit endp

calculateNextPower proc
 ; Raising to the next power of 3
mov eax,powerof3
mul ecx
mov powerof3,eax
inc count
ret
calculatenextpower endp

end

No comments:

Post a Comment