The few rules, the easier it is for the programmer (less things to remember...)
The arithmetic operations add, sub, smul and sdiv will thus operate on 32 bit values....
Syntax: add %r1, %r2, %r3
add %r1, constant, %r3 (-212 <= constant <= 212-1)
Effect: r3 = r1 + r2
r3 = r1 + constant
Examples:
Instruction Effect
=========== ==============
add %i0, %i2, %i3 Reg. i3 = Reg. i1 + Reg. i2
add %i0, %g0, %i2 Reg. i2 = Reg. i1
(because Reg. g0 = 0 !)
add %i0, 0, %i2 Reg. i2 = Reg. i1
add %g0, %g0, %i2 Reg. i2 = 0
add %g0, 1, %i2 Reg. i2 = 1
Usage of the "add" operation:
Syntax: sub %r1, %r2, %r3 sub %r1, constant, %r3 (-212 <= constant <= 212-1) Effect: sub %r1, %r2, %r3 ---> r3 = r1 - r2 sub %r1, constant, %r3 ---> r3 = r1 - constant Examples: Instruction Effect =========== ============== sub %i0, %i2, %i3 Reg. i3 = Reg. i1 - Reg. i2 sub %g0, %i0, %i2 Reg. i2 = - Reg. i1 (because Reg. g0 = 0 !)Usage of the "sub" operation:
Syntax: smul %r1, %r2, %r3 smul %r1, constant, %r3 (-212 <= constant <= 212-1) Effect: smul %r1, %r2, %r3 ---> (Y, r3) = r1 * r2 smul %r1, constant, %r3 ---> (Y, r3) = r1 * constant
NOTE:
Examples: Instruction Effect =========== ============== smul %i0, %i2, %i3 Register pair (Y,i3) = Reg. i1 * Reg. i2 smul %i0, 4, %i0 Register pair (Y,i0) = Reg. i0 * 4Usage of the "smul" operation:
Syntax: sdiv %r1, %r2, %r3 sdiv %r1, constant, %r3 (-212 <= constant <= 212-1) Effect: sdiv %r1, %r2, %r3 ---> r3 = (Y,r1) / r2 sdiv %r1, constant, %r3 ---> r3 = (Y,r1) / constant
NOTE:
Examples: Instruction Effect =========== ============== sdiv %i0, %i2, %i3 Reg. i3 = (Reg. Y, Reg. i1) / Reg. i2 If Reg. Y = 0: Reg. i3 = Reg. i1 / Reg. i2 sdiv %i0, 4, %i0 Reg. i0 = (Reg. Y, Reg. i0) / 4 If Reg. Y = 0: Reg. i0 = Reg. i0 / 4
Use the formula:
Suppose you want to calculate the remainder of the
division of A/B:
Remainder = A - Q * B where Q = A/B (quotient)
Example:
A = 13, B = 4 A / B = 3 (Quotient) A % B = 1 (Remainder) Verify for yourself that: Remainder = A - Q*B = 13 - 3*4 = 13 - 12 = 1I'll let you figure out how to write that little program...