Summing an array in assembly x86. On the inputted indexes -
im having trouble adding array on inputted indexes. example, user inputs 4 starting , 6 ending array have loop through array[4] array[6] , add numbers inclusively. i'm not sure if can use array in .data in arraysum procedure. have push procedure somehow?
i using kip irvine's external library this.
my code here:
title assignment 7 include irvine32.inc .data str1 byte "the array sum is: ",0 start byte "enter starting index: ",0 endinx byte "enter ending index: ",0 array dword 4, 6, 2, 5, 6, 7, 8, 4 sum dword ? j dword ? k dword ? .code main proc mov esi, offset array mov ecx, lengthof array mov edx, offset start call writestring call readint mov j, eax mov esi, j mov edx, offset endinx call writestring call readint mov k, eax mov ecx, k call arraysum mov sum,eax call writeint main endp ;--------------------------------------------------- arraysum proc ;sums array falling within j..k inclusive ;--------------------------------------------------- push esi push ecx mov eax, 0 l1: add eax, array[esi] add esi, type dword loop l1 pop ecx pop esi ret arraysum endp end main
you should have no problem accessing array
arraysum, seems code loop wrong. loop label
instruction decreases ecx
register , jumps label if ecx
not zero. array
contains dwords means when access elements should multiply index 4. overall code loop should this:
push ebx mov ebx, offset array l1: add eax, dword ptr [ebx + esi * 4] inc esi cmp esi, ecx jle l1 pop ebx