Homework assignment 3 Solution

$30.00

Description

Exercises:

2.4 [3 points] For the MIPS assembly instructions below, what is the corresponding C statement? Assume that the variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively.

sll $t0, $s0, 2 # $t0 = f * 4

add $t0, $s6, $t0 # $t0 = &A[f]

sll $t1, $s1, 2 # $t1 = g * 4

add $t1, $s7, $t1 # $t1 = &B[g]

lw $s0, 0($t0) # f = A[f]

addi $t2, $t0, 4

lw $t0, 0($t2)

add $t0, $t0, $s0

sw $t0, 0($t1)

2.31 [7 points] Implement the following C code in MIPS assembly. What is the total number of MIPS instructions needed to execute the function?

int fib(int n){

if (n = = 0)

return 0;

else if (n = = 1)

return 1;

else

return (fib(n−1) + fib(n−2));

}

2.34 [5 points] Translate function f into MIPS assembly language. If you need to use registers $t0 through $t7, use the lower-numbered registers first. Assume the function declaration for func is

int func(int a, int b). The code for function f is as follows:

int f(int a, int b, int c, int d) {

return func(func(a,b),c+d);

}

2.38 [2 points] Consider the following code:

lbu $t0, 0($t1)

sw $t0, 0($t2)

Assume that the register $t1 contains the address 0x1000 0000 and the register $t2 contains the address 0x1000 0010. Note the MIPS architecture utilizes big-endian addressing. Assume that the data (in hexadecimal) at address 0x1000 0000 is: 0x11223344. What value is stored at the address pointed to by register $t2?

2.39 [3 points] Write the MIPS assembly code that creates the 32-bit constant 0010 0000 0000 0001 0100 1001 0010 0100 base 2 and stores that value in register $t1.

Computer Project:

  1. [10 points] Write a program in MIPS assembly language to convert an ASCII number string containing positive and negative integer decimal strings, to an integer. Your program should expect register $a0 to hold the address of a null terminated string containing some combination of the digits 0 through 9. Your program should compute the integer value equivalent to this string of digits, then place the number in register $v0. If a non-digit character appears anywhere in the string, your program should stop with the value −1 in register $v0. For example, if register $a0 points to a sequence of three bytes 50 base 10, 52 base 10, 0 base 10 (the null terminated string “24”), then when the program stops, register $v0 should contain the value 24 base 10.