Michael Rey
Michael Rey

Reputation: 5

How can i exit the for statement in assembly

The purpose of this code is to flash the bits turned on three times, exit the loop and turn them off. Currently the code seems to be in an infinite loop and does not exit the loop after the count is 0

                   mov.b   #0x00,&P2REN ;

                   mov.b   #0xFF,&P2OUT ;
                   ;mov.b   #3, r5
                   .bss    i,3 





                                     mov.w   #3,&i        ; 



                   dec.w   &i           ; i--,
                   jnz     for          ; back to for loop
  for_Done:

                    delayloop:                 dec.w   r15          ;
                   jnz     delayloop    ;jump if not zero to delayloop
                   jmp     for         ;jump to the for
                   ;jmp    for_Done

Upvotes: 0

Views: 519

Answers (1)

Peter Paul Kiefer
Peter Paul Kiefer

Reputation: 2124

Like @PeterCordes mentioned, the last command is an unconditional jump to the loop.

That loop decrements the variable i which is 0 after the jump from the end to the loop. It will not be initialized with 3 again. After decrement i in the loop, it will be negative and will stay so for more than 32000 Iterations (w=16Bit?). Than it goes on to the last jump and all will start from new.

Do you really need the last jump? You can be sure that i == 0 if you reach this code.

Upvotes: 1

Related Questions