Geeh98

Guide to Assembly on Wii : 3

Nov 18th, 2015
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. Introduction to PowerPC assembly.
  2.  
  3. Chapter 3: All about functions
  4.  
  5. 3.1: The glorious flower pot
  6.  
  7. The stack is a part of the memory dedicated to storing data temporarily. Its model is based on LIFO (Last In First Out). Conceptually speaking it is similiar to a flower pot:
  8. Let's say we have 5 layers of dirt having 5 different colours, and each containing a gem of the same colour: Red, Purple, Blue, White, and Black, and we want to plant our lovely flower in a pot, but we want to organise the dirt in alphabetical order (Because why the fuck not?). The list would be: Black, Blue, Purple, Red, and White. Now, we want to remove the Blue gem, polish it and put it back. In order to do so, we would have to remove the White, Red, and Purple dirt beforehand, then 'edit' the Blue dirt, and put the dirt back in again. That's how the stack works, the program is nothing but a flow of functions, each of them might or might not save to the stack a portion of data that the function executing before needed. As far as Mario Kart's stack is concerned, it's somewhere around 0x80300000 - 0x80390000, and the stack pointer register (The register in which the position of first blank cell available of the stack is stored) is r1 or r3 (I can't remember anymore).
  9.  
  10. This leads us to:
  11.  
  12. 3.2: Functions
  13.  
  14. I like to start with concepts and gain experience first then have all the definitions pointed out.
  15. We made a function that calculates how many times the address is executed, and we want to debug to see how many times another instruction is executed. What you'd normally do is simply inject code in the instruction you want to check to check the timer. But let's say we want to debug 50 addresses at the same time. Not pleasant, is it?
  16. What if we, instead, branch to the part of the code that requires no input and gives out a number that is incremented each time: It would save us precious code lines and it would be easily done with one branch instruction.
  17. However, since we have 50 addresses requesting how many times they're executed, we need a temporary place that is dynamic each time and allows us not to 'pick up' the same counter other instruction used: What else if not the stack?
  18.  
  19. Assuming that the stack pointer is located in r1, the code would look like this:
  20.  
  21. stwu r1,-128(r1) ;Storing the beginning of our data in the stack, -128 tells us how 'far back' it is stored, therefore how much space we will use
  22. stmw r3,8(r1) ;Storing every register from r3 to r31 in the stack
  23.  
  24. ;In order to handle 50 different addresses we'd need an array, too complex for now, let's assume each time we call it, r4 already holds our counter after this comment
  25. addi r4,r4,1
  26. ;store in the array
  27.  
  28. mr r0,r4 ;Our 'output' value will be stored in r0 for the calling function to check (if anything was in r0, you'd move it in any register from r3 to r31 to be preserved)
  29.  
  30. lmw r3,8(r1) ;Load all the registers back
  31. addi r1,r1,128 ;Re-add the amount of data we used
  32.  
  33. Very convenient isn't it?
  34.  
  35. A function by mathematical definition, is "a special relationship where each input has a single output. It is often written as "f(x)" where x is the input value. Example: f(x) = x/2 ("f of x is x divided by 2") is a function, because each input "x" has a single output "x/2": • f(2) = 1." . Functions in ICT can be defined as that, but not always an input or output is given (More on that soon).
  36.  
  37. That concludes this chapter (short but complex), and your assignment is to make a code function that preserves any register from r14 to r31, accepts as input 2 integers, in r14 and r15, multiplies them (using addition and loop [pay attention to efficiency!], saves the output in r13.
Advertisement
Add Comment
Please, Sign In to add comment