Tuesday, October 15, 2013

GET YOUR HANDS OFF MY STUFF!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

On this post we're going to talk more into privacy of the separate procedures. you know count and termindex? Well raisetothepower is the only procedure that uses count and doit and determineoperations are the only ones that use termindex. So why should they be in global scope (.data)? the answer is they shouldn't. So how are we going to make count invisible to doit and determineoperations but not to raisetothepower,and termindex invisible to raisetothepower but not to doit and determineoperations? Well 1st I need to introduce you to the 3 sections of ram: .code,data,and stack. But which one will we need? The answer is................
Yes stack is the one that can help us because we have esp,and ebp and we have tons of room in stack. The way we can access the stack is with ebp because that's what it's made for accessing into other sections of memory. So it's simple to access the stack all we need to do is say mov ebp,esp. But wait little red flag coming up if it's at the top of the stack it's important right so we should swim through it not over it (hee hee hee Nemo) and how we do that is we say sub esp,4 (each spot in ram is 4 bytes apart and 4 bytes is just enough room so this is our new top of the stack.) because we don't wanna mess with those jellyfish (heehee) or stomp on our ret value that will also be held in ram because it will be located at the top of the stack and if we don't update the stack pointer to a different location the instruction pointer (eip) will have the value of termindex and eip will take us off into neverneverland when we return, thus it's critical that we update the stack pointer because each procedure should be able to assume that the stack pointer is pointing to the top of the stack,so now that we have a spot that we can do whatever we want we can get going on using it as a spot for count.
remember we're trying to make each procedure as independent from each other as possible

Now we gotta talk about what I call the "Every man for himself"rule. It's basically stating that the procedures must be as independent from each other as possible so we need to make sure that doit, determineoperations, & raiseToThePower are getting as little help from each other as possible which means that they need to manage by themselves. So how are we going to do that?Well we need to talk about the "Leave no trace"rule. It means that if raiseToThePower wants to use the stack pointer and ebp sure it can use them BUT only as long as it sets them back to where they originally were.

Here's my code:

.model flat, c
.stack 100h
 total dword 0
 bases dword      2,3,6,9,4,2
 exponents dword  7,5,3,2,3,3
 operations dword 0,0,1,2,0,2
doit proc
;    ecx will get the base ebx will get the exponent and the program will run all the way through
; when we get back to doit we will start over again

  sub esp,4
  mov ebp,esp
  mov dword ptr [ebp],0
  ; inc may be in the wrong spot,or be inc the wrong thing because it should be inc termindex
  mov eax,4
  mul dword ptr [ebp]
  mov ebx,eax
  mov ecx,bases[ebx]
  mov ebx,exponents[ebx]
  call raiseToThePower
  call determineOperations
  inc dword ptr [ebp]                                                                                
  cmp dword ptr [ebp],6
  jl restart
  add esp,4
doit endp
;     termindex
;    total will have the current total
determineOperations proc
  mov ebx,eax
  mov eax,4
  mul dword ptr [ebp]

  cmp operations[eax],0
  je Addition
  cmp operations[eax],1
  je Subtraction
  cmp operations[eax],2
  je Multiplication

 mov eax,total
 mul ebx
 mov total,eax
 sub total,ebx
 add total,ebx
determineOperations endp

; inputs:
; ecx -> current base
;   ebx ->current exponent
; output:
;   eax ->answer to the power
raiseToThePower proc
  push dword ptr [ebp]
  sub esp,4
  mov ebp,esp
  mov dword ptr [ebp],0
  mov eax,1
  cmp dword ptr [ebp],ebx
  je weAreDone
  mul ecx
  inc dword ptr [ebp]
  jmp again
  add esp,4
  pop dword ptr [ebp]
raiseToThePower endp


So what's different about this program and other programs that we've done so far? Well at the top of raiseToThePower we have a push to save the value of ebp to the top of the stack and a pop at the end to restore it and lots of various changes that I would advise you to look for. WELL that has been the end of this post so good-bye now see ya!

No comments:

Post a Comment