Prob Set 4 Language of The Computer
Prob Set 4 Language of The Computer
Exercise 1
For these problems, the table holds some C code. You will be asked to evaluate these C code
statements in MIPS assembly code.
1.1 For the table above, draw a control-flow graph of the C code.
Solution:
a.
b.
1.2 For the table above, translate the C code to MIPS assembly code. Use a minimum number of
instructions. Assume that the value a, b i, j are in registers $s0, $s1, $t0, $t1, respectively. Also,
assume that register $s2 holds the base address of the array D.
Solution:
For these problems, the table holds MIPS assembly code fragments. You will be asked to
evaluate each of the code fragments, familiarizing you with the different MIPS branch
instructions.
Solution:
Solution:
a. 351
b. 601
1.5 Translate the loops above into C. Assume that the C-level integer i is held in register $t1, $s2
holds the C-level integer called result, and $s0 holds the base address of the integer MemArray.
Solution:
a. j = 0;
for (i=50; i>0; i--){
result += MemArray[j];
result += MemArray[j+1];
j += 2;
b. j = 0;
for (i=0; i<100; i++){
result += MemArray[j];
j += 1;
1.6 Rewrite the loop in MIPS assembly to reduce the number of MIPS instructions executed.
Solution:
Exercise 2
Assume that the stack and the static data segments are empty and that the stack and global
pointers start at address 0x7fff fffc and 0x1000 8000, respectively. Assume the calling
conventions as specified in Figure 2.11 and that function inputs are passed using registers
$a0 -$a3 and returned in register $v0. Assume that leaf functions may only use saved registers.
a. int my_global = 100
main()
{
int x = 10;
int y = 20;
int z;
z = my_function(x, y);
}
int my_function (int x, int y)
{
return x – y + my_global;
}
2.1 Write MIPS assembly code for the code in the table below.
Solution:
a. MAIN: addi $sp, $sp, -4
sw $ra, ($sp)
addi $a0, 0, 10
addi $a1, $0, 20
jal FUNC
add $t2, $v0, $0
lw $ra, ($sp)
addi $sp, $sp, 4
jr $ra
#
FUNC: lw $t1, 0($s0), #assume $s0 has global variable base
sub $t0, $a0, $a1
addi $v0, $t0, $t1
jr $ra
lw $ra, ($sp)
addi $sp, $sp, 4
jr $ra
Exercise 3
This exercise explores ASCII and Unicode conversion. The following table shows strings of
characters.
a. Hello world
b. 0123456789
3.2 The following table shows hexadecimal ASCII character values. Translate the hexadecimal
ASCII values to text.
a. 41 44 44
b. 4D 49 50 53
Solution:
a. ADD
b. MIPS