Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE...

26
Chapters 6 Flow Control In Assembly Embedded Systems with ARM Cortext-M Updated: Monday, February 19, 2018

Transcript of Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE...

Page 1: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

Chapters 6Flow Control In Assembly

Embedded Systems with ARM Cortext-M Updated: Monday, February 19, 2018

Page 2: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

Overview: Flow Control

2

• Basics of Flowcharting• If-then-else• While loop• For loop•

Page 3: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

Flowcharting

• Flowchart• A graphical representation of processes

(tasks) to be performed and the sequence to be followed in solving computational problem

Page 4: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

Example

• Write instructions to load two bytes (37H and 92H) in data registers REG0 and REG1. Add the bytes and store the sum in REG2. • Steps• Load the two bytes in data registers REG0 and REG1.• Add the bytes.• Save the sum in data register REG2.

Page 5: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

Example

REG0 EQU 0x37REG1 EQU 0x92

MOV R0,#REG0MOV R1,#REG1ADD R2, R1, R0

A_HERE B A_HERE

Processà

Startà

Page 6: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

Steps in Writing and Executing Assembly Language Program

• Analyze the problem.• Draw a flowchart.• Convert the flowchart in mnemonics.• Look up Hex code and assign memory addresses.• Enter the Hex code into memory of a lab training board.• Execute the program.• Debug the program if necessary.

Page 7: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

Illustrative Program: Addition With Carry Check

• Write instructions to load two bytes, Byte1 (F2H) and Byte2 (32H), in data registers REG0 and REG1 respectively and add the bytes.• If the sum generates a carry, clear

the data register REG2; otherwise, save the sum in REG2.

Page 8: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

Illustrative Program: Addition With Carry Check

• Write instructions to load two bytes, Byte1 (F2H) and Byte2 (32H), in data registers REG0 and REG1 respectively and add the bytes.• If the sum generates a carry, clear

the data register REG2; otherwise, save the sum in REG2.

Page 9: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

ConditionTesting

9

• Most assembly instructions can be executed based on flags (N, Z, C, V)

• The condition of these flags is set in Application Program Status Register (APSR)

• These conditions depend on SIGNED or UNSIGNED nature of the inputs

Not Equal (Unsigned & Signed)Unsigned Higher or SameUnsigned LOwerMInus (Negative)

Equal (Unsigned & Signed)

oVerflow SetoVerflow ClearUnsigned HIgherUnsigned Lower or Same

PLus (Positive or Zero)

Signed Less ThanSigned Greater ThanSigned Less than or EqualALways

Signed Greater or Equal

EQNECS/HSCC/LO

PLVS

HILSGELTGTLEAL

MI

VC

Suffix Description

Z=0C=1C=0

Z=1Flags tested

N=1N=0V=1V=0C=1 & Z=0C=0 or Z=1N=VN!=VZ=0 & N=VZ=1 or N!=V

N Z C V ReservedApplication PSR(APSR)

Q

Page 10: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

Signed vs. Unsigned & Branch Conditions

Compare Signed Unsigned== EQ EQ≠ NE NE> GT HI≥ GE HS< LT LO≤ LE LS

10

Compare Signed Unsigned== BEQ BEQ!= BNE BNE> BGT BHI>= BGE BHS< BLT BLO<= BLE BLS

Conditional codes applied to branch instructions

Note: Branches can be conditional or unconditional (B)

Page 11: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

Comparison Instructions

ØThe only effect of the comparisons is to update the condition flags.• No need to set S bit.• No need to specify Rd.

ØOperations are:• CMP operand1 - operand2, but result not written• CMN operand1 + operand2, but result not written• TST operand1 & operand2, but result not written• TEQ operand1 ^ operand2, but result not written

ØExamples:• CMP r0, r1• TST r2, #5

Instruction Operands Brief description FlagsCMP Rn, Op2 Compare N,Z,C,VCMN Rn, Op2 Compare Negative N,Z,C,VTEQ Rn, Op2 Test Equivalence N,Z,CTST Rn, Op2 Test N,Z,C

11

Page 12: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

Example: Which is Greater: 0xFFFFFFFF or 0x00000001?

signed int x, y ; x = -1;y = 1;if (x > y)

...

unsigned int x, y ;x = 4294967295;y = 1;if (x > y)

...

12

BLE: Branch if less than or equal, signed ≤

BLS: Branch if lower or same, unsigned ≤

It’s software’s responsibility to tell computer how to interpret data:• If written in C, declare the signed vs unsigned variable • If written in Assembly, use signed vs unsigned branch instructions

MOVS r6, #0xFFFFFFFFMOVS r5, #0x00000001CMP r5, r6BLE Then_Clause...

MOVS r6, #0xFFFFFFFFMOVS r5, #0x00000001CMP r5, r6BLS Then_Clause...

R6=-1

R6= 4294967295

Page 13: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

If-then Statement

13

C Programif (a < 0 ) {a = 0 – a;

}x = x + 1;

; r1 = a, r2 = xCMP r1, #0 ; Compare a with 0BGE endif ; Go to endif if a ≥ 0

then RSB r1, r1, #0 ; a = - aendif ADD r2, r2, #1 ; x = x + 1

Implementation 1:

Page 14: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

If-then Statement

14

C Programif (a < 0 ) {a = 0 – a;

}x = x + 1;

; r1 = a, r2 = xCMP r1, #0 ; Compare a with 0 RSBLT r1, r1, #0 ; a = 0 - a if a < 0ADD r2, r2, #1 ; x = x + 1

Implementation 2:

Example of conditional ExecutionRSB + LT

Page 15: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

Compound Boolean Expression

15

x > 20 && x < 25x == 20 || x == 25

!(x == 20 || x == 25)

C Program Assembly Program// x is a signed integerif(x <= 20 || x >= 25){

a = 1}

; r0 = xCMP r0, #20 ; compare x and 20BLE then ; go to then if x ≤ 20CMP r0, #25 ; compare x and 25BLT endif ; go to endif if x < 25

then MOV r1, #1 ; a = 1endif

Page 16: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

Compound Boolean Expression Alternative

16

x > 20 && x < 25x == 20 || x == 25

!(x == 20 || x == 25)

C Program Assembly Program// x is a signed integerif(x <= 20 || x >= 25){

a = 1}

; r0 = xCMP r0, #20 ; compare x and 20BLE then ; go to then if x ≤ 20CMP r0, #25 ; compare x and 25BLT endif ; go to endif if x < 25

then MOV r1, #1 ; a = 1endif

Example of conditional ExecutionMOV + GE

Page 17: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

If-then-else

17

C Programif (a == 1)

b = 3;else

b = 4;

; r1 = a, r2 = bCMP r1, #1 ; compare a and 1BNE else ; go to else if a ≠ 1

then MOV r2, #3 ; b = 3 B endif ; go to endif

else MOV r2, #4 ; b = 4 endif

Page 18: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

For Loop

18

C Programint i;int sum = 0;for(i = 0; i < 10; i++){sum += i;

}

MOV r0, #0 ; iMOV r1, #0 ; sum

loop CMP r0, #10BGE endloopADD r1, r1, r0ADD r0, r0, #1B loop

endloop

One Implementation is

Question: How can you implement

this code using Conditional execution

(e.g., ADD)?

Page 19: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

Conditional Execution Examples for ADD

19

Add instruction Condition Flag testedADDEQ r3, r2, r1 Add if EQual Add if Z = 1ADDNE r3, r2, r1 Add if Not Equal Add if Z = 0ADDHS r3, r2, r1 Add if Unsigned Higher or Same Add if C = 1ADDLO r3, r2, r1 Add if Unsigned LOwer Add if C = 0ADDMI r3, r2, r1 Add if Minus (Negative) Add if N = 1ADDPL r3, r2, r1 Add if PLus (Positive or Zero) Add if N = 0ADDVS r3, r2, r1 Add if oVerflow Set Add if V = 1ADDVC r3, r2, r1 Add if oVerflow Clear Add if V = 0ADDHI r3, r2, r1 Add if Unsigned HIgher Add if C = 1 & Z = 0ADDLS r3, r2, r1 Add if Unsigned Lower or Same Add if C = 0 or Z = 1ADDGE r3, r2, r1 Add if Signed Greater or Equal Add if N = VADDLT r3, r2, r1 Add if Signed Less Than Add if N != VADDGT r3, r2, r1 Add if Signed Greater Than Add if Z = 0 & N = VADDLE r3, r2, r1 Add if Signed Less than or Equal Add if Z = 1 or N = !V

Page 20: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

Examples of Conditional Execution

20

if (a <= 0) y = -1;

elsey = 1;

CMP r0, #0MOVLE r1, #-1MOVGT r1, #1

LE: Signed Less than or EqualGT: Signed Greater Than

a ⟶ r0y ⟶ r1

if (a==1 || a==7 || a==11)y = 1;

elsey = -1;

CMP r0, #1CMPNE r0, #7CMPNE r0, #11MOVEQ r1, #1MOVNE r1, #-1

NE: Not EqualEQ: Equal

a ⟶ r0y ⟶ r1

Page 21: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

While Loop

• Run a loop until a condition occurs

Page 22: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

Write a program to clear R0 and add 9 to R0 1000 times. Place the sum in R4. Use BNE.

Page 23: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

Write a program to load r0 with value 0x55 and complement (negate) it 16 Billion times!• We need to use a nested loop – note 2^23 is 4 billion)

Page 24: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

Write a program to add 0x99999999 together TEN times.• We read the result in

registers R1 and R0 • Place the value in R2• Place the counter in

R3

Page 25: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

Assuming we have 5 values: 69, 87, 96, 45, and 75. Find the highest number and place it in R1.

Page 26: Chapters 6 Flow Control In Assembly - Sonoma State University · 2018. 2. 19. · > BGT BHI >= BGE BHS < BLT BLO

26

Not Equal (Unsigned & Signed)

Unsigned Higher or Same

Unsigned LOwer

MInus (Negative)

Equal (Unsigned & Signed)

oVerflow Set

oVerflow Clear

Unsigned HIgher

Unsigned Lower or Same

PLus (Positive or Zero)

Signed Less Than

Signed Greater Than

Signed Less than or Equal

ALways

Signed Greater or Equal

EQNECS/HSCC/LO

PLVS

HILSGELTGTLEAL

MI

VC

Suffix Description

Z=0

C=1

C=0

Z=1

Flags tested

N=1

N=0

V=1

V=0

C=1 & Z=0

C=0 or Z=1

N=V

N!=V

Z=0 & N=V

Z=1 or N!=V

Compare Signed Unsigned== EQ EQ

≠ NE NE

> GT HI

≥ GE HS

< LT LO

≤ LE LS

Instruction Operands Brief description FlagsCMP Rn, Op2 Compare N,Z,C,V

CMN Rn, Op2 Compare Negative N,Z,C,V

TEQ Rn, Op2 Test Equivalence N,Z,C

TST Rn, Op2 Test N,Z,C

• CMP operand1 - operand2, but result not written

• CMN operand1 + operand2, but result not written

• TST operand1 & operand2, but result not written

• TEQ operand1 ^ operand2, but result not written