Name: Box: Date:
HW8 solution
-
In the following assembly code fragment, registers $t1 and $t2 are initialized to N and zero respectively.
LOOP: slt $t3, $0, $t1 beq $t3, $0, DONE addi $t1, $t1, -1 addi $t2, $t2, 3 j LOOP DONE:
a. (2 points) What is the value in register $t2 for when the code reaches the label DONE:?
$$ 3N$$b. (2 points) How many MIPS instructions are executed?
$$ 5N + 2$$c. (6 points) If the registers $t1, $t2, and $t3 correspond to variables i, a, and t respectively, write the equivalent C code for the loop. Note: you probably won't end up with a t variable in your answer.
a = 0; i = N; while(0 < i) { //same as i >= 0 i--; a = a + 3; }
or
a = 0; for( i=N; 0<i; i-- ) a += 3;
-
(20 points) Below is a snippet from a Checkers game program in C.
void setKing(int row, int col, int isKing) { int* rowAddr = getRowAddr(row); //returns the memory address of row rowAddr[col] = isKing; }
Fill in the missing portions of the
setKing
function below, adhering to the MIPS procedure call conventions. You should assumesetKing
is called according to MIPS convention.setKing: #entry point to setKing procedure addi $sp, $sp, -12 #allocate space on stack sw $a1, 0($sp) #backup needed arg values, since they sw $a2, 4($sp) # would be lost after jal sw $ra, 8($sp) #backup return address, since we do jal jal getRowAddr #call to getRowAddr lw $ra, 8($sp) #restore values from stack lw $a2, 4($sp) lw $a1, 0($sp) addi $sp, $sp, 12 #deallocate space on stack sll $a1, $a1, 2 #multiply col by 4 to get ready for address add $v0, $a1, $v0 #build address from base and col offset sw $a2, 0($v0) #store king value at row+col location jr $ra #return form setKing