loopsassemblymasmmasm32

Loop to display a message with simple y/n question to repeat the loop or end the program


My target is to display a message 10 times, then ask the user to continue or not. If yes, return to execute the above loop again. If no, exit the program.

The first part of my code is working fine; it's displaying the message "Goodbye" 10 times. However the second part, that asks the user to continue or not, is the problem. When I enter "y" it repeats the message "Do you want to continue (Y/N)?" endlessly.

Here is my code:

INCLUDE Irvine32.inc
.data
    Msg BYTE "Goodbye",0
    again byte "Do you want to continue (Y/N)? ",0

.code
main proc
    mov edx,offset Msg
    mov ecx,10

    
    l1: 
    call writestring
    call crlf
    loop l1

    mov edx, offset again
    call writeString    
    call readChar
    call crlf

    
    cmp al, 'y'
    
    je  l1

    call ReadInt
    
    
    invoke ExitProcess,0
main endp
end main

Solution

  • When the response y is positively compared by cmp al, 'y', you jump to l1 but you forgot to initialize loop counter ecx to 10 again. The previous loop has left ecx at zero, so the loop l1 in fact repeats 0xFFFFFFFF times writing whatever edx points to (offset again).

    Instead of jumping to l1 jump to main. And omit the final call ReadInt. Why to read an integer from user when you do nothing with that number?