Here is the CSC-1 assembler code for the C version above: 0: LDI 2500 ; Put the address of the array into pointer 1: STD POINTR 2: LDI 0 ; Put 0 into sum and i 3: STD SUM 4: STD I 5: LOOP: LOD MAX ; is i == max? 6: SUB I 7: JZ ENDLOOP; if so, then we are done 8: LOD POINTR ; Get pointer to current array element 9: A2S ; Save in S to be used in indirect load 10: LDS ; Actually fetch the value 11: ADD SUM ; Add current sum to this 12: STD SUM ; store back into sum 13: LOD POINTR ; Add 1 to the pointer 14: ADD ONE ; so that it points to the next element 15: STD POINTR ; for the next round of the loop 16: LOD I ; Add 1 to the counter 17: ADD ONE ; so we can know when we've looked at all 18: STD I ; the elements of the array 19: JMP LOOP ; Back to the top of the loop 20: ENDLOOP: HLT ; When done, halt the computer 21: MAX: NUM 5 ; maximum size of the array 22: I: NUM 0 ; loop counter 23: ONE: NUM 1 ; Constant 1 used to increment I =1000 1000: POINTR: NUM 0 ; a pointer to the current element of the array =1500 1500: SUM: NUM 0 ; will hold sum of all elements of array =2500 2500: ARRAY: NUM 8 ; The array of integer 2501: NUM 2 2502: NUM 1 2503: NUM 4 2504: NUM 6 Let's examine the heart of the while loop to see how indirect addressing is used. |