1

I am relatively new to the world of ARM so I apologize if I am missing something obvious. For some context, this code is being executed on a microcontroller based on the ARM Cortex-M4F.

When running this code I expected the branch & links with conditional statements to be run if certain conditions were met duh. But, what actually happens is that after the first compare, regardless of the state of the PSR flags, both KURUMA and SHUKAKU are branched to.

Now I have been able to get the correct conditional branches to work, if I remove the link part of the branch & link command.

How do I get the branch & link to work with a conditional statement, that is actually dependent on the PSR flags?

Thank you

    .text
    .align 2
    .global main
;
; ------------------------------

main:

    MOV R1, #5
    MOV R0, #1

    BL MATATBI
    NOP
    B main

MATATBI:
    PUSH{LR}
    CMPS R1, R0;
    BLHI KURUMA; branch & link if R1 is higher, unsigned,[C=1, Z=0] than r0 to kuruma
    NOP
    CMPS R1, R0;
    BLLO SHUKAKU; branch & link if R1 is lower, unsigned,[C=0] than r0 to shukaku
    NOP
    POP{LR}
    BX LR

KURUMA:
    ADD R1, #7
    BX LR

SHUKAKU:
    SUB R1, #4
    BX LR
hang_forever:
        nop
      B   hang_forever
;
; ------------------------------

    .end

; ------------------------------------------------------------```
  • Did you single step through your code to watch the flags? – the busybee Oct 07 '22 at 09:40
  • what assembler are you using? gnu assembler?...nope, that complains, ARM? – old_timer Oct 07 '22 at 10:05
  • duh, with those comments that is definitely not gnu as for arm. – old_timer Oct 07 '22 at 10:06
  • bllo and blhi are not valid thumb (cortex-m) instructions right? need an IT block? please show the disassembly of your code as shown. – old_timer Oct 07 '22 at 10:09
  • 3
    so.s:17: Error: thumb conditional instruction should be in IT block -- `blhi KURUMA' so.s:20: Error: thumb conditional instruction should be in IT block -- `bllo SHUKAKU' – old_timer Oct 07 '22 at 10:11
  • @the busybee I did single step through the code and watched the flags, by the time the BLHI was reached [Z = 0] and [C = 1], which should meet the conditions for the BLHI branch but not the BLLO branch. This still resulted in branching to both KURUMA and SHUKAKU. – bushwacker1000 Oct 07 '22 at 18:12
  • @old_timer I am using Code Composer Studio from Texas Instruments as my IDE but I do not know what assembler is being used specifically. – bushwacker1000 Oct 07 '22 at 18:19
  • @old_timer The code below only seems to work as expected when R1 <= R0, but when the inverse is true the program will branch to KURUMA, link back to MATATABI, then branch to SHUKAKU. I have been able to get the code to work if I throw in an additional compare and IT block between KURUMA and SHUKAKU. How do I write this code so that only one IT and CMPS is used? `PUSH{LR} CMPS R1, R0; ITE HI ;IT HI; Uncomment to see behavior described above BLHI KURUMA; ;CMPS R1, R0; Uncomment to see behavior described above ;IT LS BLLS SHUKAKU; NOP POP{LR} BX LR` – bushwacker1000 Oct 07 '22 at 18:42
  • without seeing the machine code we cant help – old_timer Oct 07 '22 at 22:31
  • I think you answered one of your own questions. You can use branch in the it blocks from one compare. those branches can go to labels that then do a branch link . – old_timer Oct 10 '22 at 04:10

0 Answers0