Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Name your source code file hw02-05.s. Write the code for the .data section th

ID: 3563174 • Letter: 1

Question

1. Name your source code file hw02-05.s. Write the code for the .data section that would,

Define a global int variable named n which is not initialized (however, because n is located in the data segment,

and because the data segment is initialized to all zero bytes, n will be implicitly initialized to 0).

2. Define an array of sixteen ints named f initialized to { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610 }.

3. Define a string named s_prompt initialized to: "Enter n (0 <= n <= 15): ".

4. Define a string named s_out1 initialized to: "f(".

5. Define a string named s_out12 initialized to: ") = ".

6. Continuing, the array f contains the first sixteen Fibonacci numbers1

, i.e., f(0) = 0, f(1) = 1, f(2) = 1, f(3) = 2, ...,

f(15) = 610, where f(n) = f(n-2) + f(n-1) for n > 1. Now, write instructions in the .text section of hw02-05.s that will

implement this pseudocode (which prompts the user to enter n and then displays f(n)):

int n;

int f[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610 };

main

PrintString("Enter n (0 <= n <= 15): ")

n = ReadInt()

PrintString("f(")

PrintInt(n)

PrintString(") = ")

PrintInt(f[n])

Exit()

end main

make sure to call the Exit() system call at the end of your program.

7. Name your source code file hw02-07.s. The C and C++ languages support a programming construct referred to as a struct (short for structure). A struct is a collection of one or more data members ( think of a struct as being a class that declares data members but there are no functions in a struct). Here is an

example C struct variable definition for a variable named point,

struct {

int x;

int y;

int z;

} point;

The point structure variable contains three data members which are the x-, y-, and z-coordinates of a point in three

dimensional space. In C, we access the data members of a struct variable this way,

point.x = 10;

point.y = 20;

point.z = 30;

In C the name of the struct variable is equivalent to the address in memory of the first byte of the first data member,

for example,

+--------------------+

| ... |

+--------------------+

| z | 0x1001_0008

+--------------------+

| y | 0x1001_0004

+--------------------+

| x | 0x1001_0000 <== point

+--------------------+

| ... |

+--------------------+

In this memory diagram, the address of point is 0x1001_0000 which is also the address of the x data member. point

would be defined in the .data section as consuming 12-bytes of memory (4-bytes per int times three int's),

.data

point: .space 12

Note that there are no labels associated with x, y, and z, and that the address of point is the address of x. We can, of

course, easily determine the addresses of y and z: the address of y is point + 4 and the address of z is point + 8.

Given two points in three dimensional space, we can calculate the dot product of the two points,

dot_product = (point1.x

Explanation / Answer

#hw02-05.s

.data
n: .space 4 #4 bytes reserved for int n
f: .word 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610
s_prompt: .asciiz "Enter n (0 <= n <= 15):"
s_out1: .asciiz "f("
s_out12: .asciiz ") = "

.text
main :
# print the string
li $v0,4
la $a0,s_prompt
syscall
# Get integer n and store in n
li $v0,5
syscall
#load address of n
la $t0,n
#store the read value at n
sw $v0,0($t0)

#$t1 is set to the first address of the array
la $t1,f
#read n
lw $t2,0($t0)
#as each element in array is 4 bytes, address of f[n] = address of f[0] + 4*n
#find 4*n
add $t2,$t2,$t2
add $t2,$t2,$t2

#f[0] + 4*n
add $t1,$t1,$t2

#read f[n] to $t1
lw $t1,0($t1)

# print the string
li $v0,4
la $a0,s_out1
syscall

# print the value of n
li $v0,1
lw $a0,0($t0)
syscall

# print the string
li $v0,4
la $a0,s_out12
syscall

# print the value of f[n]
li $v0,1
move $a0,$t1
syscall

#exit
li $v0,10
syscall

#hw02-07.s
.data
point1: .space 12
point2: .space 12
s_enterx: .asciiz "Enter x: "
s_entery: .asciiz "Enter y: "
s_enterz: .asciiz "Enter z: "
s_out: .asciiz "The dot product is "

.text
main :
# print the string
li $v0,4
la $a0,s_enterx
syscall
# Get value of point1.x and store
li $v0,5
syscall
#load address of n
la $t0,point1
#store the read value at point1.x
sw $v0,0($t0)


# print the string
li $v0,4
la $a0,s_entery
syscall
# Get value of point1.y and store
li $v0,5
syscall
#store the read value at point1.y
sw $v0,4($t0)


# print the string
li $v0,4
la $a0,s_enterz
syscall
# Get value of point1.z and store
li $v0,5
syscall
#store the read value at point1.z
sw $v0,8($t0)

# print the string
li $v0,4
la $a0,s_enterx
syscall
# Get value of point2.x and store
li $v0,5
syscall
#load address of point2.x
la $t0,point2
#store the read value at point1.x
sw $v0,0($t0)


# print the string
li $v0,4
la $a0,s_entery
syscall
# Get value of point2.y and store
li $v0,5
syscall
#store the read value at point1.y
sw $v0,4($t0)


# print the string
li $v0,4
la $a0,s_enterz
syscall
# Get value of point2.z and store
li $v0,5
syscall
#store the read value at point1.z
sw $v0,8($t0)

#read address of point1 and point2
la $t0,point1
la $t1,point2

#load point1.x to $t2 and point2.x to $t3
lw $t2,0($t0)
lw $t3,0($t1)

#multiplyt $t2, $t3
mult $t2,$t3
#$t4 has the multiplied value
mflo $t4

#load point1.y to $t2 and point2.y to $t3
lw $t2,4($t0)
lw $t3,4($t1)

#multiplyt $t2, $t3
mult $t2,$t3
#$t5 has the multiplied value
mflo $t5
#update the dot product in $t4
add $t4,$t5,$t4

#load point1.y to $t2 and point2.y to $t3
lw $t2,8($t0)
lw $t3,8($t1)

#multiplyt $t2, $t3
mult $t2,$t3
#$t5 has the multiplied value
mflo $t5
#update the dot product in $t4
add $t4,$t5,$t4

# print the string
li $v0,4
la $a0,s_out
syscall

# print dotproduct stored in $t4
li $v0,1
move $a0,$t4
syscall

#exit
li $v0,10
syscall