; ; CPE 221 Assembly Example ; ; This program finds the maximum 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 i = 0; ; max = nums[0]; ; for (i = 1; i < size; i++) ; if (nums[i] > max) ; max = nums[i]; ; } .org 200 size: .dc 10 neg_count: .dw 1 pos_count: .dw 1 nums: .dc 100, 3, -1, 2, 4, 37, -100, 13, -5, 0 orig: .org 1000 lar r10, nums ; pointer to first element of array ld r1, size ; holds size of array la r2, 0 ; i = 0 la r3, 0 ; neg_count = 0 la r4, 0 ; pos_count = 0 lar r31, done lar r30, loop lar r29, negative loop: sub r5, r2, r1 ; Check to see whether index < size. brpl r31, r5 ; If not, done. shl r5, r1, 2 ; Multiply index by 4 to access entry ; in array by byte address. add r5, r5, r10 ; Add index to base array pointer. ld r5, 0(r5) ; Load array[index] into r7. brmi r29, r5 addi r4, r4, 1 ; pos_count++ br r30 negative: addi r3, r3, 1 ; neg_count++ br r30 done: st r3, neg_count st r4, pos_count stop