You are on page 1of 6

CS-212 Computer Organization & A.L.

LAB 3 Week-04

CS-212 - Computer Organization & A.L – LAB 3


Date: ; Lab Section:
Instructor: Amna K

Student Roll Number: Total Marks: 100

PCSPIM: Control Instructions and working with Data (Arrays)

TASK 1 - First Control Instruction: Summing the first ten integers

Note: Fill all blanks

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).

GCWUS CS Department Fall Semester 2021 Page 1 of 6


CS-212 Computer Organization & A.L. LAB 3 Week-04

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:

addi $s0,$zero,20 # counter initialized to 20


addi $t0,$zero,0 # sum initialized to 0
addi $t1,$zero,1 # $t1 initialized with 1
loop1:

add $t0,$t0,$t1 # Performing addition in $t0


add $t1,$t1,$t1 # Calculating new number to added
addi $s0,$s0,-1 # counter--
bne $s0,$zero,loop1 # Exit loop after 20th iteration
exit:
li $v0,10 # End of program
syscall

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

GCWUS CS Department Fall Semester 2021 Page 2 of 6


CS-212 Computer Organization & A.L. LAB 3 Week-04

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

Task 2: Summing the elements of an array


Objectives: Please read and tick each of the following six points after understanding them while doing
Exercise 3 on the next page.
1. Understand working of DATA area in memory and how data is stored there ✓
2. Understand that there are always two steps to access data in memory:
(a) Its 32-bit address in a register✓.
(b) Bringing its value in a register✓.
3. Base address of an Array and Array traversing✓.
4. Understand that Array name is the name of address of the first element in an array. Each
element is 4 byte addresses apart (byte addressing) ✓.
5. The address 12($s0) has a value Hex:0x00000007,Decimal:7 where $s0 contains the address of
myData.
6. The address 36($s0) has a value ✓

GCWUS CS Department Fall Semester 2021 Page 3 of 6


CS-212 Computer Organization & A.L. LAB 3 Week-04

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:

.data # This is how data is initialized in PCSpim


myData: .word 2, 12, -5, 7, 4 # array initialization
sum: .word 0 # this will contain the sum

.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

la $s0, sum #Load the address of “sum” into $s0


sw $t2, 0($s0) #Store the value of final sum in $t2 to the
#memory location at “sum”

GCWUS CS Department Fall Semester 2021 Page 4 of 6


CS-212 Computer Organization & A.L. LAB 3 Week-04

After completing Exercise 3 fill the following blanks:

 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:

Address (Hex) Value (Hex) Value (Decimal)


0x10010000 0x00000002 2
0x10010004 0x0000000c 12
0x10010008 0xfffffffb -5
0x1001000c 0x00000007 7
0x10010010 0x00000004 4

You need to show the program and its working to the Instructor

TASK 3: Finding sum using branch instructions

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

GCWUS CS Department Fall Semester 2021 Page 5 of 6


CS-212 Computer Organization & A.L. LAB 3 Week-04

.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

sw $t2, 0($s0) #Store the value of the final sum to memory

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

GCWUS CS Department Fall Semester 2021 Page 6 of 6

You might also like