Saturday, August 17, 2013

The powers

On this post I'm going to show you a program that I did with my dad to do a little adding and multiplication. This program is supposed to do this problem: 31 + 32 + 33 + ... 3n

In this program ebx tracks the SUM,and ecx tracks the base. Eax serves as a general purpose multiply. This program was challenging for me because I didn't know very much about powers.

32 = 3 x 3
33 = 3 x 3 x 3
34 = 3 x 3 x 3 x 3
35 = 3 x 3 x 3 x 3 x 3
36 = 3 x 3 x 3 x 3 x 3 x 3   

32 is a short way of saying 3 x 3,33 is a short way of saying 3 x 3 x 3 etc. SUM is the total of adding the 2 powers together. If you go high enough going up power by power you will get HUGE amounts of overflow so much in fact the amount in R.A.M is nothing compared to the real answer. Sometimes when you are doing a big enough problem you have to use R.A.M because edx doesn't cooperate because when you do multiplication it tells you what you didn't use. So you gotta use R.A.M instead. If you don't know what ram is you should read my earlier blogs.

Now I'm going to tell you which registers are doing what while this is all going on. R.A.M tracks the power of 3 which is how many 3's are being multiplied. Ecx is tracking the base which is 3. Ebx is tracking the SUM or total. And as for edx it is not doing anything because as I said before edx wouldn't work. As for R.A.M it takes over for edx as the power of 3.

Here is a chart I made to track everything for each iteration:

As you can see I have shown you what is in R.A.M,and ebx,and I have written down what ecx,eax,and edx are doing.

And here's the program.

.model flat, c
.stack 100h
powerof3 dword 1
count dword 0
doit proc
mov ecx, 3
mov ebx, 1

; Raise to the next power of 3
mov eax, powerof3
mul ecx
mov powerof3,eax
inc count

; Multiplying the power of 3 and the sum
mov eax,powerof3
mul ebx
mov ebx,eax

; Raising to the next power of 3
mov eax,powerof3
mul ecx
mov powerof3,eax
inc count

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

; Repeat if neccesary
cmp count,5
jl again

No comments:

Post a Comment