简体   繁体   中英

Code giving me an infinite loop in assembly?

I have some assembly code that's trying to add up all the even numbers in an array. But, I keep getting an infinite loop when I run it and I can't seem to figure out why. Any help would be appreciated, thank you.

.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD

.data
array BYTE 10,11,13,18,21,23,24,17,45
index BYTE 0
sum BYTE 0
arraySize BYTE ?

.code
main PROC
    mov ecx, LENGTHOF array
    mov arraySize, LENGTHOF array
    mov eax, 0
    mov esi, 0
    mov al, 0

L1:        ;for loop
    WHYLE:   ;while loop
    cmp esi, ecx
    inc esi
    test array[esi], 1
    jz EVENNUM
    EVENNUM: 
    add al, array[esi]
    jl WHYLE
LOOP L1

mov sum, al

invoke ExitProcess, 0
main ENDP
END main

Assuming your array always has at least one element, the following can work:

.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD

.data
array BYTE 10,11,13,18,21,23,24,17,45
sum BYTE 0

.code
main PROC
    mov ecx, LENGTHOF array
    mov esi, -1
    mov al, 0

L1:        ;for loop
    inc esi
    test array[esi], 1
    jnz ODDNUM
    add al, array[esi]
ODDNUM:
    loop L1

    mov sum, al

    invoke ExitProcess, 0
main ENDP
END main

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM