; *********************************************************************** ; * * ; * Matrix Operation (Second Version in Assembly Language) * ; * * ; *********************************************************************** ; Authors: Saeid Nooshabadi and John Zaitseff ; Date: 5th May, 2003 ; Version: 1.3 ; This program calculates the following matrix (more properly called ; vector) operation: ; ; C[i] = A[i] / 2 + 2 * B[i] ; ; Note that this program uses arithmetic shift right ("asr") instead of ; divide-by-2 and arithmetic shift left ("asl") instead of multiply-by-2. ; ; This program is the optimised version of matrix-v1.s. It takes ; advantage of the post-indexing variant of the "ldr" instruction, a ; shift/rotate variant of "add". It also takes great advantage of the ; structure of the program, particularly the relationship between the ; addresses of the arrays. This level of optimisation is usually NOT ; recommended by hand, but may be necessary at times---such as when space ; is extremely limited in a ROM or in fast RAM. ; ----------------------------------------------------------------------- ; Assembly-language preamble for the main module .text ; Executable code follows _start: .global _start ; "_start" is required by the linker .global main ; "main" is our main program b main ; Start running the main program ; ----------------------------------------------------------------------- ; Start of the main program main: ldr r2, =a ; R2 = address of a, used as "counter" a+i add r3, r2, #(b-a) ; R3 = address of b (NB: b-a = 16 bytes) mainlp: ldr r6, [r2], #4 ; Load R6 with *(a+i), then make R2 = R2+4 ldr r1, [r2, #(b-a-4)] ; R1 = *(b+i) (NB: b-a-4 = 12 bytes) mov r1, r1, asl #1 ; R1 = *(b+i) << 1 add r1, r1, r6, asr #1 ; R1 = R1 + (R6 >> 1) str r1, [r2, #(c-a-4)] ; Store result into *(c+i) (NB: c-a-4 = 28) cmp r2, r3 ; Has the end of a[] been reached, ie, is ; it now b[0]? Assumes b follows a. bls mainlp ; No, repeat the loop exit: mov pc, lr ; Stop the program (and return to OS) ; ----------------------------------------------------------------------- ; Program variables (stored together with the code) a: .word -1, 2, 3, 4 ; Array a (each array is 16 bytes in size) b: .word 5, 6, -7, 8 ; Array b c: .word 0, 0, 0, 0 ; Array c ; ----------------------------------------------------------------------- .end