; *********************************************************************** ; * * ; * Matrix Operation (First 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 essentially the hand-translated version of matrix-c.c. ; ----------------------------------------------------------------------- ; 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: mov r5, #0 ; Assign i to R5; initialise for start of loop ldr r2, =a ; R2 = address of "a", ie, R2 points to a[0] ldr r3, =b ; R3 = address of array "b" ldr r0, =c ; R0 = address of array "c" mainlp: ldr r6, [r2, r5] ; Load R6 with contents of a[i] (a+i = R2+R5) ldr r1, [r3, r5] ; Load R1 with contents of b[i] mov r6, r6, asr #1 ; R6 = a[i] >> 1 (asr = Arithmetic Shift Right) mov r1, r1, asl #1 ; R1 = b[i] << 1 (asl = Arithmetic Shift Left) add r1, r6, r1 ; R1 = a[i] >> 1 (in R6) + b[i] << 1 (in R1) str r1, [r0, r5] ; Store the result into c[i] (c+i = R0+R5) cmp r5, #(3 * 4) ; Is i > 3 * 4-bytes (int takes four bytes) add r5, r5, #4 ; Increment R5 (i++); Doing this does not ; affect cmp result (in the CPSR register) bls mainlp ; If i <= 3 * 4, repeat the loop ; NB: bls is "branch if less", but i has ; already been incremented, so real meaning ; is as shown in the comment. 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" b: .word 5, 6, -7, 8 ; Array "b" c: .word 0, 0, 0, 0 ; Array "c" ; ----------------------------------------------------------------------- .end