Second conditional statement not working in assembly

95 views Asked by At

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
1

There are 1 answers

1
lurker On

There is an error in the way you're handling your loop instruction, and you have bits of redundant code.

Your current looping section is as follows:

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

If the first loop instruction decrements ECX to 0, it will complete and not take the jump but fall through to the L4 section. The loop instruction in the L4 section will then decrement ECX to 0xFFFFFFFF and you're going to get a lot more looping back to the top label as the loop instructions proceed to decrement your new, very large ECX value to 0.

The redundancy comes from constructs such as this:

    jg L2             ; jump to L2
    jl L4             ; else jump to L4
L2:

Here, if the result is greater than zero, you land on L2. If it's less than zero, you land on L4. If it's equal to zero, you fall through to L2. The net result is that you are really wanting to go to L4 if the result is less than zero, and fall through to L2 otherwise. So this is equivalent to, simply:

    jl L4
L2:

Here's the same function with the loop issue removed and the redundancy tidied up a bit. (NOTE this still assumes that ecx, the array length, and esi, the array address, have been loaded by the caller.)

ArraySum Proc
    push  esi
    push  ecx
    mov   eax, 0
    mov   ebx, varj
    mov   edx, vark

top:
    cmp   [esi], ebx         ; if [esi] < ebx
    jl    skip               ;     jump to skip

    cmp   [esi], edx         ; if [esi] > edx
    jg    skip               ;     jump to skip

    add   eax, [esi]         ; add the value in array into eax

skip:
    add   esi, TYPE DWORD    ; move to next array index
    loop  top                ; loop to top

    pop ecx
    pop esi
    ret
ArraySum endp