Professional Documents
Culture Documents
LAB 3 Week-04
The following code sums the first ten positive integers (10+9+8+7+...), Figure out the
missing parts in the code, fill them up and run the program. Use only the registers mentioned in
this program, i.e., $s0, $t0, $zero.
Exercise 1
Code:
.text
.globl main
main:
addi $s0, $zero, 10 # counter initialized to 10
addi $t0, $zero, 0 # sum/accumulator initialized to 0
loop1: # fill missing values
add $t0 , $t0 , $s0 # Performing addition in $t0
addi $s0 , $s0 , -1 # counter--
bne $s0 , $zero ,loop1 # Exit loop after 10th iteration
nop
# Few no operations to indicate end of interestnop
What is the value of $t0?Hex: 0x00000037, Decimal: 55 when we reach the end of the program (first
nop).
Exercise 2
Modify the program in Exercise 1 to calculate (1+2+4+8+16+32+….), for the first 20
numbers. Write your code below; store the value of the sum in $t0. Use the registers mentioned
before in Exercise-1, i.e., $s0, $t0, $zero. In addition to these registers, you can only use $t1. Store
the final (and intermediate) sum in $t0. Multiple implementations are possible. Write only one.
Code:
.text
.globl main
main:
The program above calculated the sum of 20 numbers. What is the value of the final sum:
Hex: 0x7fffffff, Decimal: 2147483647
Modify the program to calculate the sum of more numbers (say 50 numbers). What is the value of
the intermediate sum contained in $t0 when an overflow occurs in any of the registers involved:
. How do you know that an overflow event has happened? Because when we
again jump to loop the value in $t0 is -1 even the counter is not equal to zero. At this point we get an
Repeat the above program (of the same 50 numbers) but this time use the addu instruction while
calculating the sum instead of the add instruction. Now what is the first value of the intermediate
sum for which overflow occurs in any of the registers involved . Does the overflow
occur at the same point? If it does not, then why not?
You need to show the program and its working to the Instructor
Exercise 3
Without using branches, write a program that sums the integers in an array of 5 elements and then
saves the result in “sum”. The starting code is given below:
Code:
.text
.globl main
main:
la $s0, myData # load address of myData into $s0
li $t2, 0 # initialize $t2 to save the sum
lw $t0, 0($s0)
add $t2, $t2, $t0
lw $t0, 4($s0)
add $t2, $t2, $t0
lw $t0, 8($s0)
add $t2, $t2, $t0
lw $t0, 12($s0)
add $t2, $t2, $t0
lw $t0, 16($s0)
add $t2, $t2, $t0
What is the value of sum (in $t2 and stored in memory at location “sum”) after running this
program Hex: 0x00000014; Decimal: 20
Observe the data segment and write the address of myData (hex): 0x10010000
What is the address of the first element of the array in the data segment: 0x10010000
What is the address of the third element of the array in the data segment: 0x10010008
The value stored at the above address (Hex): 0xfffffffb (Decimal): −5
Observe how INT variables are stored in data area, what format are they stored in word
Check that after you run the first instruction of the program, $s0 is indeed loaded with the
correct address of the array my Data using the special assembler generated instruction. We
may not discuss la (load address instruction) any further.
What address is loaded for sum in $s0 at the end of the program 0x10010014
Fill up the missing values in the following table:
You need to show the program and its working to the Instructor
Exercise 4
Repeat Exercise 3 above but this time use branch instruction(s) and the following 15 element
array.
Code:
.data
myData: .word 200, -1299, -5000, 7123, 4, -2, 3, -7, 89,
4, -1000, 11, 0, 14, -1
sum: .word 0 # this location will contain the sum
.text
.globl main
main:
la $s0, myData # load address of myData into $s0
li $t1, 15 # initialize the index
add $t2, $zero, $zero # initialize to save the sum in $t2
loop:
addi $t1, $t1, -1
sll $t3, $t1, 2
add $t3, $s0, $t3
lw $t0, 0($t3)
add $t2, $t2, $t0
bne $t1, $zero, loop
exit:
la $s0, sum #Load the address of “sum” into $s0
Fill in these blanks and show the working of your program to the Instructor.
What is the value of sum :Hex: 0x0000008b; Decimal: 139
What branch instruction is produced by the assembler? bne $9, $0, −20
What is the branch offset: The number of words between branch instruction and branch target.
Loop-0x00400044