; ; CPE 221 Assembly Example ; ; This program finds the number with the maximum absolute value in an array ; ; #include ; using namespace std; ; ; int main() ; { ; const int size = 10; ; int max; ; int nums[size] = {100, 3, -1, 2, 4, 37, -100, 13, -5, 0}; ; int temp; ; int i = 0; ; max = nums[0]; ; if (max < 0) ; max = -max; ; for (i = 1; i < size; i++) ; { ; temp = nums[i]; ; if (temp < 0) ; temp = -temp; ; if (temp > max) ; max = temp; ; } ; } .org 200 size: .equ 10 aamax: .dw 1 array: .dc 5, 3, -1, 2, 4, 37, -100, 13, -5, 0 orig: .org 1000 la r30, done la r29, loop la r28, inc la r27, pos2 la r26, pos1 lar r10, array ; pointer to first element of array la r1, size ; holds size of array ld r2, 0(r10) ; max = num[0] brpl r26, r2 ; If r2 > 0 jump to pos1 neg r2, r2 ; r2 = -r2 pos1: la r4, 1 ; r4 = 1, index1 into array 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 r6, r6, r10 ; Add index to base array pointer. ld r7, 0(r6) ; Load array[index] into r7. brpl r27, r7 ; If array[index] > 0 jump to pos2 neg r7, r7 ; r7 = -r7 pos2: sub r8, r7, r2 ; test to see whether r7 is greater than r2 brmi r28, r8 ; If not, go to i++ addi r2, r7, 0 ; r2 gets new max inc: addi r4, r4, 1 ; i++ br r29 ; Go to next iteration of the loop done: st r2, amax ; store computed max into variable stop