Study Web

Week 8

Conditional Branching

Conditional · Branching

Conditional Branching

Conditionals in programming languages

  • Almost all programming languages have the capability to choose to do something only if a condition is met.
  • Most commonly, this comes in the form of an “if” statement: if (x < 2): else:

But how does this translate to assembly code?

if (x < 2): else:

But how does this translate to assembly code?

if (x < 2): else: First thing to notice: we have two

mutually exclusive computations to perform!

But how does this translate to assembly code?

if (x < 2):

x = 0 Second thing to notice:

we have a condition that determines which instruction else: to branch to

But how does this translate to assembly code?

if (x < 2): Second thing to notice:

x = 0 we have a condition that determines which instruction

to branch to else:

This requires the ability to branch only when a specific condition is true

IF Tests: CMP

  • In ARM assembly, the CMP (Compare) instruction allows values to be compared:
  • CMP R1, R2 subtracts the 2nd value (R2) from the 1st (R1)
  • The result of this subtraction is then used to update the Application Program Status Register (APSR)
  • Performed by the ALU
  • Specifically, 4 flag bits are updated within the APSR:
  • N ALU result was Negative
  • Z ALU result was Zero
  • C ALU set the Carry bit
  • V ALU result caused oVerflow

APSR and the Program Status Register

http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0552a/CHDBIBGJ.html

http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0552a/CHDBIBGJ.html

In ARMlite …

You can visually inspect the outcome of a CMP here

details

r2 - #1234;

set APSR with

  • cmp r2,#1234 compare r2 ALU flags and #1234

store the ALU flags in the APSR compare

Conditional Branching using the APSR

  • Branch (B) reads the APSR and jumps according to the flags and the relevant suffix you give it.
  • In ARMlite, there are five variants of Branch:
  • B – unconditional branch
  • BEQ ‘Branch if EQual’
  • BGT ‘Branch if Greater Than’
  • BLT ‘Branch if Less Than’
  • BNE ‘Branch if Not Equal’
  • In the ARM instruction set more generally, there are many others

Determining the comparison from the flags

Suffix Flags Meaning

EQ Z set Equal NE Z clear Not equal CS or HS C set Higher or same

(unsigned >= )

CC or LO C clear Lower (unsigned <) In ARM MI N set Negative assembly, the PL N clear Positive or zero condition code VS V set Overflow suffix can be VC V clear No overflow added to many HI C set and Z clear Higher (unsigned >) operations. e.g. LS C clear or Z set Lower or same

(unsigned <=)
movne r1,#12 GE N and V the same Signed >=

LT N and V differ Signed < GT Z clear, N and V the Signed > same

LE Z set, N and V differ Signed <=

SO.. how does this translate to assembly code?

if (x < 2):

SO.. how does this translate to assembly code?

if (x <= 2): LDR R0,X // assume X is holding a value
x = 0 CMP R0,#2 // compare contents of R0 with #2
BGT skip // if R0 > 2 then jump to skip
MOV R0,#0 // if R0 <= 2 then assign R0 #0

skip: // continue program from here …

What about this?

if (x < 2): else:

What about this?

if (x <= 2): LDR R0,X // assume X is holding a value
x = 0 CMP R0,#2 // compare contents of R0 with #2
BGT else // if R0 > 2 then jump to else
else: MOV R0,#0 // case for R0 <= 2
x = 3 B cont // Branch to label cont

else:

MOV R0,#3 // case for R0 > 2

cont: ….

if (x <= 2): LDR R0,X // assume X is holding a value
x = 0 CMP R0,#2 // compare contents of R0 with #2
BGT else // if R0 > 2 then jump to label else
else: MOV R0,#0 // case for R0 <= 2
x = 3 B cont // Branch to label cont

else:

MOV R0,#3 // case for R0 > 2

cont: …. Why do you think we need this branch?

Pause the video and try this one

if (x <= 2):
else if (x <= 4):

else

Pause the video and try this one

if (x <= 2): LDR R0,X // assume X is holding a value

CMP R0,#2 // compare contents of R0 with #2
x = 0 BGT else1 // if R0 > 2 then jump to label else
else if (x <= 4): MOV R0,#0 // case for R0 <= 2

B cont // Branch to label cont

x = 2 else1: // if R0 > 2
CMP R0,#4 // compare contents of R0 with #4
else BGT else2 // if R0 > 4 then jump to label else2
MOV R0, #2 // otherwise, handled case for R0 > 2 <= 4
x = 4 B cont // Branch to label cont
else2: // if R0 > 4
MOV R0,#4 // case for R0 > 4

cont: // exit point of conditionals. Continue with program from here ….

Pause the video and try this one

if (x <= 2): LDR R0,X // assume X is holding a value

CMP R0,#2 // compare contents of R0 with #2
x = 0 BGT else1 // if R0 > 2 then jump to label else
else if (x <= 4): MOV R0,#0 // case for R0 <= 2

B cont // Branch to label cont

x = 2 else1: // if R0 > 2
CMP R0,#4 // compare contents of R0 with #4
else BGT else2 // if R0 > 4 then jump to label else2
MOV R0, #2 // otherwise, handled case for R0 > 2 <= 4
x = 4 B cont // Branch to label cont
else2: // if R0 > 4
MOV R0,#4 // case for R0 > 4

cont: // exit point of conditionals. Continue with program from here ….

So yeah
things get complicated quickly!