Computer Composition and Design Homework 4
2.26
Consider the following MIPS loop:
LOOP: slt $t2, $0, $t1 # 0<t1则t2=1 else 0
beq $t2, $0, DONE # t2==0则跳转到DONE
subi $t1, $t1, 1 # t1 = t1 - 1
addi $s2, $s2, 2 # s2 = s2 + 2
j LOOP # 跳到LOOP
DONE:
2.26.1 [5] <§2.7> Assume that the register $t1 is initialized to the value 10. What is the value in register $s2 assuming $s2 is initially zero?
s 2 = 20 s2=20 s2=20
2.26.2 [5] <§2.7> For each of the loops above, write the equivalent C code routine. Assume that the registers $s1, $s2, $t1, and $t2 are integers A, B, i, and temp, respectively
转换成等效的C语言为
int i = 10, B = 0;
while(i > 0)
{
i--;
B+=2;
}
如果需要一一对应的话
int B = 0;
for(int i = 10; (A = i > 0); i--)
{
//这里的判定条件是先让 i>0赋值给A再由A判定
B += 2;
}
2.26.3 [5] <§2.7> For the loops written in MIPS assembly above, assume that the register $t1 is initialized to the value N. How many MIPS instructions are executed
N每次成功-1说明做了5个语句,N==0执行第二行语句进行跳转
所以应该为
N ∗ 5 + 2 N*5+2 N∗5+2
2.27
[5] <§2.7>
Translate the following C code to MIPS assembly code. Use a minimum number of instructions. Assume that the values of a, b, i, and j are in registers $s0, $s1, $t0, and $t1, respectively. Also, assume that register $s2 holds the base address of the array D.
for(i=0; i<a; i++)
for(j=0; j<b; j++