Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Introduction to PowerPC assembly.
- Chapter 2: General concepts
- Before we get into the 'cool' stuff, assembly directives, macros and whatnot, we must cover some general concepts.
- First, I want to underline the difference between what is seen as data and instructions.
- To generalise the concept even further, an instruction is defined so because its address will be in the PC (Program Counter [SRR0 for us])
- Anything that's not 'normally' going to be in SRR0, is data.
- 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!
- 2.1 : Data types and directives
- 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)
- Let's see some of them:
- .word 0x12345678 -> Defines a value of type .word. (Note, you HAVE to make sure this doesn't get executed for aforementioned reasons)
- .ascii "abcdefg" -> Defines a 'string'
- .int 0x1234 -> Defines an Integer
- .float -0.5 -> float
- 2.2 : Basic instructions
- Let's revise all the instructions we've seen:
- Read/Store instructions:
- lbz rA,b(rC)
- lhz rA,b(rC)
- lwz rA,b(rC)
- stb rA,b(rC)
- sth rA,b(rC)
- stw rA,b(rC)
- Load immediate:
- li rA,X
- Arithmetic - Logic instructions (Only read if you are familiar with what they mean):
- or rA,rB,rC -> 'OR's (Logical addition) the contents of rB and rC, and puts the result into rA
- ori rA,rB,X -> 'OR's the contents of rB with X, and puts the result into rA
- and rA,rB,rC -> 'AND's (Logical multiplication) the contents of rB and rC, and puts the result into rA
- andi rA,rB,X -> 'AND's the contents of rB with X, and puts the result into rA
- 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]
- xori rA,rB,X -> 'XOR's the contents of rB with X, and puts the result into rA
- ---
- In general, if you don't know what arithmetic logic instructions or operations are, it's advisable to use add instructions.
- But there's a catch!
- 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.
- tl;dr, easy explaination: Value range in ori >APPEARS< to be larger ( 0 - FFFF )
- 2.3 : Conditions (Basic concepts)
- Conditons is what can make a code more..dynamic.
- 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.)
- 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).
- The pseudo code, algorithm or however you wish to call it, would be as follows:
- 1. Load sin_val
- 2. if sin_val == 1
- a. set it_val == 5
- 3. else
- ;;Do nothing
- Let's turn that into an assembly code, shall we?
- ----------------------------------------------------------------------------------------------------------------------------------------------------------------
- lis r14,0x81000000 ; Remember, everytime we want to load a value from a memory location, we have to initialize the 'pointer' to the value
- lbz r14,0(r14) ;Value contained in the 'pointer' that was in r14 before (0x81000000) is now in r14 [I like confusing people]
- cmpwi r14, 1
- bne do_nothing
- lis r14,0x80000000
- li r15,0x05
- 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.
- do_nothing:
- ----------------------------------------------------------------------------------------------------------------------------------------------------------------
- 2.4 : Condition branches
- Once we compare using one of the compare instructions:
- cmpwi rA,X ; Compares rA to X
- cmpw rA,rB ; Compares rA to rB
- all the processor does is substract the second argument to the first, say
- cmpwi r1,0x2 ; Let's assume that r1 holds a value of 0x01
- the result of the substraction would be..That's right, 0x01.
- After the processor has calculated the result, it will 'do' something depending on your 'request':
- bge label1 ;If the second argument is bigger than the first, go to 'label1'
- Generally speaking, the basic instruction is b, without conditions
- and the conditions you can add are:
- le - less than or equal to
- lt - less than
- ge - greater than or equal to
- gt - greater than
- ne - not equal
- eq - equal
- 2.5 : Loop (Intermediate Concepts)
- What can we do with conditions? We can make a timer! ..Somewhat.
- 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!
- Once you have completed the assigment, you can skype me your code and I will review it for you.
- Send me a message on pastebin if you're someone I do not know :)
Advertisement
Add Comment
Please, Sign In to add comment