Exercise [Arrays ] Step 1: Create a new ASSEMBLY project using Keil Software Mak
ID: 3602705 • Letter: E
Question
Exercise [Arrays ]
Step 1: Create a new ASSEMBLY project using Keil Software
Make sure you use the M3 option
Make sure you name the folder “COSC2440_Lab06_YourFirstName_YourLastName”
Step 2: Copy the following template to your assembly file:
AREA Lab_06_YourFirstName_YourLastName, CODE, READONLY
EXPORT __main
__main
MOV R0, SP ; Replace ___ with base register.
MOV R1, #1 ; R1 will be used to increment the loop.
MOV R2, #20; R2 will represent the number of iterations.
MOV R3, #0 ; R3 will be used to hold the sum of the array.
storeValuesLoop
STR R1, [R0], #4;Store the value of R1 to address of R0, then auto increment the address by 4. Hint use STR instruction.
ADD R1, #1; Increment the Loop by adding 1 to R1
CMP R1, R2; Compare R1 & R2 to determine if we need to exit the loop or not
BLE storeValuesLoop; Stay in the loop if R1 is less than or equal to R2
MOV R1, #1 ; Reset R1 to start from the beginning again for the 2nd loop.
MOV R0, SP;
readValuesLoop
LDR R4, [R0], #4;Read the value of current iteration to R4. Hint use LDR instruction.
ADD R3, R4 ; Add the value your read [R4] to the sum [R3]
ADD R1, #1; Increment the Loop by adding 1 to R1
CMP R1, R2; Compare R1 & R2 to determine if we need to exit the loop or not
BLE readValuesLoop; Stay in the loop if R1 is less than or equal to R2
stop B stop
END
Step 3:
Change YourFirstName to your first name.
Change YourLastName to your last name.
This is the same program that we used for Lab 05.
Step 4: Modify storeValuesLoop from using the STR-Post-indexed-autoindexing-post-incrementing to STR-Pre-indexed-autoindexing-pre-incrementing.
You are ONLY allowed to modify the highlighted line inside the storeValuesLoop loop.
Step 5: Modify readValuesLoop from using the LDR-Post-indexed-autoindexing-post-incrementing to LDR- indexed-Double Register with Scaling.
You are ONLY allowed to modify the highlighted line inside the readValuesLoop loop.
Step 6: You should have the same sum result as we had in (the pic below)
Value Register Core 0x20001058 0x00000015 0x00000014 0x000000D2 Ox00000014 RO 0 R4Explanation / Answer
__main
MOV R0, ___; Replace ___ with base register.
MOV R1, #1 ; R1 will be used to increment the loop.
MOV R2, #20; R2 will represent the number of iterations.
MOV R3, #0 ; R3 will be used to hold the sum of the array.
storeValuesLoop
.text
main:
j mm
mm:
la $a3, array_A # base address for array_A loaded into $a3
la $a1, array_B # base address for array_B loaded into $a1
la $a2, array_C # base address for array_C loaded into $a2
li $t1, four # $t1 = four (row-size and loop end)
li $s0, zero # i = 0; initialize first for loop
loop1:
li $s1, zero # j = 0; restart second for loop
loop2:
li $s2, zero # k = 0; restart third for loop
sll $t2, $s0, two # $t2 = i * four (size of row of c)
addu $t2, $t2, $s1 # $t2 = i * size(row) + j
sll $t2, $t2, 2 # $t2 = byte offset of [i][j]
addu $t2, $a2, $t2 # $t2 = byte offset of [i][j]
lw $t4, 0($t2) # $t4 = 2 bytes of c[i][j]
loop3:
sll $t0, $s2, 2 # $t0 = k * 4 (size of row of b)
addu $t0, $t0, $s1 # $t0 = k * size(row) + j
sll $t0, $t0, 2 # $t0 = byte offset off [k][j]
addu $t0, $a1, $t0 # $t0 = byte address of b[k][j]
lw $t5, 0($t0) # $t5 = 2 bytes of b[k][j]
sll $t0, $s0, 2 # $t0 = i * 4 (size of row of a)
addu $t0, $t0, $s2 # $t0 = i * size(row) + k
sll $t0, $t0, 2 # $t0 = byte offset of [i][k]
addu $t0, $a3, $t0 # $t0 = byte address of a[i][k]
lw $t6, 0($t0) # $t6 = 2 b
ADD R1, #1; Increment the Loop by adding 1 to R1
CMP R1, R2; Compare R1 & R2 to determine if we need to exit the loop or not
BLE storeValuesLoop; Stay in the loop if R1 is less than or equal to R2
MOV R1, #1 ;
addi $a2, $a2, 4
bne $s1, $t1, loop2 # if (j != 4) visit loop2
addiu $s0, $s0, one # $i = i + one
bne $s0, $t1, loop1 # if (i != 32) visit L1
Exit:
li $v0, 10 #exits
syscall
.data
array_A: .word 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
array_B: .word 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
array_C: .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
output_row_string_C: .asciiz "Matrix C Output Row "
colon_string: .asciiz ":
space_string: .asciiz " "
new_row: .asciiz " "
char_space: .space 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.