.text:91C034B4 li r11, 0x1E # Load Immediate
.text:91C034B8 divw r11, r27, r11 # Divide Word
.text:91C034BC mulli r11, r11, 0x1E # Multiply Low Immediate
.text:91C034C0 subf. r11, r11, r27 # Subtract from
.text:91C034C4 bne loc_91C034E0
Usually in PowerPC there is a compare instruction before a bne
(Branch if not equal) instruction. Could someone explain how this is compared or what is going on in the instructions above?
The BNE
instruction on Power, like many instruction sets with similar branching instructions, does not actually need a compare to function. On Power this instruction is simply checking the bits in the Condition Register (CR), testing whether certain bits are set or cleared.
In the case you are providing, the result of the subf
will modify the CR register depending upon the outcome. The programmer is relying on the side effect of this subtraction to perform the test.
In fact, this is usually equivalent to an actual compare instruction since a compare will typically perform a subtraction to determine which bits to set in CR. The programmer is simply doing two things at once.
I'd suggest that you have a look at chapter 2 in the PowerPC User Instruction Architecture reference, which covers the branch processor. In particular, take note of section 2.3.1:
For Compare instructions, a specified CR field is set to reflect the result of the comparison. The bits of the specified CR field are interpreted as follows. A complete description of how the bits are set is given in the instruction descriptions in Section 3.3.9, “Fixed- Point Compare Instructions” on page 58 and Section 4.6.7, “Floating-Point Compare Instructions” on page 113.
Bit Description:
0- Less Than, Floating-Point Less Than (LT, FL) For fixed-point Compare instructions, (RA) < SI or (RB) (signed comparison) or (RA)
1- Greater Than, Floating-Point Greater Than (GT, FG) For fixed-point Compare instructions, (RA) > SI or (RB) (signed comparison) or (RA) >u UI or (RB) (unsigned comparison). For floating-point Compare instructions, (FRA) > (FRB).
2- Equal, Floating-Point Equal (EQ, FE) For fixed-point Compare instructions, (RA) = SI, UI, or (RB). For floating-point Compare instructions, (FRA) = (FRB).
3- Summary Overflow, Floating-Point Unordered (SO, FU) For fixed-point Compare instructions, this is a copy of the final state of XERSO at the completion of the instruction. For floating-point Compare instructions, one or both of (FRA) and (FRB) is a NaN.
Note that this is how the bits are set after a compare and hence are then used by a branch instruction. If you check the reference for the subf
instruction you can see which CR
bits are also affected by the subtraction, allowing you to do a branch as though a compare has been done.