Come up with an algorithm that does the following things: 1) It calculates the s
ID: 3808449 • Letter: C
Question
Come up with an algorithm that does the following things:
1) It calculates the sum of the integers present in an array of size 100. The array starts at the memory location starting from 0x4000, and the array elements are present in consecutive memory locations.
2) The final sum is stored in the following way:
a) If the final sum is positive or zero, it is stored in the memory location 0x3050.
b) If the final sum is negative, it is stored in the memory location 0x3051.
3) Terminate. Your solution should use a looping construct. Note: The memory locations can have both positive and negative integers. Assume that no overflow occurs while adding the elements, and the sum can always be represented as a 16-bit 2’s complement number.
a) Show the algorithm as a flowchart by decomposing it into its basic constructs.
b) Convert the above algorithm to an LC-3 program. Write the program in LC-3 binary code. Comment each line of code. The program should start at memory location x3000.
Explanation / Answer
As no specific algorithm was mentioned for first question ; so I wrote in MIPS algorithm
.data
pos_memory .word 0x3050
neg_memory .word 0x3051
.text
main:
li $s1,0x4000 #for array address
li $s0,100 #for array dimension
li $t0,0 #for sum
li $t1,0 #for elements
loop:
lw $t1,$s1
add $t0,$t0,$t1
addi $s1,$s1,4
subi $s0,$s0,1
bnz loop
li $t1,0
blt $t0,$t1,NEGATIVE
sw $t0,pos_memory
j DONE
NEGATIVE:
sw $t0,neg_memory
DONE:
li $v0,10
syscall