Why doesn't code branch at BEQ after MOVS

0

Id really appreciate if somebody could help me.

Why doesn't code branch on the first reading of BEQ, if comparing r2 with r2. Apparently the final value in r0 is 0X0000001B. Thank you so much, im very confused and frustrated running this over and over in Keil.

    MOV r0,#1
    MOV r1, #0X3
    MOV r2,#0X3
    MOVS r2,r2
 while  
    BEQ stop    
    MUL r0,r1,r0
    SUBS r2,r2,#1
    B while

stop
assembly
arm
instruction-set
microprocessors
asked on Stack Overflow Nov 29, 2018 by MangoKitty

1 Answer

0

Your code doesn't branch because BEQ test if Z flag is equal to 1.

  • MOVS will set Z to 1 if you MOV r2, #0, or register which has a value of 0.
  • If you want to set the Z flag to 1, you can use CMP to check the flags N and Z. Such will do R2-R2 = 0, Z=1

      MOV r0,#1
      MOV r1, #0X3
      MOV r2,#0X3
      CMP r2,r2
     while  
      BEQ stop    
      MUL r0,r1,r0
      SUBS r2,r2,#1
      B while
     stop  
    
  • Another, less efficient method would be to use another register with SUBS, setting all the flags (NZCV).

      MOV r0,#1
      MOV r1, #0X3
      MOV r2,#0X3
      SUBS r3, r2,r2
     while  
      BEQ stop    
      MUL r0,r1,r0
      SUBS r2,r2,#1
      B while
     stop  
    

Good luck!

answered on Stack Overflow Dec 10, 2018 by chocovore17

User contributions licensed under CC BY-SA 3.0