; ; CPE 221 Assembly Example ; ; This program adds a scalar value to all the elements in one ; array and stores that in another array. ; ; using namespace std; ; ; int main() ; { ; const int size = 10; ; int x[size] = {5, 3, -1, 2, 4, 37, -100, 13, -5, 0}; ; int y[size]; ; int a = 5; ; int i; ; for (i = 0; i < size; i++) ; y[i] = x[i] + a; ; } ; ; .org 200 size: .dc 10 a: .dc 5 x: .dc 5, 3, -1, 2, 4, 37, -100, 13, -5, 0 y: .dw 10 orig: .org 1000 lar r30, done lar r29, loop lar r10, x ; pointer to first element of x lar r11, y ; pointer to first element of y ld r1, size ; holds size of arrays ld r4, 0 ; i = 0 ld r2, a ; load a into r2 loop: sub r5, r4, r1 ; Check to see whether index < size. brpl r30, r5 ; If not, done. shl r6, r4, 2 ; Multiply index by 4 to access entry ; in array by byte address. add r3, r6, r10 ; Add index to base pointer of x. add r8, r6, r11 ; Add index to base pointer of y. ld r7, 0(r3) ; Load array[i] into r7. add r7, r7, r2 ; Add a to x[i] st r7, 0(r8) ; Store result in y. addi r4, r4, 1 ; i++ br r29 done: stop