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

USING PIC24 LANGUAGE Given two matrices below, A and B, of exact same dimension:

ID: 3837746 • Letter: U

Question

USING PIC24 LANGUAGE

Given two matrices below, A and B, of exact same dimension:

Matrix A:

Address Byte0 Byte1 Byte2 Byte3 Byte4 Byte5

0x1100 2 4 6 8 10 12

0x1110 14 16 18 20 22 24

0x1120 26 28 30 32 34 36

0x1130 38 40 42 44 46 48

Matrix B =

Address Byte0 Byte1 Byte2 Byte3 Byte4 Byte5

0x1200 1 3 5 7 9 11

0x1210 13 15 17 19 21 23

0x1220 25 27 29 31 33 35

0x1230 37 39 41 43 45 47

Write a simple Assembly program to perform the following tasks:

1. Initialize Matrix A & B at the specified memory address.

2. Initialize Matrix C (same dimension) with all zero's at location 0x1300.

3. Compute C = A + B. (The result should be stored at location 0x1300). Your program MUST utilize at least the followings assembly features: - Immediate addressing - INC instruction - Indirect Addressing Mode

****More important question below****

Rewrite previous problem with subroutines. Your program should include at least two subroutines as described below and all parameters must be passed using registers W0-W7.

Matrix Init subroutine: This subroutine is called to initialize a matrix. The parameters to be passed to this subroutine are:

- W0: Matrix starting address (0x1100 for A, 0x1200 for B, and 0x1300 for C)

- W1: Row size in bytes (16 in this assignment)

- W2: Number of entries in use per row (6 for this assignment)

- W3: Number of rows in matrix (4 for this assignment)

- W4: Starting value to initialize matrix entry (2, 1, 0 for Matrix A, B, C respectively)

- W5: Increment value (2 for Matrix A and B and 0 for C)

Matrix Add subroutine: This subroutine is called to add two matrices and save to a 3rd matrix. The parameters to be passed to this subroutine are:

- W0: Starting address of first matrix to add

- W1: Starting address of second matrix to add

- W2: Starting address of third matrix where the sum will be stored

- W3: Row size in bytes (16 in this assignment)

- W4: Number of entries in use per row (6 in this assignment)

- W5: Number of rows (4 in this assignment)

Explanation / Answer

.extern printf
.extern scanf
.global main

.text

main:
push {ip, lr}

@--read lines and columns of matrix A
ldr r0, =scanf2
ldr r1, =linesA
ldr r2, =columnsA
bl scanf

@--read all values of matrix A
ldr r4, =linesA
ldr r4, [r4]
ldr r5, =columnsA
ldr r5, [r5]
mul r6, r4, r5
ldr r7, =matrixA
loop1:
cmp r6, #0
ble endloop1
ldr r0, =scanf1
mov r1, r7
bl scanf
sub r6, r6, #1
add r7, r7, #4
b loop1
endloop1:

@--read lines and columns of matrix B
ldr r0, =scanf2
ldr r1, =linesB
ldr r2, =columnsB
bl scanf

@--check if matrices are compatible
ldr r6, = linesB
ldr r6, [r6]
ldr r7, = columnsB
ldr r7, [r7]
cmp r5, r6
bne abort
  
@--read all values of matrix B
ldr r4, =linesB
ldr r4, [r4]
ldr r5, =columnsB
ldr r5, [r5]
mul r6, r4, r5
ldr r7, =matrixB
loop2:
cmp r6, #0
ble endloop2
ldr r0, =scanf1
mov r1, r7
bl scanf
sub r6, r6, #1
add r7, r7, #4
b loop2
endloop2:
  
@--load all the necessary numbers on registers r4-r9
ldr r4, =matrixA
ldr r5, =linesA
ldr r5, [r5]
ldr r6, =columnsA
ldr r6, [r6]
ldr r7, =matrixB
ldr r8, =linesB
ldr r8, [r8]
ldr r9, =columnsB
ldr r9, [r9]

@--perform calculations and output result
mov r0, #0
forLinesA:
cmp r0, r5
bge endForLinesA
mov r1, #0
forColumnsB:
cmp r1, r9
bge endForColumnsB
mov r2, #0
mov r3, #0
multiply:
mov r12, #4
cmp r3, r6
bge endMultiply
@--load first element, from matrixA
mul r10, r0, r6
add r10, r10, r3
mul r11, r10, r12
add r11, r11, r4
ldr r10, [r11]
@--load second elements, from matrixB
mul r11, r9, r3
add r11, r11, r1
push {r0}
mul r0, r11, r12
add r0, r0, r7
ldr r11, [r0]
pop {r0}
@--multiply and add to result variable
mul r12, r10, r11
add r2, r2, r12
@--loop
add r3, r3, #1
b multiply
endMultiply:
@--printf result
push {r0-r12}
ldr r0, =printfResult
mov r1, r2
bl printf
pop {r0-r12}
@--if necessary print white space
sub r10, r9, #1
cmp r1, r10
bge endif
push {r0-r12}
ldr r0, =printfSpace
bl printf
pop {r0-r12}
endif:
@--loop
add r1, r1, #1
b forColumnsB
endForColumnsB:
@--loop
sub r10, r5, #1
cmp r0, r10
bge endif2
push {r0-r12}
ldr r0, =printfNewline
bl printf
pop {r0-r12}
endif2:
add r0, r0, #1
b forLinesA
endForLinesA:

b end

@--abort, imcompatible matrices
abort:
ldr r0, =printfError
bl printf

@--normal end of program
end:

mov r0, #0
pop {ip, pc}

  
.data
linesA: .word 0
columnsA: .word 0
linesB: .word 0
columnsB: .word 0
matrixA: .space 400
matrixB: .space 400
scanf1: .asciz "%d"
scanf2: .asciz "%d %d"
printfError: .asciz "Matrizes Incompativeis.n"
printfResult: .asciz "%05d"
printfSpace: .asciz " "
printfNewline: .asciz "n"
debug: .asciz "elemento = %dn"
debug2: .asciz "offset = %dn"