Post: PowerPC Tutorials: Lesson 1- load a value in a register
02-17-2014, 04:04 PM #1
(adsbygoogle = window.adsbygoogle || []).push({}); In this tutorial i will show you how to set a register.

first, what is a register ?
A register is a variable which contains a value.

there is 32 registers


r0 - r31, we count the register 0 too so its 32 registers in total, there is also some other registers but i won't talk about them for now.
In PowerPC we will notice them like this: r0, r1, r2, r3 etc
    
r0 = old link register, i won't talk about it for now
r1 = stack pointer, i won't talk about it for now
r2 = table of contents pointer, i won't talk about it for now

r3 = it can be used as the first argument of a function OR as the returned value from a function
r4–r10 = Used as the 2nd to the 8th arguments of a function.
r11 = used for the PS3 Syscalls
r12 = local var
r13-r31 = global var, we use them to store a value and being able to use it later on another function.


Don't worry we won't use them all, we will focus on r3, r4 and r5 for now :P



so in C# we can load values in variables like this:
    
int a = 10;
string b = "Bad Luck Brian";
byte[] c = new byte[] {0x05, 0x02, 0xFF};

in powerpc we use li and lis

registers are 4 bytes, 11 22 33 44
try to imagine them like this: XX XX YY YY



li - load imediate, it sets a register in the YY YY region

example: li r3, 0x15 ::: r3 = 00 00 00 15 (0x15 is the same as 0x0015)
    
li r3, 0x1500 ::: r3 = 00 00 15 00


now lis is the same but with the XX XX region
    
lis r3, 0x26 ::: r3 = 00 26 00 00
lis r3, 0x2600 ::: r3 = 26 00 00 00
lis r3, 0x216 ::: r3 = 02 16 00 00


Now we can set 2 bytes in a register, either the XX XX or the YY YY Region. We need to include some maths to set 4 bytes, its simple.

we will use addic and addis, addic adds something to the YY YY part and addis adds something to the XX XX Part.

we use them like this:
    
addic RESULT, REGISTER, VALUE


Result = register containing the result of the addition
Register = register to be added to the value
Value = value to be added to the register

in english it would translater to this: Result = Register + Value

lets use some examples:
    
li r3, 0x15
addic r3, r3, 0x01


r3 is now equal to: 0x16 (0x15 + 0x01)

now lets set r3 to: 0xFCA2801

first of all, lets put it in a XX XX YY YY format.

we start from the RIGHT separating them bytes after bytes
    
FCA2801
FCA28 01
FCA 28 01
F CA 28 01


we add a zero before the last value ('F'Winky Winky to reach the XX XX YY YY format
    
*************
*XX XX YY YY*
*0F CA 28 01*
*************

now i will use lis to set the XX XX part and addic to set the YY YY Part
    
lis r3, 0x0FCA //r3 = 0F CA 00 00
addic r3, r3, 0x2801 //r3 = 0F CA 00 00 + 00 00 28 01 (0F CA 28 01)

lets do another example:

lets set r3 to: 0x1d60c

set it to the XX XX YY YY format to make it easier
    
1d60c
1d6 0c
1 d6 0c


add the zeros before it to reach XX XX YY YY
    
01 d6 0c
00 01 d6 0c

*************
*XX XX YY YY*
*00 01 d6 0c*
*************


now i will use lis to set the XX XX part and addic to set the YY YY Part
    
lis r3, 0x0001 //r3 = 00 01 00 00
addic r3, r3, 0xD60C //r3 = 00 01 00 00 + 00 00 D6 0C (00 01 D6 0C )

r3 = 0x0001D60c or 0x1D60c


its simple ! Smile
in the next lesson i will teach you how to write in the memory :P

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo