user10039600
user10039600

Reputation:

Question about the logic of flag setting in assembly language

Given codes in C for example

if a>b
  c=a+b+10
else
  c=b-a

My code in assembly language without using branch:

ADR R0,a
ADR R1,b
ADR R2,c
LDR R0,[R0]
LDR R1,[R1]

CMP R0,R1
ADDGT R0,R0,R1
ADDGT R0,#10
STRGT R0,[R2]

SUBLE R1,R1,R0
STRLE R1,[R2]

assume, R0>R1, so flags were set at CMP lines

ADDGT and STRGT will run due to the flags. What if ADDGT R0,R0,R1 has a value that will change the flag(Maybe cause V=1) . we didnt do 'ADDGTS' here so next ADDGT can run but wouldnt if affect the actual value of R0??

Thanks

Upvotes: 0

Views: 562

Answers (1)

marko
marko

Reputation: 9169

From the ARM®v7-M Architecture Reference Manual (these instructions were available all the way back to ARM 1).

§A4.4.1 In addition to placing a result in the destination register, these instructions can optionally [using the 'S' postfix] set the condition code flags according to the result of the operation. If an instruction does not set a flag, the existing value of that flag, from a previous instruction, is preserved.

Here you have chosen not to set the condition code register, so the two conditional paths are entirely separate from one another, and the operations in each conditional path won't affect the value of the condition register.

The condition code registers are orthogonal to the operation of the ALU - it might only matter if you wanted to do a 64-bit add or subtract, in which case you might care about the oVerflow or Carry flags.

Your assembler code seems to agree with the pseudocode.

Upvotes: 1

Related Questions