Hey guys so I am having trouble with this problem for class. So the problem is to add the sum of an array with in a range. The issue I am having is that I can't get the second conditional statement to work, jumping to L4. I am sure there are other issues with the coding so I am completely open to know those issues as well. Thanks in advance guys.
; Program template
Include Irvine32.inc
.data
list DWORD 10, 20, 30, 40
ptrA SDWORD list
varj DWORD 25
vark DWORD 100
.code
main proc
mov esi, ptrA
mov ecx, LENGTHOF list
call ArraySum
call WriteDec
invoke ExitProcess,0
main endp
ArraySum Proc
push esi
push ecx
mov eax, 0
mov ebx, varj
mov edx, vark
top:
cmp [esi], ebx   ; if esi > ebx
jg L2             ; jump to L2
jl L4             ; else jump to L4
L2:
cmp [esi], edx    ;if esi < edx
jl L3            ; jump to L3
jg L4            ; else jump to L4
L3:
add eax, [esi]            ;add the value in array into eax
add esi, TYPE DWORD        ; move to next array index
loop top                ; loop to top
 L4:
 add esi, TYPE DWORD        ; move ot next array index
 loop top                ; loop to top
pop ecx
pop esi
ret
ArraySum endp
end main
				
                        
There is an error in the way you're handling your
loopinstruction, and you have bits of redundant code.Your current looping section is as follows:
If the first
loopinstruction decrementsECXto 0, it will complete and not take the jump but fall through to theL4section. Theloopinstruction in theL4section will then decrementECXto0xFFFFFFFFand you're going to get a lot more looping back to thetoplabel as theloopinstructions proceed to decrement your new, very largeECXvalue to 0.The redundancy comes from constructs such as this:
Here, if the result is greater than zero, you land on
L2. If it's less than zero, you land onL4. If it's equal to zero, you fall through toL2. The net result is that you are really wanting to go toL4if the result is less than zero, and fall through toL2otherwise. So this is equivalent to, simply:Here's the same function with the
loopissue removed and the redundancy tidied up a bit. (NOTE this still assumes thatecx, the array length, andesi, the array address, have been loaded by the caller.)