Matrix Transposition in CUSP Assembly Language Homework Sample

You need to write a routine than transposes an array, so
[ 1 2
3 4
5 6 ] becomes

[ 1 3 5
2 4 6 ]

The arrays are stored in row order format. You should push the address of the array to transpose, the number of rows, the number of columns and finally the destination address for the array where the result will be stored. For more assembly language programming assignments contact us for details.

Solution:

.EQU @,$000 ; main program start address
main:
LDS# $E00 ; initialize stack to $E00

PSH# array1 ; address of array to transpose
PSH rows ; number of rows
PSH columns ; number of columns
PSH# array2 ; address of transposed array
JSR Transpose ; transpose array

ADS# 4 ; restore stack pointer

HLT ; terminate program

; Variables used in main
array1: .word 1
.word 2
.word 3
.word 4
.word 5
.word 6
array2: .blkw 6,0
rows: .word 3
columns: .word 2

; Transpose subroutine
.EQU @,$020 ; subroutine start address
.EQU startArray,5
.EQU nRows,4
.EQU nColumns,3
.EQU transArray,2
Transpose:
PSHF ; push FP on stack
TSF ; load stack pointer in FP

CLR posA ; point to initial position in first array

CLR I ; initialize row index to zero
forRow:
LDA I ; load current row
STA posB ; set as initial position in second array
CLR J ; initialize column index to zero
forCol:
LDX posA ; load current position in first array
LDA& ! startArray ; load value from array
ADX# 1 ; advance to next position in array
STX posA ; save new position of first array

LDX posB ; load current position in second array
STA& ! transArray ; load value from array
ADX ! nRows ; advance to next position in array
STX posB ; save new position of second array

INC J ; increment column index
LDA J ; load index in ACC
CMA ! nColumns ; compare with number of columns
JLT forCol ; if I < nColumns, continue loop

INC I ; increment row index
LDA I ; load index in ACC
CMA ! nRows ; compare with number of rows
JLT forRow ; if I < nRows, continue loop

POPF ; restore FP from stack
RTN ; return to caller

; Variables for subroutine
I: .word 0 ; row index
J: .word 0 ; column index
posA: .word 0 ; current position in first array
posB: .word 0 ; current position in second array

.END