Tom Kelliher, CS 220
Sept. 5, 2007
Read 2.6.
Operands and instruction formats.
Compiling HLL control structures.
Operations, operands, and instruction formats.
The basics:
~)
&, not
&&).
|, not
||).
<< and >>). Complication: logical and
arithmetic shifts.
Details:
~(a | b).
How do you use NOR to get NOT?
~1101 = 0010
1101 & 1001 = 1001
and $s2, $t0 $t1
1001 | 0100 = 1101
11001101 << 3 = 01101000
Usage example: shift and mask operations in finding a character in a word. In C:
int charinstr(unsigned char c, unsigned int s)
{
int i;
for (i = 0; i < 4; i++)
{
if (c == (0xff & s))
return 1;
s >>= 8;
}
return 0;
}
charinstr(0xaa, 0xccddaabb);
charinstr(0xaa, 0xbbccdaad);
The complete set, all synthesized from beq, bne, and slt.
Branch instructions use a signed 16-bit offset field; hence they can jump
instructions (not bytes) forward or
instructions backwards. The jump instruction contains a 26 bit
address field (the third instruction format).
b labelBranch instruction
Unconditionally branch to the instruction at the label.
beq Rsrc1, Src2, labelBranch on Equal
Conditionally branch to the instruction at the label if the contents
of register Rsrc1 equals Src2.
beqz Rsrc, labelBranch on Equal Zero
Conditionally branch to the instruction at the label if the contents
of Rsrc equals 0.
bge Rsrc1, Src2, labelBranch on Greater Than Equal
bgeu Rsrc1, Src2, labelBranch on GTE Unsigned
Conditionally branch to the instruction at the label if the contents
of register Rsrc1 are greater than or equal to Src2.
bgez Rsrc, labelBranch on Greater Than Equal Zero
Conditionally branch to the instruction at the label if the contents
of Rsrc are greater than or equal to 0.
bgt Rsrc1, Src2, labelBranch on Greater Than
bgtu Rsrc1, Src2, labelBranch on Greater Than Unsigned
Conditionally branch to the instruction at the label if the contents
of register Rsrc1 are greater than Src2.
bgtz Rsrc, labelBranch on Greater Than Zero
Conditionally branch to the instruction at the label if the contents
of Rsrc are greater than 0.
ble Rsrc1, Src2, labelBranch on Less Than Equal
bleu Rsrc1, Src2, labelBranch on LTE Unsigned
Conditionally branch to the instruction at the label if the contents
of register Rsrc1 are less than or equal to Src2.
blez Rsrc, labelBranch on Less Than Equal Zero
Conditionally branch to the instruction at the label if the contents
of Rsrc are less than or equal to 0.
blt Rsrc1, Src2, labelBranch on Less Than
bltu Rsrc1, Src2, labelBranch on Less Than Unsigned
Conditionally branch to the instruction at the label if the contents
of register Rsrc1 are less than Src2.
bltz Rsrc, labelBranch on Less Than Zero
Conditionally branch to the instruction at the label if the contents
of Rsrc are less than 0.
bne Rsrc1, Src2, labelBranch on Not Equal
Conditionally branch to the instruction at the label if the contents
of register Rsrc1 are not equal to Src2.
bnez Rsrc, labelBranch on Not Equal Zero
Conditionally branch to the instruction at the label if the contents
of Rsrc are not equal to 0.
j labelJump
Unconditionally jump to the instruction at the label.
jal labelJump and Link
jalr RsrcJump and Link Register
Unconditionally jump to the instruction at the label or whose address
is in register Rsrc. Save the address of the next
instruction in register 31.
jr RsrcJump Register
Unconditionally jump to the instruction whose address is in register
Rsrc.