Geeh98

Guide to Assembly on Wii : 2

Oct 9th, 2015
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. Introduction to PowerPC assembly.
  2.  
  3. Chapter 2: General concepts
  4.  
  5. Before we get into the 'cool' stuff, assembly directives, macros and whatnot, we must cover some general concepts.
  6. First, I want to underline the difference between what is seen as data and instructions.
  7.  
  8. To generalise the concept even further, an instruction is defined so because its address will be in the PC (Program Counter [SRR0 for us])
  9. Anything that's not 'normally' going to be in SRR0, is data.
  10. You often see people fill a C2 code with 0s, and the reason why that doesn't work..is because the processor will try to 'execute' a piece of data!
  11.  
  12. 2.1 : Data types and directives
  13.  
  14. We have already covered data types in the previous chapter(s), but there are quite a number of assembly directives, which are there to make your life easier (I personally rarely use them)
  15. Let's see some of them:
  16.  
  17. .word 0x12345678 -> Defines a value of type .word. (Note, you HAVE to make sure this doesn't get executed for aforementioned reasons)
  18. .ascii "abcdefg" -> Defines a 'string'
  19. .int 0x1234 -> Defines an Integer
  20. .float -0.5 -> float
  21.  
  22. 2.2 : Basic instructions
  23.  
  24. Let's revise all the instructions we've seen:
  25.  
  26. Read/Store instructions:
  27.  
  28. lbz rA,b(rC)
  29. lhz rA,b(rC)
  30. lwz rA,b(rC)
  31.  
  32. stb rA,b(rC)
  33. sth rA,b(rC)
  34. stw rA,b(rC)
  35.  
  36. Load immediate:
  37.  
  38. li rA,X
  39.  
  40. Arithmetic - Logic instructions (Only read if you are familiar with what they mean):
  41.  
  42. or rA,rB,rC -> 'OR's (Logical addition) the contents of rB and rC, and puts the result into rA
  43. ori rA,rB,X -> 'OR's the contents of rB with X, and puts the result into rA
  44.  
  45. and rA,rB,rC -> 'AND's (Logical multiplication) the contents of rB and rC, and puts the result into rA
  46. andi rA,rB,X -> 'AND's the contents of rB with X, and puts the result into rA
  47.  
  48. xor rA,rB,rC -> 'XOR's (eXclusive OR) the contents of rB and rC, and puts the result into rA (For instance 0110 XOR 0111 = 0001) [Binary]
  49. xori rA,rB,X -> 'XOR's the contents of rB with X, and puts the result into rA
  50.  
  51. ---
  52.  
  53. In general, if you don't know what arithmetic logic instructions or operations are, it's advisable to use add instructions.
  54. But there's a catch!
  55. add instructions allow you to specify a 16 bit argument (SIGNED short int), whilst using oris will allow you to do things like 'ori r0,r0,0xFFFF'. It can come pretty handy sometimes.
  56.  
  57. tl;dr, easy explaination: Value range in ori >APPEARS< to be larger ( 0 - FFFF )
  58.  
  59.  
  60.  
  61. 2.3 : Conditions (Basic concepts)
  62.  
  63. Conditons is what can make a code more..dynamic.
  64. Let's say we have a value in 0x81000000 that changes with time in a sinusoidal manner (To make it simpler, let's say it's a signed int that changes from -1 to 0, from 0 to 1 and from 1 to 0, and from 0 to -1, and so on.)
  65. and we want to change our item to 'star' everytime the value hits 1 (For the sake of this tutorial let's put the item value to 0x80001234).
  66.  
  67. The pseudo code, algorithm or however you wish to call it, would be as follows:
  68.  
  69. 1. Load sin_val
  70. 2. if sin_val == 1
  71. a. set it_val == 5
  72. 3. else
  73. ;;Do nothing
  74.  
  75.  
  76.  
  77. Let's turn that into an assembly code, shall we?
  78.  
  79. ----------------------------------------------------------------------------------------------------------------------------------------------------------------
  80. lis r14,0x81000000 ; Remember, everytime we want to load a value from a memory location, we have to initialize the 'pointer' to the value
  81. lbz r14,0(r14) ;Value contained in the 'pointer' that was in r14 before (0x81000000) is now in r14 [I like confusing people]
  82. cmpwi r14, 1
  83. bne do_nothing
  84.  
  85. lis r14,0x80000000
  86. li r15,0x05
  87. stb r15,0x1234(r14) ; The pointer in r14 is added with 0x1234, so the new 'temporary' pointer will become 0x80001234, to which we store the value.
  88.  
  89. do_nothing:
  90. ----------------------------------------------------------------------------------------------------------------------------------------------------------------
  91.  
  92. 2.4 : Condition branches
  93.  
  94. Once we compare using one of the compare instructions:
  95. cmpwi rA,X ; Compares rA to X
  96. cmpw rA,rB ; Compares rA to rB
  97.  
  98. all the processor does is substract the second argument to the first, say
  99. cmpwi r1,0x2 ; Let's assume that r1 holds a value of 0x01
  100. the result of the substraction would be..That's right, 0x01.
  101. After the processor has calculated the result, it will 'do' something depending on your 'request':
  102. bge label1 ;If the second argument is bigger than the first, go to 'label1'
  103.  
  104. Generally speaking, the basic instruction is b, without conditions
  105. and the conditions you can add are:
  106. le - less than or equal to
  107. lt - less than
  108. ge - greater than or equal to
  109. gt - greater than
  110. ne - not equal
  111. eq - equal
  112.  
  113. 2.5 : Loop (Intermediate Concepts)
  114.  
  115. What can we do with conditions? We can make a timer! ..Somewhat.
  116.  
  117. Everytime the processor will execute our code, it will add 1 to our 'timer', and we want to reset the timer once the timer reaches 0x7F. This is an assignment, work slave!
  118.  
  119. Once you have completed the assigment, you can skype me your code and I will review it for you.
  120. Send me a message on pastebin if you're someone I do not know :)
Advertisement
Add Comment
Please, Sign In to add comment