begin: addi $t0, $zero, 0 addi $t1, $zero, 1 loop: slt $t2, $a0, $t1 bne $t2, $zero, finish add $t0, $t0, $t1 addi $t1, $t1, 2 j loop finish: add $v0, $t0, $zero
start: beq $s1, $s2, exit add $t0, $s2, $s2 add $t0, $t0, $t0 add $t0, $t0, $s0 lw $t1, 0($t0) add $t1, $t1, $t1 sw $t1, 0($t0) addi $s2, $s2, 1 j start exit:
Address | Initial contents |
First time at "j start" |
Second time at "j start" |
... |
---|---|---|---|---|
400 | 11 | |||
404 | 23 | |||
408 | 63 | |||
412 | 128 | |||
416 | 911 |
Register | Initial contents |
First time at "j start" |
Second time at "j start" |
... |
---|---|---|---|---|
$s0 | 400 | |||
$s1 | 5 | |||
$s2 | 0 | |||
$t0 | N/A | |||
$t1 | N/A |
Trace through the MIPS code above. Each
time you reach the "j start" line, write down the contents of
addresses 400-416 and registers $s0, $s1, $s2, $t0 and $t1 by
filling in the tables above (you might need to add more columns
to the tables). In one sentence describe what this program
does? (Note: the "addi" instruction is just like "add" except
it allows you to add in a small constant directly rather than
specifying a second register. For example, if $s0 contains 3,
then after 'add $s2, $s0, 5' executes $s2 will contain 8.)
Extra Credit: Both a conditional branch ("beq $s1, $s2, exit")
and an unconditional jump ("j start") are executed each time through
the loop. Only poor compilers would produce code with this loop
overhead. Rewrite the assembly code so that it uses at most one
branch or jump each time through the code. How many instructions are
executed before and after the optimization? (Note: You may find it
necessary to put one or more instructions before the "start" label.)
In problems 4-6, resist the temptation to use a calculator that does the conversion
between binary, decimal, and hexadecmial automatically.
loop: lw $v1, 0($a0) #read next word from source addi $v0, $v0, 1 #increment count words copied sw $v1, 0($a1) #write to destination addi $a0,$a0,1 #advance ptr to next source addi $a1, $a1, 1 #advance ptr to next dest bne $v1, $zero,loop #loop if word copied not zero
x[10] = x[11]+c;
add $t1, $t2, $t3 addi $s0, $t1, 3 sw $t3, 32($t2)