https://wiibrew.org/w/api.php?action=feedcontributions&user=KiraW&feedformat=atomWiiBrew - User contributions [en]2022-01-24T00:41:51ZUser contributionsMediaWiki 1.34.0https://wiibrew.org/w/index.php?title=Assembler_Tutorial&diff=104884Assembler Tutorial2016-11-12T02:55:34Z<p>KiraW: Correcting typo for mulli</p>
<hr />
<div>==Introduction==<br />
This introduction for PowerPC assembler assumes that you are somewhat familiar with the Intel assembler. It is not written as a tutorial for beginners in assembly programming. Hopefully it is possible to understand this tutorial if you just have programed in C before. This tutorial will allow you to write applications in PowerPC assembler. Disassembling compiled code is not covered, however, knowing assembler is a prerequisite to disassemble code.<br />
<br />
<br />
The PowerPC is a RISC (Reduced Instruction Set Computing) processor architecture. PowerPC is an acronym which shall stand for Performance Optimization With Enhanced RISC / Performance Chip or Power Performance Computing. The specification for it was released in 1993 and is a 64-bit specification with a 32-bit subset. Almost all PowerPC processors are 32-bit now but feature a 64-bit data bus.<br />
<br />
The PowerPC was developed jointly by Apple, IBM and Motorola (now named Freescale). There are many different PowerPC processors available. Apple has been using the PowerPC in the Macintosh systems, IBM is using it in its RS/6000 and pSeries computers und Nintendo used it in its Gamecube and in Wii playstations. There are many embedded applications using the PowerPC.<br />
<br />
The PowerPC is a Superscalar microprocessor which means it has separate execution units. This are an integer unit, a floating-point unit, a branching unit, and even more depending on the processor type. These units can execute instructions in parallel within one clock cycle.<br />
<br />
<br />
== The registers ==<br />
<br />
The PowerPC has many more registers than the Intel processors and these are named differently. All registers are 4 bytes or 32bits long on the 32-bit versions of the PowerPC. There are 32 (0-31) General Purpose Registers (GPRs or rX), 32 Floating point registers (FPRs or fX) and special purpose registers (SPRs) like the program counter PC or IAR (instruction address register). This keeps track which instruction needs to be executed next. There is a link register (LR) which can hold the address of a procedure for branch instructions, the condition register (CR) which has eight (0-7) 4 bit fields holding the result of e.g. a compare instruction. The count register for loops is called CTR. XER is the fixed-point exception register. FPSCR is the floating point status and control register.<br />
<br />
On the PowerPC you cannot move data from one memory address to another. You have to read the data into a register first and then store the contents of the register at the destination address in memory. This design shall allow the processor to operate more efficiently.<br />
<br />
The PowerPC uses the big-endian format to store data in memory. The most significant byte (MSB) value is stored at the memory location with the lowest address. So the bit numbering is reversed compared to an Intel processor.<br />
<br />
<br />
== Variables ==<br />
<br />
The 32-bit version of the PowerPC supports the following data sizes:<br />
<tt><br />
:Byte - 8 bits<br />
:Halfword - 16 bits<br />
:Word - 32 bits<br />
</tt><br />
<br />
An integer value of 12 can also be specified as 0x0C in hexadecimal or01100 in binary.<br />
<br />
<br />
Variables are defined either in the data section or in the bss section which takes uninitialised data only.<br />
<br />
Here are examples how to define variables. The name of the variable is always set as a label followed by a colon.<br />
<tt><br />
:bytevar: .byte 0 #length of one byte - init zero<br />
:shortvar: .short 0 #length of two byte - init zero<br />
:wordvar: .long 0 #length of four byte - init zero<br />
<br /><br />
:fivebytevar: .byte 11,12,13,14,15 #an array of five variables of one byte each<br />
:endof_fivebytevar: #specifies the address immediately following the array<br />
<br /><br />
:stringvar: .string "Hello\n" #string variable - init to "Hello" plus newline<br />
:: .size stringvarlen, .-stringvar #length of stringvar<br />
</tt><br />
<br />
<br />
== Constants ==<br />
<br />
The AS assembler allows to define constants. These will be replaced by the assembler before assembling the code.<br />
<br />
Example:<br />
<br />
.set GPR0,0<br />
<br />
This will define the constant "GPR0" having the value zero. This will replace all occurences of GPR0 in the code by the number zero. This can enhance the readability of the code, since the registers are specified as numbers just like immediate values in the instructions. So using constants e.g. the instruction addi 0,0,0 can be written as addi GPR0,GPR0,0 in the code. Without defining constants the assembler will also accept addi %r0,%r0,0.<br />
<br />
<br />
== Instructions and Mnemonics ==<br />
<br />
Assembler instructions for Intel processors have up to two parameters separated by a comma. Typically the first parameter is modified with the second.<br />
<br />
The AS assembler will do this for Intel processors in the opposite direction compared with MASM, TASM etc. For the PowerPC, however, the first operand is used as the destination register and there can be up to five parameters separated by commas.<br />
<br />
The PowerPC uses fixed-length 32-bit instructions. As an example for an addi instruction this 32-bit integer is divided in the following fields:<br />
<br />
<tt><br />
:Opcode: 6 bits<br />
:Source register: 5 bits<br />
:Destination register: 5 bits<br />
:Immediate value: 16 bits<br />
</tt><br />
<br />
So to fill a 32bit register with an immediate value you have to use two instructions moving 16 bits each. In case of a 64bit processor you need even more instructions since you have to shift the bits here too.<br />
<br />
Mnemonics are specializations of a more general instruction. They are used as simplified instructions for easier coding of assembly language programs. They are defined for frequently used instructions. A mnemonic may have two parameters and this will be converted by the assembler to an instruction which may require three or more parameters. Samples of mnemonics can be found among the described instructions below.<br />
<br />
The available instructions for the PowerPC can be grouped as follows:<br />
<br />
# Integer instructions<br />
# Floating point instructions<br />
# Load and store instructions<br />
# Branch and flow control instructions<br />
# Various instructions<br />
<br />
The most common instructions of each group will be discussed here.<br />
<br />
<br />
=== Integer Instructions ===<br />
<br />
<br />
==== Integer Arithmetic Instructions ====<br />
<br />
===== ADD =====<br />
<br />
This instructions has several variants:<br />
<br />
1. '''ADD'''<br />
<br />
Syntax: add rD,rA,rB<br />
<br />
This command adds two registers (rA and rB) and puts the result into the register rD (destination).<br />
<br />
Example: add 3,6,4<br />
<br />
In this example GPR6 and GPR4 are added and the result is put into GPR3.<br />
<br />
Example: add 3,6,3<br />
<br />
In this example GPR6 and GPR3 are added and the result is put into GPR3.<br />
<br />
Example: add 3,0,4<br />
<br />
Here the value in GPR4 is moved into GPR3 - like an Intel move instruction. If the second parameter is a zero this does not mean a GPR0 but the value zero. This is the case for several instructions.<br />
<br />
<br />
2. '''ADDI - Add Immediate'''<br />
<br />
Syntax: addi rD,rA,SIMM<br />
<br />
This command adds a 16-bit signed integer (SIMM) to register rA and puts the result into the register rD (destination).<br />
<br />
Example: addi 3,6,4<br />
<br />
In this example GPR6 and the value 4 are added and the result is put into GPR3.<br />
<br />
As you can see the registers are specified as a number and the integer is specified as a number. To improve the readability of the code you can define constants for the registers, e.g.: <br />
<br />
.set r0,0; .set r1,1; .set r2,2; .set r3,3; .set r4,4; .set r5,5; .set r6,6; .....<br />
<br />
Then the above command can be written as: addi r3,r6,4<br />
<br />
You can also use the addi command as a move instruction:<br />
<br />
addi 3,0,4<br />
<br />
This sets GPR3 to the value 4. If the second parameter is a zero here this does not mean a GPR0 but the value zero.<br />
<br />
<br />
3. '''ADDIS - Add Immediate Shifted'''<br />
<br />
Syntax: addis rD,rA,SIMM<br />
<br />
This command is used to add a 16-bit immediate value to the upper 16 bits of a 32bit register. It adds a 16-bit signed integer (SIMM) to register rA, then shifts left register rA by 16 bits and then puts the result into the register rD (destination). The lower 16 bits are cleared by this command. So to fill a 32 bit register with an immediate 32-bit value you first have to use addis to fill the upper 16 bits and then addi to fill the lower 16 bits. Otherwise the lower 16 bits would be cleared again by the addis command.<br />
<br />
Example: addis 3,3,4<br />
<br />
In this example GPR3 and the value 4 are added, then GPR3 is shifted 16 bits to the left and the result is put into GPR3. This will then contain 0x00040000 in hexadecimal. If you then execute an addi 3,3,4 command GPR3 will contain 0x00040004 in hexadecimal.<br />
<br />
To move a pointer to an address of a variable or function into a register there are the @ha/@h and @l modifiers available. If you append these to the variable name you get the lower (@l) 16 bit of the absolute 32-bit address of the variable and with @ha you get the higher 16 bit of the absolute 32-bit address. <br />
<br />
Example:<br />
:addis 3,0,hello@ha<br />
:addi 3,0,hello@l<br />
<br />
With these two instructions the absolute 32-bit address of the string variable hello is moved into the GPR 3 register.<br />
<br />
Instead of addis/addi the mnemonics lis/la are often used. These are explained below.<br />
<br />
<br />
4. '''ADD. - ADD with CR Update'''<br />
<br />
Contrary to the Intel processors the ADD instruction will not modify any flags. To achieve this you have to apped a dot to the instruction. So add. will set the CR bits 0-3 (CR0) in the CR register. These bits will then reflect a signed comparison of the result to zero. In effect the dot adds a <br />
cmpwi rD,0<br />
instruction to the ADD instruction. A dot can be added to many PowerPC instructions.<br />
<br />
<br />
5. '''Mnemonics for the ADD instruction'''<br />
<br />
<br />
The following mnemonics are converted into ADD instruction by the assembler:<br />
<br />
<br />
LI - Load Immediate<br />
<br />
Syntax: li rD,value<br />
<br />
This is equivalent to addi rD,0,value<br />
<br />
Example: li 3,100<br />
<br />
Sets GPR3 to 100 and clears the higher 16 bits.<br />
<br />
<br />
LIS - Load Immediate Shifted<br />
<br />
Syntax: lis rD,value<br />
<br />
This is equivalent to addis rD,0,value<br />
<br />
Example: lis 3,100<br />
<br />
Sets higher 16 bits of GPR3 to 100 and clears the lower 16 bits.<br />
<br />
<br />
LA - Load Address<br />
<br />
Syntax: la rD,d(rA)<br />
<br />
This is equivalent to addi rD,rA,d<br />
<br />
Example: la 3,100(9)<br />
<br />
Adds 100 to the address in GPR9 and loads the result in GPR3.<br />
<br />
<br />
As a side note this is also equivalent: <br />
<br />
li rD,value = ori rA,0,UIMM (UIMM = unsigned integer value)<br />
<br />
So OR immediate can be used to load an immediate too as long as the value is unsigned.<br />
<br />
To load an immediate 32-bit value in a register you can use:<br />
:lis 3,100<br />
:ori 3,0,200<br />
This loads 100 into the higher 16 bits and 200 into the lower 16 bits.<br />
<br />
<br />
===== SUBF - Subtract From =====<br />
<br />
<br />
1. '''SUBF'''<br />
<br />
Syntax: subf rD,rA,rB<br />
<br />
Example: subf 3,4,5<br />
<br />
Similar to the ADD instruction SUBF will subtract GPR4 from GPR5 and place the result in GPR3.<br />
<br />
<br />
2. '''Subfic - Subtract from Immediate Carrying'''<br />
<br />
Syntax: subfic rD,rA,SIMM<br />
<br />
Example: subfic 3,4,5<br />
<br />
This will subtract GPR4 from signed integer value 5 and place the result in GPR3. CRO is modified.<br />
<br />
<br />
===== MUL - Multiply =====<br />
<br />
Multiplying two 32-bit values will often result in a 64-bit value. So there are separate instructions to put the 64-bit result into two 32-bit registers:<br />
<br />
1. '''MULLW - Multiply Low Word'''<br />
<br />
Syntax: mullw rD,rA,rB<br />
<br />
Example: mullw 3,4,5<br />
<br />
This will multiply the contents of GPR4 and GPR5 and place the lower 32 bits of the result in GPR3.<br />
<br />
<br />
2. '''MULLH - Multiply High Word'''<br />
<br />
Syntax: mullh rD,rA,rB<br />
<br />
Example: mullh 6,4,5<br />
<br />
This will multiply the contents of GPR4 and GPR5 and place the higher 32 bits of the result in GPR6.<br />
<br />
<br />
3. '''MULLI - Multiply Low Immediate'''<br />
<br />
Syntax: mulli rD,rA,SIMM<br />
<br />
Example: mulli 3,4,5<br />
<br />
This will multiply the contents of GPR4 with the integer 5 and place the lower 32 bits of the result in GPR3. So the higher 32 bits - if any - are lost.<br />
<br />
<br />
===== DIV - Divide =====<br />
<br />
'''divw - Divide Word'''<br />
<br />
Syntax: divw rD,rA,rB<br />
<br />
Example: divw 3,4,5<br />
<br />
This will divide the contents of GPR4 with the contents of GPR5 and place the result in GPR3. The remainder is lost.<br />
<br />
Assembler programmers somehow design their tasks so that they only need to multiply and divide by powers of two. This way they can use the shift instructions instead of multiply and divide.<br />
<br />
<br />
==== Integer Compare und Logical Instructions ====<br />
<br />
1. '''CMP - Compare'''<br />
<br />
Syntax: cmp crfD,L,rA,rB<br />
<br />
Example: cmp 7,0,3,4<br />
<br />
This will compare the signed contents of the GPR3 and GPR4 registers and set the CR7 field of the CR register accordingly. The second parameter has to be set to zero for 32bit processors.<br />
<br />
If <nowiki>rA<rB then bit 0 of CR7 will be set. If rA>rB</nowiki> then bit 1 of CR7 will be set. If rA=rB then bit 2 of CR7 will be set.<br />
<br />
<br />
2. '''CMPI - Compare Immediate'''<br />
<br />
Syntax: cmpi crfD,L,rA,SIMM<br />
<br />
Example: cmpi 7,0,3,4<br />
<br />
This will compare the signed contents of the GPR3 register with the value 4 and set the CR7 field of the CR register accordingly. The second parameter has to be set to zero for 32bit processors.<br />
<br />
If rA<4 then bit 0 of CR7 will be set. If rA>4 then bit 1 of CR7 will be set. If rA=4 then bit 2 of CR7 will be set.<br />
<br />
<br />
3. '''Mnemonics for CMP'''<br />
<br />
Sometimes the following mnemonics are used:<br />
<br />
CMPWI - compare word immediate<br />
<br />
Syntax: cmpwi crD,rA,SIMM<br />
<br />
This is equivalent to cmpi crD,0,rA,SIMM<br />
<br />
CMPLWI - Compare Logical word immediate<br />
<br />
Syntax: cmplwi crD,rA,UIMM<br />
<br />
This is equivalent to cmpli crD,0,rA,UIMM (UIMM = unsigned integer value)<br />
<br />
<br />
===== OR =====<br />
<br />
1. '''OR'''<br />
<br />
Syntax: or rA,rS,rB<br />
<br />
Example: or 3,4,5<br />
<br />
This instruction will OR the contents of GPR4 and GPR5 and place the result in GPR3. The variant OR. (+dot) will update CR too.<br />
<br />
<br />
2. '''ORI - OR Immediate'''<br />
<br />
Syntax: ori rA,rS,UIMM<br />
<br />
Example: ori 3,4,5<br />
<br />
This instruction will OR the contents of GPR4 with the unsigned integer 5 and place the result in GPR3.<br />
<br />
ori 0,0,0 can be used as a NOP (no operation) instruction. This could be used e.g. as a breakpoint for a debugger.<br />
<br />
<br />
3. '''ORIS - OR Immediate Shifted'''<br />
<br />
Syntax: ori rA,rS,UIMM<br />
<br />
Example: oris 3,4,5<br />
<br />
This instruction will OR the upper 16 bits contained in GPR4 with the unsigned integer 5 and place the result in GPR3.<br />
<br />
<br />
4. '''Mnemonics for OR'''<br />
<br />
MR - move [to] register<br />
<br />
Syntax: mr rA,rS <br />
<br />
This is equivalent to: or rA,rS,rS<br />
<br />
Example: mr 31,1<br />
<br />
This will move the value in GPR1 to GPR31.<br />
<br />
<br />
===== AND =====<br />
<br />
1. '''AND'''<br />
<br />
Syntax: and rA,rS,rB<br />
<br />
Example: and 3,4,5<br />
<br />
This instruction will AND the contents of GPR4 and GPR5 and place the result in GPR3.<br />
<br />
<br />
2. '''ANDI. - AND Immediate'''<br />
<br />
Syntax: andi. rA,rS,UIMM<br />
<br />
Example: andi. 3,4,0b00000011<br />
<br />
This instruction will AND the contents of GPR4 with the unsigned integer 3 (binary 000011) and place the result in GPR3. Since this instruction ends with a dot, the CR register is updated. The higher 16 bits will be cleared. In effect all but the last two bits of GPR4 are cleared in this example.<br />
<br />
<br />
3. '''ANDIS. - AND Immediate Shifted'''<br />
<br />
Syntax: andis. rA,rS,UIMM<br />
<br />
Example: andis. 3,4,5<br />
<br />
This instruction will AND the upper 16 bits contained in GPR4 with the unsigned integer 5 and place the result in GPR3. Since this instruction ends with a dot, the CR register is updated. The lower 16 bits will be cleared.<br />
<br />
<br />
4. There are equivalent instructions for XOR, NAND etc.<br />
<br />
<br />
==== Integer shift and rotate instructions ====<br />
<br />
1. '''SLW - Shift left word'''<br />
<br />
Syntax: slw rA,rS,rB<br />
<br />
Example: slw 3,4,5<br />
<br />
The contents of the GPR4 register are shifted left by the value placed in the low-order six bits of the GPR5 register. The 32-bit result is placed in GPR3.<br />
<br />
<br />
2. '''SRW - Shift right word'''<br />
<br />
Syntax: srw rA,rS,rB<br />
<br />
Example: srw 3,4,5<br />
<br />
The contents of the GPR4 register are shifted right by the value placed in the low-order six bits of the GPR5 register. The 32-bit result is placed in GPR3.<br />
<br />
<br />
3. '''RLWINM - Rotate Left Word Immediate then AND with Mask'''<br />
<br />
Syntax: rlwinm rA,rS,SH,MB,ME<br />
<br />
Example: rlwinm 3,4,5,0,31<br />
<br />
Here the contents in GPR4 will be rotated left by 5 bits (the immediate value of 5 - parameter three) and the result placed into GPR3. After rotating GPR4 and before placing the result into GPR3 the value is ANDed with the mask specified in the last two parameters. The fourth parameter specifies the beginning of the 1-bits in the mask and the fifth parameter specifies the end of the 1-bits in the mask. In this example the begin is 0 and the end is 31. So all 32 bits are set in the AND mask. This causes no bits to be cleared by the mask. <br />
<br />
A rotate right can be done by specifying a value of 32-n as the third parameter. So<br />
<br />
rlwinm 3,4,31,0,31<br />
<br />
will rotate GPR4 right by one bit. This is not equal to a division by 2 since a bit may be moved into the sign bit by the rotation. Use the shift mnemonics for that which are described below.<br />
<br />
<br />
The PowerPC stores the data in big-endian format. So if you have a value of 31 which is0011111 and you want to clear the lower two bits you have to AND this with a mask of 0,29. Then bits 30 and 31 are set to zero in the mask and these bits will be cleared in the value of 31. The result will then be0011100 or 28.<br />
<br />
<br />
If the third parameter is a zero there is no rotation and this command is just used as an AND mask. This is often done by gcc since this allows to execute AND with an immediate 32-bit value.<br />
<br />
Example:<br />
<br />
li 3,0b11111111<br />
rlwinm 3,3,0,24,24 <br />
<br />
will clear all bits except bit 24 (big-endian format) in register GPR3. So register GPR3 will then contain 128 or10000000.<br />
<br />
<br />
4. '''Mnemonics for shift'''<br />
<br />
SLWI - Shift Left Word Immediate<br />
<br />
Syntax: slwi rD,rA,SIMM (SIMM<32) <br />
<br />
This is equivalent to: rlwinm rA,rS,n,0,31–n<br />
<br />
Example: slwi 3,4,5<br />
<br />
Shifts GPR4 left by 5 bits and places the result in GPR3. This is equal to multiplying GPR4 with 32 (2**5).<br />
<br />
<br />
SRWI - Shift Right Word Immediate<br />
<br />
Syntax: srwi rD,rA,SIMM (SIMM<32) <br />
<br />
This is equivalent to: rlwinm rA,rS,32 – n,n,31<br />
<br />
Example: srwi 3,4,5<br />
<br />
Shifts GPR4 right by 5 bits and places the result in GPR3. This is equal to dividing GPR4 by 32 (2**5).<br />
<br />
<br />
<br />
=== Floating point instructions ===<br />
<br />
<br />
1. '''FMR - Floating Move Register'''<br />
<br />
Syntax: fmr frD,frB<br />
<br />
Example: fmr 3,4<br />
<br />
The integer value in the FPR4 will be moved into the FPR3 (FPR=Floating point register)<br />
<br />
<br />
2. '''LFS - Load Floating-Point Single'''<br />
<br />
Syntax: lfs frD,d(rA)<br />
<br />
Example: lsd 3,0(4)<br />
<br />
Loads the word of data from the location in memory specified in GPR4 into floating-point register FPR3 and thereby converting it to floating-point double-precision.<br />
<br />
<br />
3. '''STFS - Store Floating-Point Single'''<br />
<br />
Syntax: stfs frS,d(rA)<br />
<br />
Example: stfs 3,0(4)<br />
<br />
Converts the contents of FPR3 to single-precision and stores it at the location in memory specified in GPR4.<br />
<br />
<br />
4. '''LFD- Load Floating-Point Double'''<br />
<br />
Syntax: lfd frD,d(rA)<br />
<br />
Example: lfd 3,0(4)<br />
<br />
Loads the doubleword of data from the location in memory specified in GPR4 into floating-point register FPR3.<br />
<br />
<br />
5. '''STFD- Store Floating-Point Double'''<br />
<br />
Syntax: stfd frD,d(rA)<br />
<br />
Example: stfd 3,0(4)<br />
<br />
Stores the doubleword of data in the floating-point register FPR3 at the location in memory specified in GPR4.<br />
<br />
<br />
6. '''MTFSF - Move to FPSCR Fields''' <br />
<br />
Syntax: mtfsf FM,frB<br />
<br />
Copies the contents of the floating-point register frB into the Floating-Point Status and Control Register (FPSCR) under the control of the field mask in FM.<br />
<br />
<br />
7. '''MTFSB1 - Move to FPSCR Bit 1'''<br />
<br />
Syntax: mtfsb1 crbD<br />
<br />
Example: mtfsb1 4<br />
<br />
Sets bit 4 of the FBSCR register to 1.<br />
<br />
<br />
=== Load and Store Instructions ===<br />
<br />
The PowerPC allows you to move data from register to memory and from memory to register. You cannot copy directly from one memory location to another.<br />
<br />
Described here are the instruction for word operations. There are equivalent instructions for reading a byte (LBZ) and a halfword (LHZ) or storing a byte (STB) and a halfword (STH).<br />
<br />
<br />
1. '''LWZ - Load Word and Zero'''<br />
<br />
Syntax: lwz rD,d(rA)<br />
<br />
Example: lwz 3,10(4)<br />
<br />
This will read the word at the memory location specified in GPR4 plus an "offset" of 10 and place it in GPR3.<br />
<br />
In the case of LBZ and LHZ the higher bits are cleared to zero when moving the value into a 32-bit register.<br />
<br />
<br />
2. '''LWZX - Load Word and Zero Indexed'''<br />
<br />
Syntax: lwzx rD,rA,rB<br />
<br />
Example: lwzx 3,4,5<br />
<br />
Here the word at the memory address computed by adding the values in GPR4 and GPR5 is read and placed in GPR3.<br />
<br />
<br />
3. '''LWZU - Load Word and Zero Update'''<br />
<br />
Syntax: lwzu rD,d(rA)<br />
<br />
Example: lwzu 3,10(4)<br />
<br />
This will read the word at the memory location specified in GPR4 plus an "offset" of 10 and place it in GPR3. Then the computed memory address is placed(updated) in GPR4.<br />
<br />
<br />
4. '''STW - Store Word'''<br />
<br />
Syntax: stw rD,d(rA)<br />
<br />
Example: stw 3,10(4)<br />
<br />
This will store the value in GPR3 at the memory location specified in GPR4 plus an "offset" of 10.<br />
<br />
<br />
5. '''STWX - Store Word Indexed'''<br />
<br />
Syntax: stwx rD,rA,rB<br />
<br />
Example: stwx 3,4,5<br />
<br />
This will store the value in GPR3 at the memory location computed by adding the values in GPR4 and GPR5.<br />
<br />
<br />
6. '''STWU - Store Word with Update'''<br />
<br />
Syntax: stwu rD,d(rA)<br />
<br />
Example: stwu 3,10(4)<br />
<br />
This will store the value in GPR3 at the memory location specified in GPR4 plus an "offset" of 10. Then the computed memory address is placed in GPR4.<br />
<br />
This instruction is frequently used to set up a stack frame.<br />
<br />
<br />
7. '''MFSPR - Move from Special-Purpose Register'''<br />
<br />
Syntax: mfspr rD,SPR<br />
<br />
Example: mfpsr 3,920<br />
<br />
Here the value from the special register 920 is moved to GPR3.<br />
<br />
<br />
8. '''MTSPR - Move to Special-Purpose Register'''<br />
<br />
Syntax: mtspr SPR,rS<br />
<br />
Example: mtspr 912,3<br />
<br />
Here the value in GPR3 is moved to the special register 912.<br />
<br />
<br />
9. '''Mnemonics for MFSPR and MTSPR'''<br />
<br />
a) '''MFLR - Move from Link Register'''<br />
<br />
It is not possible to use the standard instructions on the link register. So the value of this register first has to be moved into a GPR register.<br />
<br />
Example: mflr 0<br />
<br />
This will read the value in the link register into GPR0.<br />
<br />
b) '''MTLR - Move to Link Register'''<br />
<br />
Example: mtlr 0<br />
<br />
Here the value in GPR0 is written into the link register.<br />
<br />
C) '''MTCTR - Move to Count Register'''<br />
<br />
Example:<br />
<tt><br />
:li 4,100<br />
:mtctr 4<br />
</tt><br />
<br />
The count register is set to 100 via the GPR4. You cannot load an immediate value into the CTR.<br />
<br />
<br />
10. '''There are equivalent instructions for the CR, XER, CTR etc. registers.'''<br />
<br />
=== Branch instructions ===<br />
<br />
The PowerPC branch instructions are similar to the Intel Processor's jmp and call commands.<br />
<br />
1. '''B - Branch'''<br />
<br />
Syntax: b target_addr<br />
<br />
Example: b testlabel<br />
<br />
Here the execution will continue at the label "testlabel".<br />
<br />
This instruction can be compared to a "near jmp" in Intel syntax.<br />
<br />
<br />
2. '''Conditional branch instructions (mnemonics)'''<br />
<br />
Following a cmpi 7,0,3,5 instruction which compares the value in GPR3 with the integer 5 and places the resulting flags in the CR7 field of the CR register, the following conditional branch instructions can be executed. If the condition is true the program will continue at "testlabel" within the code segment. Otherwise it will just continue.<br />
<br />
<tt><br />
:beq - branch if equal | example: beq 7,testlabel<br />
:bne - branch if not equal | example: bne 7,testlabel<br />
:blt - branch if less than | example: blt 7,testlabel<br />
:bgt - branch if greater than | example: bgt 7,testlabel<br />
:ble - branch if less or equal | example: ble 7,testlabel<br />
:bge - branch if greater of equal | example: bge 7,testlabel<br />
</tt><br />
<br />
<br />
Example:<br />
<tt><br />
::cmpwi 4,100 /* Compare value in GPR4 with 100 */<br />
::bne else_label /*if not 100 goto else */<br />
:::...if statements...<br />
:::b endif_label /* jmp over else part */<br />
:else_label:<br />
:::...else statements...<br />
:endif_label:<br />
</tt><br />
<br />
<br />
3. '''BL - Branch then Link'''<br />
<br />
Syntax: bl target_addr<br />
<br />
Example: bl testsubroutine<br />
<br />
This instruction can be used to call a subroutine or function. Its absolute address has to be loaded into the link register before executing the bl instruction. The bl instruction will save the next instruction address in the link register after branching to allow the called subroutine to return. If bl is executed in a subroutine itself, the link register value has to be saved first before executing a bl instruction since that register will be overwritten by the bl instruction.<br />
<br />
<br />
4. '''BLR - Branch on Link Register'''<br />
<br />
This instruction is frequently used as a return command from a subroutine or function. The link register is filled with the 32-bit return address when a bl (branch then link) instruction is executed (see above). The blr instruction will read that return address from the link register and return to the next instruction in the calling routine.<br />
<br />
<br />
5. '''BDNZ - Branch Decrement not Zero'''<br />
<br />
Syntax: bdnz target<br />
<br />
Example:<br />
<tt><br />
::li 4, 100<br />
::mtctr 4<br />
:looplabel: <br />
::...some statements...<br />
::bdnz looplabel<br />
</tt><br />
In this example the count register (CTR) is loaded with the value 100 first via GPR4. Then the bdnz instruction will decrement the CTR register and branch to looplabel as long as the CTR (count register) is not zero.<br />
<br />
<br />
6. '''BDNZT - Branch Decrement not Zero True'''<br />
<br />
This mnemonic adds a conditional branch test to the BDNZ instruction.<br />
<br />
Syntax: bdnzt BI,target <br />
<br />
Example:<br />
<tt><br />
::li 4, 100<br />
::mtctr 4<br />
:looplabel: <br />
::...some statements...<br />
::cmpwi 5,10<br />
::bdnzt eq,looplabel<br />
</tt><br />
In this example the count register (CTR) is loaded with the value 100 first via GPR4. Then the bdnzt instruction will decrement the CTR register and branch to looplabel as long as the CTR (count register) is not zero. However, the instruction will also test if the condition is TRUE. So if the cmpwi instruction has determined that GPR5 has the value 10, the loop will be terminated before CTR has reached zero.<br />
<br />
<br />
7. '''BEQLR - Branch then Link if Equal'''<br />
<br />
Syntax: beqlr target_addr<br />
<br />
This mnemonic can be used to call a subroutine or function if the preceeding cmp instruction determined equal.<br />
<br />
<br />
8. '''BNELR - Branch then Link if not Equal'''<br />
<br />
Syntax: bnelr target_addr<br />
<br />
This mnemonic can be used to call a subroutine or function if the preceeding cmp instruction determined NOT equal.<br />
<br />
<br />
9. '''RFI - Return from interrupt'''<br />
<br />
returns from an interrupt service routine<br />
<br />
<br />
<br />
=== Various instructions ===<br />
<br />
<br />
1. '''CRXOR - Condition Register XOR'''<br />
<br />
Syntax: crxor crbD,crbA,crbB<br />
<br />
Example: crxor 6,6,6<br />
<br />
Clears bit 6 of the CR register<br />
<br />
<br />
2. '''CLRLWI - Clear left word immediate'''<br />
<br />
Syntax: clrlwi rA,rS,16<br />
<br />
Clear the high-order 16 bits of rS and place the result into rA.<br />
<br />
<br />
<br />
3. '''ISYNC'''<br />
<br />
Delay all following instructions until all previous instructions required for context.<br />
<br />
Since the PowerPC has several instruction queues this can make sure that an instruction is executed before the next in the code.<br />
<br />
<br />
<br />
== Application Binary Interface (SVR4 ABI) ==<br />
<br />
On Intel processors external subroutines and functions are usually called by pushing the arguments to be passed to the subroutine on the stack. The subroutine then sets up a stack frame and reads the parameters from the stack.<br />
<br />
The PowerPC has lots of registers but none is defined by the PowerPC architecture as a stack pointer. The programmer can select any register to be a stack register.<br />
<br />
To enable interoperatibility between different compilers and object files or libraries an ABI has been defined. There a slightly different versions of ABI's available. Here the Sytem V R4 ABI or SVR4 ABI is described since the GCC compiler for 32bit PowerPC's uses this ABI.<br />
<br />
The SVR4 ABI specifies that arguments are not passed on the stack but in registers beginning with GPR3. GPR1 is specified to be used as the stack frame pointer. Bit 6 of the CR register indicates that a floating point argument is passed in the registers. If this is not the case this bit can be cleared using the CRXOR 6,6,6 instruction.<br />
<br />
The registers are used as follows: <br />
<br />
r0 volatile, may be used by function linkage<br />
r1 stack pointer<br />
r2 reserved for system<br />
r3 .. r4 volatile, pass 1st - 2nd int args, return 1st - 2nd ints<br />
r5 .. r10 volatile, pass 3rd - 8th int args<br />
r11 .. r12 volatile, may be used by function linkage<br />
r13 small data area pointer<br />
r14 .. r31 saved<br />
f0 volatile<br />
f1 volatile, pass 1st float arg, return 1st float<br />
f2 .. f8 volatile, pass 2nd - 8th float args<br />
f9 .. f13 volatile<br />
f14 .. f30 saved<br />
f31 saved, static chain if needed.<br />
lr volatile, return address<br />
ctr volatile<br />
xer volatile<br />
fpscr volatile<br />
cr0 volatile<br />
cr1 volatile<br />
cr2 .. cr4 saved<br />
cr5 .. cr7 volatile<br />
<br />
Volatile means that a called function does not have to preserve its value when it returns, saved means that a called function must restore its value before returning. So the calling function must save these registers before calling a subroutine.<br />
<br />
The ABI also defines the stack frame which should be set up by the called subroutine. This a table showing how it shall be set up:<br />
<br />
SP----> +---------------------------------------+<br />
| back chain to caller | 0<br />
+---------------------------------------+<br />
| saved LR | 4<br />
+---------------------------------------+<br />
| Parameter save area (P) | 8<br />
+---------------------------------------+<br />
| Alloca space (A) | 8+P<br />
+---------------------------------------+<br />
| Local variable space (L) | 8+P+A<br />
+---------------------------------------+<br />
| saved CR (C) | 8+P+A+L<br />
+---------------------------------------+<br />
| Save area for GP registers (G) | 8+P+A+L+C<br />
+---------------------------------------+<br />
| Save area for FP registers (F) | 8+P+A+L+C+G<br />
+---------------------------------------+<br />
old SP->| back chain to caller's caller |<br />
+---------------------------------------+<br />
<br />
<br />
GCC uses the .align 2 command to align the SP to two words or 8 bytes. To set up this stack frame the subroutine first decrements the passed SP. This depends how many local variables shall be put into the stack frame, lets assume the SP is decremented by 40 bytes here. This is done using the<br />
<br />
stwu 1,-40(1) <br />
<br />
instruction. Hereby the current stack pointer passed from the calling function in GPR1 is stored at SP-40 which then becomes the back link field. Following that this instruction will set the SP in GPR1 to point to GPR1-40, the back link field. In this field the stack pointer of the previous stack frame set up by the calling function has just been stored before setting SP to SP-40. This sets up a linked list of stackframes. It allows to follow this list and write into the preceeding stack frame. GCC writes the link register value into the "LR save word" field of the preceeding stack frame with the<br />
<br />
stw 0,44(1) <br />
<br />
instruction after moving the link register into GPR0.<br />
<br />
In its own stack frame the called subroutine will store the link register in the "LR save word" field.<br />
<br />
If the stack frame has a size of 40 bytes, this results in 10 fields of word size. Subtracting two for the back chain and LR save word fields leaves eight words to save data in the stack frame. These fields could be addressed by:<br />
<br />
SP+8, SP+12, SP+16, SP+20, SP+24, SP+28, SP+32, SP+36<br />
<br />
In the "Hello world" example GCC saved GPR31 in SP+36, GPR3 in SP+28 and GPR4 in SP+24.<br />
<br />
To free the stack frame again the SP has to be set back to point to the back chain field of the previous stack frame. The instruction<br />
<br />
addi 1,1,40 <br />
<br />
will do this in our case here.<br />
<br />
GCC will call its exit function instead.<br />
<br />
[[Category:Development]]</div>KiraWhttps://wiibrew.org/w/index.php?title=Assembler_Tutorial&diff=104883Assembler Tutorial2016-11-12T02:53:52Z<p>KiraW: Correcting typo for subfic</p>
<hr />
<div>==Introduction==<br />
This introduction for PowerPC assembler assumes that you are somewhat familiar with the Intel assembler. It is not written as a tutorial for beginners in assembly programming. Hopefully it is possible to understand this tutorial if you just have programed in C before. This tutorial will allow you to write applications in PowerPC assembler. Disassembling compiled code is not covered, however, knowing assembler is a prerequisite to disassemble code.<br />
<br />
<br />
The PowerPC is a RISC (Reduced Instruction Set Computing) processor architecture. PowerPC is an acronym which shall stand for Performance Optimization With Enhanced RISC / Performance Chip or Power Performance Computing. The specification for it was released in 1993 and is a 64-bit specification with a 32-bit subset. Almost all PowerPC processors are 32-bit now but feature a 64-bit data bus.<br />
<br />
The PowerPC was developed jointly by Apple, IBM and Motorola (now named Freescale). There are many different PowerPC processors available. Apple has been using the PowerPC in the Macintosh systems, IBM is using it in its RS/6000 and pSeries computers und Nintendo used it in its Gamecube and in Wii playstations. There are many embedded applications using the PowerPC.<br />
<br />
The PowerPC is a Superscalar microprocessor which means it has separate execution units. This are an integer unit, a floating-point unit, a branching unit, and even more depending on the processor type. These units can execute instructions in parallel within one clock cycle.<br />
<br />
<br />
== The registers ==<br />
<br />
The PowerPC has many more registers than the Intel processors and these are named differently. All registers are 4 bytes or 32bits long on the 32-bit versions of the PowerPC. There are 32 (0-31) General Purpose Registers (GPRs or rX), 32 Floating point registers (FPRs or fX) and special purpose registers (SPRs) like the program counter PC or IAR (instruction address register). This keeps track which instruction needs to be executed next. There is a link register (LR) which can hold the address of a procedure for branch instructions, the condition register (CR) which has eight (0-7) 4 bit fields holding the result of e.g. a compare instruction. The count register for loops is called CTR. XER is the fixed-point exception register. FPSCR is the floating point status and control register.<br />
<br />
On the PowerPC you cannot move data from one memory address to another. You have to read the data into a register first and then store the contents of the register at the destination address in memory. This design shall allow the processor to operate more efficiently.<br />
<br />
The PowerPC uses the big-endian format to store data in memory. The most significant byte (MSB) value is stored at the memory location with the lowest address. So the bit numbering is reversed compared to an Intel processor.<br />
<br />
<br />
== Variables ==<br />
<br />
The 32-bit version of the PowerPC supports the following data sizes:<br />
<tt><br />
:Byte - 8 bits<br />
:Halfword - 16 bits<br />
:Word - 32 bits<br />
</tt><br />
<br />
An integer value of 12 can also be specified as 0x0C in hexadecimal or01100 in binary.<br />
<br />
<br />
Variables are defined either in the data section or in the bss section which takes uninitialised data only.<br />
<br />
Here are examples how to define variables. The name of the variable is always set as a label followed by a colon.<br />
<tt><br />
:bytevar: .byte 0 #length of one byte - init zero<br />
:shortvar: .short 0 #length of two byte - init zero<br />
:wordvar: .long 0 #length of four byte - init zero<br />
<br /><br />
:fivebytevar: .byte 11,12,13,14,15 #an array of five variables of one byte each<br />
:endof_fivebytevar: #specifies the address immediately following the array<br />
<br /><br />
:stringvar: .string "Hello\n" #string variable - init to "Hello" plus newline<br />
:: .size stringvarlen, .-stringvar #length of stringvar<br />
</tt><br />
<br />
<br />
== Constants ==<br />
<br />
The AS assembler allows to define constants. These will be replaced by the assembler before assembling the code.<br />
<br />
Example:<br />
<br />
.set GPR0,0<br />
<br />
This will define the constant "GPR0" having the value zero. This will replace all occurences of GPR0 in the code by the number zero. This can enhance the readability of the code, since the registers are specified as numbers just like immediate values in the instructions. So using constants e.g. the instruction addi 0,0,0 can be written as addi GPR0,GPR0,0 in the code. Without defining constants the assembler will also accept addi %r0,%r0,0.<br />
<br />
<br />
== Instructions and Mnemonics ==<br />
<br />
Assembler instructions for Intel processors have up to two parameters separated by a comma. Typically the first parameter is modified with the second.<br />
<br />
The AS assembler will do this for Intel processors in the opposite direction compared with MASM, TASM etc. For the PowerPC, however, the first operand is used as the destination register and there can be up to five parameters separated by commas.<br />
<br />
The PowerPC uses fixed-length 32-bit instructions. As an example for an addi instruction this 32-bit integer is divided in the following fields:<br />
<br />
<tt><br />
:Opcode: 6 bits<br />
:Source register: 5 bits<br />
:Destination register: 5 bits<br />
:Immediate value: 16 bits<br />
</tt><br />
<br />
So to fill a 32bit register with an immediate value you have to use two instructions moving 16 bits each. In case of a 64bit processor you need even more instructions since you have to shift the bits here too.<br />
<br />
Mnemonics are specializations of a more general instruction. They are used as simplified instructions for easier coding of assembly language programs. They are defined for frequently used instructions. A mnemonic may have two parameters and this will be converted by the assembler to an instruction which may require three or more parameters. Samples of mnemonics can be found among the described instructions below.<br />
<br />
The available instructions for the PowerPC can be grouped as follows:<br />
<br />
# Integer instructions<br />
# Floating point instructions<br />
# Load and store instructions<br />
# Branch and flow control instructions<br />
# Various instructions<br />
<br />
The most common instructions of each group will be discussed here.<br />
<br />
<br />
=== Integer Instructions ===<br />
<br />
<br />
==== Integer Arithmetic Instructions ====<br />
<br />
===== ADD =====<br />
<br />
This instructions has several variants:<br />
<br />
1. '''ADD'''<br />
<br />
Syntax: add rD,rA,rB<br />
<br />
This command adds two registers (rA and rB) and puts the result into the register rD (destination).<br />
<br />
Example: add 3,6,4<br />
<br />
In this example GPR6 and GPR4 are added and the result is put into GPR3.<br />
<br />
Example: add 3,6,3<br />
<br />
In this example GPR6 and GPR3 are added and the result is put into GPR3.<br />
<br />
Example: add 3,0,4<br />
<br />
Here the value in GPR4 is moved into GPR3 - like an Intel move instruction. If the second parameter is a zero this does not mean a GPR0 but the value zero. This is the case for several instructions.<br />
<br />
<br />
2. '''ADDI - Add Immediate'''<br />
<br />
Syntax: addi rD,rA,SIMM<br />
<br />
This command adds a 16-bit signed integer (SIMM) to register rA and puts the result into the register rD (destination).<br />
<br />
Example: addi 3,6,4<br />
<br />
In this example GPR6 and the value 4 are added and the result is put into GPR3.<br />
<br />
As you can see the registers are specified as a number and the integer is specified as a number. To improve the readability of the code you can define constants for the registers, e.g.: <br />
<br />
.set r0,0; .set r1,1; .set r2,2; .set r3,3; .set r4,4; .set r5,5; .set r6,6; .....<br />
<br />
Then the above command can be written as: addi r3,r6,4<br />
<br />
You can also use the addi command as a move instruction:<br />
<br />
addi 3,0,4<br />
<br />
This sets GPR3 to the value 4. If the second parameter is a zero here this does not mean a GPR0 but the value zero.<br />
<br />
<br />
3. '''ADDIS - Add Immediate Shifted'''<br />
<br />
Syntax: addis rD,rA,SIMM<br />
<br />
This command is used to add a 16-bit immediate value to the upper 16 bits of a 32bit register. It adds a 16-bit signed integer (SIMM) to register rA, then shifts left register rA by 16 bits and then puts the result into the register rD (destination). The lower 16 bits are cleared by this command. So to fill a 32 bit register with an immediate 32-bit value you first have to use addis to fill the upper 16 bits and then addi to fill the lower 16 bits. Otherwise the lower 16 bits would be cleared again by the addis command.<br />
<br />
Example: addis 3,3,4<br />
<br />
In this example GPR3 and the value 4 are added, then GPR3 is shifted 16 bits to the left and the result is put into GPR3. This will then contain 0x00040000 in hexadecimal. If you then execute an addi 3,3,4 command GPR3 will contain 0x00040004 in hexadecimal.<br />
<br />
To move a pointer to an address of a variable or function into a register there are the @ha/@h and @l modifiers available. If you append these to the variable name you get the lower (@l) 16 bit of the absolute 32-bit address of the variable and with @ha you get the higher 16 bit of the absolute 32-bit address. <br />
<br />
Example:<br />
:addis 3,0,hello@ha<br />
:addi 3,0,hello@l<br />
<br />
With these two instructions the absolute 32-bit address of the string variable hello is moved into the GPR 3 register.<br />
<br />
Instead of addis/addi the mnemonics lis/la are often used. These are explained below.<br />
<br />
<br />
4. '''ADD. - ADD with CR Update'''<br />
<br />
Contrary to the Intel processors the ADD instruction will not modify any flags. To achieve this you have to apped a dot to the instruction. So add. will set the CR bits 0-3 (CR0) in the CR register. These bits will then reflect a signed comparison of the result to zero. In effect the dot adds a <br />
cmpwi rD,0<br />
instruction to the ADD instruction. A dot can be added to many PowerPC instructions.<br />
<br />
<br />
5. '''Mnemonics for the ADD instruction'''<br />
<br />
<br />
The following mnemonics are converted into ADD instruction by the assembler:<br />
<br />
<br />
LI - Load Immediate<br />
<br />
Syntax: li rD,value<br />
<br />
This is equivalent to addi rD,0,value<br />
<br />
Example: li 3,100<br />
<br />
Sets GPR3 to 100 and clears the higher 16 bits.<br />
<br />
<br />
LIS - Load Immediate Shifted<br />
<br />
Syntax: lis rD,value<br />
<br />
This is equivalent to addis rD,0,value<br />
<br />
Example: lis 3,100<br />
<br />
Sets higher 16 bits of GPR3 to 100 and clears the lower 16 bits.<br />
<br />
<br />
LA - Load Address<br />
<br />
Syntax: la rD,d(rA)<br />
<br />
This is equivalent to addi rD,rA,d<br />
<br />
Example: la 3,100(9)<br />
<br />
Adds 100 to the address in GPR9 and loads the result in GPR3.<br />
<br />
<br />
As a side note this is also equivalent: <br />
<br />
li rD,value = ori rA,0,UIMM (UIMM = unsigned integer value)<br />
<br />
So OR immediate can be used to load an immediate too as long as the value is unsigned.<br />
<br />
To load an immediate 32-bit value in a register you can use:<br />
:lis 3,100<br />
:ori 3,0,200<br />
This loads 100 into the higher 16 bits and 200 into the lower 16 bits.<br />
<br />
<br />
===== SUBF - Subtract From =====<br />
<br />
<br />
1. '''SUBF'''<br />
<br />
Syntax: subf rD,rA,rB<br />
<br />
Example: subf 3,4,5<br />
<br />
Similar to the ADD instruction SUBF will subtract GPR4 from GPR5 and place the result in GPR3.<br />
<br />
<br />
2. '''Subfic - Subtract from Immediate Carrying'''<br />
<br />
Syntax: subfic rD,rA,SIMM<br />
<br />
Example: subfic 3,4,5<br />
<br />
This will subtract GPR4 from signed integer value 5 and place the result in GPR3. CRO is modified.<br />
<br />
<br />
===== MUL - Multiply =====<br />
<br />
Multiplying two 32-bit values will often result in a 64-bit value. So there are separate instructions to put the 64-bit result into two 32-bit registers:<br />
<br />
1. '''MULLW - Multiply Low Word'''<br />
<br />
Syntax: mullw rD,rA,rB<br />
<br />
Example: mullw 3,4,5<br />
<br />
This will multiply the contents of GPR4 and GPR5 and place the lower 32 bits of the result in GPR3.<br />
<br />
<br />
2. '''MULLH - Multiply High Word'''<br />
<br />
Syntax: mullh rD,rA,rB<br />
<br />
Example: mullh 6,4,5<br />
<br />
This will multiply the contents of GPR4 and GPR5 and place the higher 32 bits of the result in GPR6.<br />
<br />
<br />
3. '''MULLI - Multiply Low Immediate'''<br />
<br />
Syntax: mulli rD,rA,SIMM<br />
<br />
Example: mullh 3,4,5<br />
<br />
This will multiply the contents of GPR4 with the integer 5 and place the lower 32 bits of the result in GPR3. So the higher 32 bits - if any - are lost.<br />
<br />
<br />
===== DIV - Divide =====<br />
<br />
'''divw - Divide Word'''<br />
<br />
Syntax: divw rD,rA,rB<br />
<br />
Example: divw 3,4,5<br />
<br />
This will divide the contents of GPR4 with the contents of GPR5 and place the result in GPR3. The remainder is lost.<br />
<br />
Assembler programmers somehow design their tasks so that they only need to multiply and divide by powers of two. This way they can use the shift instructions instead of multiply and divide.<br />
<br />
<br />
==== Integer Compare und Logical Instructions ====<br />
<br />
1. '''CMP - Compare'''<br />
<br />
Syntax: cmp crfD,L,rA,rB<br />
<br />
Example: cmp 7,0,3,4<br />
<br />
This will compare the signed contents of the GPR3 and GPR4 registers and set the CR7 field of the CR register accordingly. The second parameter has to be set to zero for 32bit processors.<br />
<br />
If <nowiki>rA<rB then bit 0 of CR7 will be set. If rA>rB</nowiki> then bit 1 of CR7 will be set. If rA=rB then bit 2 of CR7 will be set.<br />
<br />
<br />
2. '''CMPI - Compare Immediate'''<br />
<br />
Syntax: cmpi crfD,L,rA,SIMM<br />
<br />
Example: cmpi 7,0,3,4<br />
<br />
This will compare the signed contents of the GPR3 register with the value 4 and set the CR7 field of the CR register accordingly. The second parameter has to be set to zero for 32bit processors.<br />
<br />
If rA<4 then bit 0 of CR7 will be set. If rA>4 then bit 1 of CR7 will be set. If rA=4 then bit 2 of CR7 will be set.<br />
<br />
<br />
3. '''Mnemonics for CMP'''<br />
<br />
Sometimes the following mnemonics are used:<br />
<br />
CMPWI - compare word immediate<br />
<br />
Syntax: cmpwi crD,rA,SIMM<br />
<br />
This is equivalent to cmpi crD,0,rA,SIMM<br />
<br />
CMPLWI - Compare Logical word immediate<br />
<br />
Syntax: cmplwi crD,rA,UIMM<br />
<br />
This is equivalent to cmpli crD,0,rA,UIMM (UIMM = unsigned integer value)<br />
<br />
<br />
===== OR =====<br />
<br />
1. '''OR'''<br />
<br />
Syntax: or rA,rS,rB<br />
<br />
Example: or 3,4,5<br />
<br />
This instruction will OR the contents of GPR4 and GPR5 and place the result in GPR3. The variant OR. (+dot) will update CR too.<br />
<br />
<br />
2. '''ORI - OR Immediate'''<br />
<br />
Syntax: ori rA,rS,UIMM<br />
<br />
Example: ori 3,4,5<br />
<br />
This instruction will OR the contents of GPR4 with the unsigned integer 5 and place the result in GPR3.<br />
<br />
ori 0,0,0 can be used as a NOP (no operation) instruction. This could be used e.g. as a breakpoint for a debugger.<br />
<br />
<br />
3. '''ORIS - OR Immediate Shifted'''<br />
<br />
Syntax: ori rA,rS,UIMM<br />
<br />
Example: oris 3,4,5<br />
<br />
This instruction will OR the upper 16 bits contained in GPR4 with the unsigned integer 5 and place the result in GPR3.<br />
<br />
<br />
4. '''Mnemonics for OR'''<br />
<br />
MR - move [to] register<br />
<br />
Syntax: mr rA,rS <br />
<br />
This is equivalent to: or rA,rS,rS<br />
<br />
Example: mr 31,1<br />
<br />
This will move the value in GPR1 to GPR31.<br />
<br />
<br />
===== AND =====<br />
<br />
1. '''AND'''<br />
<br />
Syntax: and rA,rS,rB<br />
<br />
Example: and 3,4,5<br />
<br />
This instruction will AND the contents of GPR4 and GPR5 and place the result in GPR3.<br />
<br />
<br />
2. '''ANDI. - AND Immediate'''<br />
<br />
Syntax: andi. rA,rS,UIMM<br />
<br />
Example: andi. 3,4,0b00000011<br />
<br />
This instruction will AND the contents of GPR4 with the unsigned integer 3 (binary 000011) and place the result in GPR3. Since this instruction ends with a dot, the CR register is updated. The higher 16 bits will be cleared. In effect all but the last two bits of GPR4 are cleared in this example.<br />
<br />
<br />
3. '''ANDIS. - AND Immediate Shifted'''<br />
<br />
Syntax: andis. rA,rS,UIMM<br />
<br />
Example: andis. 3,4,5<br />
<br />
This instruction will AND the upper 16 bits contained in GPR4 with the unsigned integer 5 and place the result in GPR3. Since this instruction ends with a dot, the CR register is updated. The lower 16 bits will be cleared.<br />
<br />
<br />
4. There are equivalent instructions for XOR, NAND etc.<br />
<br />
<br />
==== Integer shift and rotate instructions ====<br />
<br />
1. '''SLW - Shift left word'''<br />
<br />
Syntax: slw rA,rS,rB<br />
<br />
Example: slw 3,4,5<br />
<br />
The contents of the GPR4 register are shifted left by the value placed in the low-order six bits of the GPR5 register. The 32-bit result is placed in GPR3.<br />
<br />
<br />
2. '''SRW - Shift right word'''<br />
<br />
Syntax: srw rA,rS,rB<br />
<br />
Example: srw 3,4,5<br />
<br />
The contents of the GPR4 register are shifted right by the value placed in the low-order six bits of the GPR5 register. The 32-bit result is placed in GPR3.<br />
<br />
<br />
3. '''RLWINM - Rotate Left Word Immediate then AND with Mask'''<br />
<br />
Syntax: rlwinm rA,rS,SH,MB,ME<br />
<br />
Example: rlwinm 3,4,5,0,31<br />
<br />
Here the contents in GPR4 will be rotated left by 5 bits (the immediate value of 5 - parameter three) and the result placed into GPR3. After rotating GPR4 and before placing the result into GPR3 the value is ANDed with the mask specified in the last two parameters. The fourth parameter specifies the beginning of the 1-bits in the mask and the fifth parameter specifies the end of the 1-bits in the mask. In this example the begin is 0 and the end is 31. So all 32 bits are set in the AND mask. This causes no bits to be cleared by the mask. <br />
<br />
A rotate right can be done by specifying a value of 32-n as the third parameter. So<br />
<br />
rlwinm 3,4,31,0,31<br />
<br />
will rotate GPR4 right by one bit. This is not equal to a division by 2 since a bit may be moved into the sign bit by the rotation. Use the shift mnemonics for that which are described below.<br />
<br />
<br />
The PowerPC stores the data in big-endian format. So if you have a value of 31 which is0011111 and you want to clear the lower two bits you have to AND this with a mask of 0,29. Then bits 30 and 31 are set to zero in the mask and these bits will be cleared in the value of 31. The result will then be0011100 or 28.<br />
<br />
<br />
If the third parameter is a zero there is no rotation and this command is just used as an AND mask. This is often done by gcc since this allows to execute AND with an immediate 32-bit value.<br />
<br />
Example:<br />
<br />
li 3,0b11111111<br />
rlwinm 3,3,0,24,24 <br />
<br />
will clear all bits except bit 24 (big-endian format) in register GPR3. So register GPR3 will then contain 128 or10000000.<br />
<br />
<br />
4. '''Mnemonics for shift'''<br />
<br />
SLWI - Shift Left Word Immediate<br />
<br />
Syntax: slwi rD,rA,SIMM (SIMM<32) <br />
<br />
This is equivalent to: rlwinm rA,rS,n,0,31–n<br />
<br />
Example: slwi 3,4,5<br />
<br />
Shifts GPR4 left by 5 bits and places the result in GPR3. This is equal to multiplying GPR4 with 32 (2**5).<br />
<br />
<br />
SRWI - Shift Right Word Immediate<br />
<br />
Syntax: srwi rD,rA,SIMM (SIMM<32) <br />
<br />
This is equivalent to: rlwinm rA,rS,32 – n,n,31<br />
<br />
Example: srwi 3,4,5<br />
<br />
Shifts GPR4 right by 5 bits and places the result in GPR3. This is equal to dividing GPR4 by 32 (2**5).<br />
<br />
<br />
<br />
=== Floating point instructions ===<br />
<br />
<br />
1. '''FMR - Floating Move Register'''<br />
<br />
Syntax: fmr frD,frB<br />
<br />
Example: fmr 3,4<br />
<br />
The integer value in the FPR4 will be moved into the FPR3 (FPR=Floating point register)<br />
<br />
<br />
2. '''LFS - Load Floating-Point Single'''<br />
<br />
Syntax: lfs frD,d(rA)<br />
<br />
Example: lsd 3,0(4)<br />
<br />
Loads the word of data from the location in memory specified in GPR4 into floating-point register FPR3 and thereby converting it to floating-point double-precision.<br />
<br />
<br />
3. '''STFS - Store Floating-Point Single'''<br />
<br />
Syntax: stfs frS,d(rA)<br />
<br />
Example: stfs 3,0(4)<br />
<br />
Converts the contents of FPR3 to single-precision and stores it at the location in memory specified in GPR4.<br />
<br />
<br />
4. '''LFD- Load Floating-Point Double'''<br />
<br />
Syntax: lfd frD,d(rA)<br />
<br />
Example: lfd 3,0(4)<br />
<br />
Loads the doubleword of data from the location in memory specified in GPR4 into floating-point register FPR3.<br />
<br />
<br />
5. '''STFD- Store Floating-Point Double'''<br />
<br />
Syntax: stfd frD,d(rA)<br />
<br />
Example: stfd 3,0(4)<br />
<br />
Stores the doubleword of data in the floating-point register FPR3 at the location in memory specified in GPR4.<br />
<br />
<br />
6. '''MTFSF - Move to FPSCR Fields''' <br />
<br />
Syntax: mtfsf FM,frB<br />
<br />
Copies the contents of the floating-point register frB into the Floating-Point Status and Control Register (FPSCR) under the control of the field mask in FM.<br />
<br />
<br />
7. '''MTFSB1 - Move to FPSCR Bit 1'''<br />
<br />
Syntax: mtfsb1 crbD<br />
<br />
Example: mtfsb1 4<br />
<br />
Sets bit 4 of the FBSCR register to 1.<br />
<br />
<br />
=== Load and Store Instructions ===<br />
<br />
The PowerPC allows you to move data from register to memory and from memory to register. You cannot copy directly from one memory location to another.<br />
<br />
Described here are the instruction for word operations. There are equivalent instructions for reading a byte (LBZ) and a halfword (LHZ) or storing a byte (STB) and a halfword (STH).<br />
<br />
<br />
1. '''LWZ - Load Word and Zero'''<br />
<br />
Syntax: lwz rD,d(rA)<br />
<br />
Example: lwz 3,10(4)<br />
<br />
This will read the word at the memory location specified in GPR4 plus an "offset" of 10 and place it in GPR3.<br />
<br />
In the case of LBZ and LHZ the higher bits are cleared to zero when moving the value into a 32-bit register.<br />
<br />
<br />
2. '''LWZX - Load Word and Zero Indexed'''<br />
<br />
Syntax: lwzx rD,rA,rB<br />
<br />
Example: lwzx 3,4,5<br />
<br />
Here the word at the memory address computed by adding the values in GPR4 and GPR5 is read and placed in GPR3.<br />
<br />
<br />
3. '''LWZU - Load Word and Zero Update'''<br />
<br />
Syntax: lwzu rD,d(rA)<br />
<br />
Example: lwzu 3,10(4)<br />
<br />
This will read the word at the memory location specified in GPR4 plus an "offset" of 10 and place it in GPR3. Then the computed memory address is placed(updated) in GPR4.<br />
<br />
<br />
4. '''STW - Store Word'''<br />
<br />
Syntax: stw rD,d(rA)<br />
<br />
Example: stw 3,10(4)<br />
<br />
This will store the value in GPR3 at the memory location specified in GPR4 plus an "offset" of 10.<br />
<br />
<br />
5. '''STWX - Store Word Indexed'''<br />
<br />
Syntax: stwx rD,rA,rB<br />
<br />
Example: stwx 3,4,5<br />
<br />
This will store the value in GPR3 at the memory location computed by adding the values in GPR4 and GPR5.<br />
<br />
<br />
6. '''STWU - Store Word with Update'''<br />
<br />
Syntax: stwu rD,d(rA)<br />
<br />
Example: stwu 3,10(4)<br />
<br />
This will store the value in GPR3 at the memory location specified in GPR4 plus an "offset" of 10. Then the computed memory address is placed in GPR4.<br />
<br />
This instruction is frequently used to set up a stack frame.<br />
<br />
<br />
7. '''MFSPR - Move from Special-Purpose Register'''<br />
<br />
Syntax: mfspr rD,SPR<br />
<br />
Example: mfpsr 3,920<br />
<br />
Here the value from the special register 920 is moved to GPR3.<br />
<br />
<br />
8. '''MTSPR - Move to Special-Purpose Register'''<br />
<br />
Syntax: mtspr SPR,rS<br />
<br />
Example: mtspr 912,3<br />
<br />
Here the value in GPR3 is moved to the special register 912.<br />
<br />
<br />
9. '''Mnemonics for MFSPR and MTSPR'''<br />
<br />
a) '''MFLR - Move from Link Register'''<br />
<br />
It is not possible to use the standard instructions on the link register. So the value of this register first has to be moved into a GPR register.<br />
<br />
Example: mflr 0<br />
<br />
This will read the value in the link register into GPR0.<br />
<br />
b) '''MTLR - Move to Link Register'''<br />
<br />
Example: mtlr 0<br />
<br />
Here the value in GPR0 is written into the link register.<br />
<br />
C) '''MTCTR - Move to Count Register'''<br />
<br />
Example:<br />
<tt><br />
:li 4,100<br />
:mtctr 4<br />
</tt><br />
<br />
The count register is set to 100 via the GPR4. You cannot load an immediate value into the CTR.<br />
<br />
<br />
10. '''There are equivalent instructions for the CR, XER, CTR etc. registers.'''<br />
<br />
=== Branch instructions ===<br />
<br />
The PowerPC branch instructions are similar to the Intel Processor's jmp and call commands.<br />
<br />
1. '''B - Branch'''<br />
<br />
Syntax: b target_addr<br />
<br />
Example: b testlabel<br />
<br />
Here the execution will continue at the label "testlabel".<br />
<br />
This instruction can be compared to a "near jmp" in Intel syntax.<br />
<br />
<br />
2. '''Conditional branch instructions (mnemonics)'''<br />
<br />
Following a cmpi 7,0,3,5 instruction which compares the value in GPR3 with the integer 5 and places the resulting flags in the CR7 field of the CR register, the following conditional branch instructions can be executed. If the condition is true the program will continue at "testlabel" within the code segment. Otherwise it will just continue.<br />
<br />
<tt><br />
:beq - branch if equal | example: beq 7,testlabel<br />
:bne - branch if not equal | example: bne 7,testlabel<br />
:blt - branch if less than | example: blt 7,testlabel<br />
:bgt - branch if greater than | example: bgt 7,testlabel<br />
:ble - branch if less or equal | example: ble 7,testlabel<br />
:bge - branch if greater of equal | example: bge 7,testlabel<br />
</tt><br />
<br />
<br />
Example:<br />
<tt><br />
::cmpwi 4,100 /* Compare value in GPR4 with 100 */<br />
::bne else_label /*if not 100 goto else */<br />
:::...if statements...<br />
:::b endif_label /* jmp over else part */<br />
:else_label:<br />
:::...else statements...<br />
:endif_label:<br />
</tt><br />
<br />
<br />
3. '''BL - Branch then Link'''<br />
<br />
Syntax: bl target_addr<br />
<br />
Example: bl testsubroutine<br />
<br />
This instruction can be used to call a subroutine or function. Its absolute address has to be loaded into the link register before executing the bl instruction. The bl instruction will save the next instruction address in the link register after branching to allow the called subroutine to return. If bl is executed in a subroutine itself, the link register value has to be saved first before executing a bl instruction since that register will be overwritten by the bl instruction.<br />
<br />
<br />
4. '''BLR - Branch on Link Register'''<br />
<br />
This instruction is frequently used as a return command from a subroutine or function. The link register is filled with the 32-bit return address when a bl (branch then link) instruction is executed (see above). The blr instruction will read that return address from the link register and return to the next instruction in the calling routine.<br />
<br />
<br />
5. '''BDNZ - Branch Decrement not Zero'''<br />
<br />
Syntax: bdnz target<br />
<br />
Example:<br />
<tt><br />
::li 4, 100<br />
::mtctr 4<br />
:looplabel: <br />
::...some statements...<br />
::bdnz looplabel<br />
</tt><br />
In this example the count register (CTR) is loaded with the value 100 first via GPR4. Then the bdnz instruction will decrement the CTR register and branch to looplabel as long as the CTR (count register) is not zero.<br />
<br />
<br />
6. '''BDNZT - Branch Decrement not Zero True'''<br />
<br />
This mnemonic adds a conditional branch test to the BDNZ instruction.<br />
<br />
Syntax: bdnzt BI,target <br />
<br />
Example:<br />
<tt><br />
::li 4, 100<br />
::mtctr 4<br />
:looplabel: <br />
::...some statements...<br />
::cmpwi 5,10<br />
::bdnzt eq,looplabel<br />
</tt><br />
In this example the count register (CTR) is loaded with the value 100 first via GPR4. Then the bdnzt instruction will decrement the CTR register and branch to looplabel as long as the CTR (count register) is not zero. However, the instruction will also test if the condition is TRUE. So if the cmpwi instruction has determined that GPR5 has the value 10, the loop will be terminated before CTR has reached zero.<br />
<br />
<br />
7. '''BEQLR - Branch then Link if Equal'''<br />
<br />
Syntax: beqlr target_addr<br />
<br />
This mnemonic can be used to call a subroutine or function if the preceeding cmp instruction determined equal.<br />
<br />
<br />
8. '''BNELR - Branch then Link if not Equal'''<br />
<br />
Syntax: bnelr target_addr<br />
<br />
This mnemonic can be used to call a subroutine or function if the preceeding cmp instruction determined NOT equal.<br />
<br />
<br />
9. '''RFI - Return from interrupt'''<br />
<br />
returns from an interrupt service routine<br />
<br />
<br />
<br />
=== Various instructions ===<br />
<br />
<br />
1. '''CRXOR - Condition Register XOR'''<br />
<br />
Syntax: crxor crbD,crbA,crbB<br />
<br />
Example: crxor 6,6,6<br />
<br />
Clears bit 6 of the CR register<br />
<br />
<br />
2. '''CLRLWI - Clear left word immediate'''<br />
<br />
Syntax: clrlwi rA,rS,16<br />
<br />
Clear the high-order 16 bits of rS and place the result into rA.<br />
<br />
<br />
<br />
3. '''ISYNC'''<br />
<br />
Delay all following instructions until all previous instructions required for context.<br />
<br />
Since the PowerPC has several instruction queues this can make sure that an instruction is executed before the next in the code.<br />
<br />
<br />
<br />
== Application Binary Interface (SVR4 ABI) ==<br />
<br />
On Intel processors external subroutines and functions are usually called by pushing the arguments to be passed to the subroutine on the stack. The subroutine then sets up a stack frame and reads the parameters from the stack.<br />
<br />
The PowerPC has lots of registers but none is defined by the PowerPC architecture as a stack pointer. The programmer can select any register to be a stack register.<br />
<br />
To enable interoperatibility between different compilers and object files or libraries an ABI has been defined. There a slightly different versions of ABI's available. Here the Sytem V R4 ABI or SVR4 ABI is described since the GCC compiler for 32bit PowerPC's uses this ABI.<br />
<br />
The SVR4 ABI specifies that arguments are not passed on the stack but in registers beginning with GPR3. GPR1 is specified to be used as the stack frame pointer. Bit 6 of the CR register indicates that a floating point argument is passed in the registers. If this is not the case this bit can be cleared using the CRXOR 6,6,6 instruction.<br />
<br />
The registers are used as follows: <br />
<br />
r0 volatile, may be used by function linkage<br />
r1 stack pointer<br />
r2 reserved for system<br />
r3 .. r4 volatile, pass 1st - 2nd int args, return 1st - 2nd ints<br />
r5 .. r10 volatile, pass 3rd - 8th int args<br />
r11 .. r12 volatile, may be used by function linkage<br />
r13 small data area pointer<br />
r14 .. r31 saved<br />
f0 volatile<br />
f1 volatile, pass 1st float arg, return 1st float<br />
f2 .. f8 volatile, pass 2nd - 8th float args<br />
f9 .. f13 volatile<br />
f14 .. f30 saved<br />
f31 saved, static chain if needed.<br />
lr volatile, return address<br />
ctr volatile<br />
xer volatile<br />
fpscr volatile<br />
cr0 volatile<br />
cr1 volatile<br />
cr2 .. cr4 saved<br />
cr5 .. cr7 volatile<br />
<br />
Volatile means that a called function does not have to preserve its value when it returns, saved means that a called function must restore its value before returning. So the calling function must save these registers before calling a subroutine.<br />
<br />
The ABI also defines the stack frame which should be set up by the called subroutine. This a table showing how it shall be set up:<br />
<br />
SP----> +---------------------------------------+<br />
| back chain to caller | 0<br />
+---------------------------------------+<br />
| saved LR | 4<br />
+---------------------------------------+<br />
| Parameter save area (P) | 8<br />
+---------------------------------------+<br />
| Alloca space (A) | 8+P<br />
+---------------------------------------+<br />
| Local variable space (L) | 8+P+A<br />
+---------------------------------------+<br />
| saved CR (C) | 8+P+A+L<br />
+---------------------------------------+<br />
| Save area for GP registers (G) | 8+P+A+L+C<br />
+---------------------------------------+<br />
| Save area for FP registers (F) | 8+P+A+L+C+G<br />
+---------------------------------------+<br />
old SP->| back chain to caller's caller |<br />
+---------------------------------------+<br />
<br />
<br />
GCC uses the .align 2 command to align the SP to two words or 8 bytes. To set up this stack frame the subroutine first decrements the passed SP. This depends how many local variables shall be put into the stack frame, lets assume the SP is decremented by 40 bytes here. This is done using the<br />
<br />
stwu 1,-40(1) <br />
<br />
instruction. Hereby the current stack pointer passed from the calling function in GPR1 is stored at SP-40 which then becomes the back link field. Following that this instruction will set the SP in GPR1 to point to GPR1-40, the back link field. In this field the stack pointer of the previous stack frame set up by the calling function has just been stored before setting SP to SP-40. This sets up a linked list of stackframes. It allows to follow this list and write into the preceeding stack frame. GCC writes the link register value into the "LR save word" field of the preceeding stack frame with the<br />
<br />
stw 0,44(1) <br />
<br />
instruction after moving the link register into GPR0.<br />
<br />
In its own stack frame the called subroutine will store the link register in the "LR save word" field.<br />
<br />
If the stack frame has a size of 40 bytes, this results in 10 fields of word size. Subtracting two for the back chain and LR save word fields leaves eight words to save data in the stack frame. These fields could be addressed by:<br />
<br />
SP+8, SP+12, SP+16, SP+20, SP+24, SP+28, SP+32, SP+36<br />
<br />
In the "Hello world" example GCC saved GPR31 in SP+36, GPR3 in SP+28 and GPR4 in SP+24.<br />
<br />
To free the stack frame again the SP has to be set back to point to the back chain field of the previous stack frame. The instruction<br />
<br />
addi 1,1,40 <br />
<br />
will do this in our case here.<br />
<br />
GCC will call its exit function instead.<br />
<br />
[[Category:Development]]</div>KiraW