- You will need +, -, * and / to translate
assignment statements and get operands
(e.g., array elements)
- Notice that SPARC is a modern architecture and
exhibits the uniform/regular property:
- There are no exception rules on what operand to use with an instruction
- Instruction formats are "regular"
The few rules, the easier it is for the programmer
(less things to remember...)
- SPARC only perform 32 bit operations.
In fact, when a byte or a half word (16 bits) is fetched
into a register by ldsb or ldsh, the register
is automatically sign-extended to 32 bits.
So everything in SPARC's registers will be in 32 bit representation.
The arithmetic operations add, sub, smul and sdiv will
thus operate on 32 bit values....
- add: You have seen this before...
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:
- Add operands in 2 registers
- Add operand in a register and a constant
- Copy a register to another register (use 0 or g0 as second operand)
- Move a small constant into a register
(g0 as first operand)
- sub: subtract
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:
- Subtract operands in 2 registers
- Subtract operand in a register and a constant
- Negate a value in a register !
(You guess it... there is no "negate" instruction in SPARC)
- smul: Multiply
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:
- Recall SPARC smul operates on 32 bit values.
- When you multiply two 32-bits numbers, the result can be 64 bits long...
- The lower portion of the product is contained by register r3
specified in the smul instruction
- The upper portion of the product is contained by the multiply
help register Y
(Register Y was introduced in the architecture lecture:
click here)
- Often, we multiply small numbers (like 4*i where i is a small index
in an array).
In such cases, the register Y is equal to ZERO and
we don't bother with it.
Only the value in register "r3" is relevant.
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 * 4
Usage of the "smul" operation:
- Multiply operands in 2 registers
- Multiply operand in a register and a constant - very useful
when you need to access an array element (calculate the offset
with a multiplication)
- sdiv: Divide
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:
- Recall SPARC sdiv operates on 32 bit values.
- The sdiv instruction divides a 64 biy value in the register pair
(Y, r1) by the 32 bit value in register r2 or the 13 bit constant.
- The result is the quotient
- The remainder is not available.
- Often, we divide small numbers and register Y is already ZERO.
In such cases, we don't bother with register Y at all...
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
- To obtain the remainder of a division, you have to write
a small program to calculate it.
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 = 1
I'll let you figure out how to write that little program...