Tom Kelliher, CS 220
Sept. 23, 2011
Operands and instruction formats.
Support for procedures.
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 charinword(unsigned char c, unsigned int w)
{
int i;
for (i = 0; i < 4; i++)
{
if (c == (0xff & w))
break;
w = w >> 8;
}
return 0;
}
charinword(0xaa, 0xccddaabb);
charinword(0xaa, 0xbbccdaad);
All branch instructions are 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).
Summary of branch instructions:
b label
Example:
b foo
...
foo: add $1, $1, $1
beqz, bnez, bgez, bgtz,
blez, bltz.
bnez $s0, label
beq, bne, bge, bgt,
ble, blt.
Unsigned versions.
bge $sp, $ra, foo blt $s0, 5, bar
Write MIPS code fragments corresponding to the following:
if (i < 12) ++i; else --j;
i = 1;
j = 0;
while (i < 200)
{
j += i;
i *= i;
}
Write MIPS code corresponding/solving each of the following:
j = 0; for (i = 0; i < 10; ++i) j += i;
j = 0;
for (i = 0; i < 10; ++i)
if (i > 5)
j += i;
while (i > 0 && i < 10) ++i;
if (i < 12 && j > 3 || k != 0) ++i; else if (i == 33) --j; else k += 2;
int i;
for (i = 0; i < 4; i++)
{
if (c == (0xff & w))
return 1;
w = w >> 8;
}
while (save[i] == k) i += k;requires execution of both a conditional branch and an unconditional jump each time through the loop. Produce the naive code.
Optimize the naive code so that only a conditional branch is executed each time through the loop.
$a0.
$a1.
$v0.