1.05k likes | 1.31k Views
Program Control. ECE511: Digital System & Microprocessor. What we are going to learn in this session:. Program Control: What are they. Available instructions. How they effect program flow. Introduction. Program Control Group. Instructions that perform Conditional execution.
E N D
Program Control ECE511: Digital System & Microprocessor
What we are going to learn in this session: • Program Control: • What are they. • Available instructions. • How they effect program flow.
Program Control Group • Instructions that perform • Conditional execution. • Unconditional execution. • Subroutine handling. • Achieves this through branching. • Manipulate PC.
Branch Always Conditional Branch Decrement & Branch Conditional Set Program Control Branch to Subroutine Unconditional Jump Jump to Subroutine Return from Subroutine Return from Subroutine & Restore CCR The Program Control Group
BRA (Branch Always) • Performs unconditional branching. • Used to create infinite loops. • Repeats the same instructions over and over. • Doesn’t stop until M68k resets/turned off.
BRA (Branch Always) • Uses relative addressing mode: • Has limitations. • 8-bit signed displacement: -128 to 127. • 16-bit signed displacement: -32,768 to 32,767.
BRA Format Example: BRA <ea> BRA $001000 BRA LABELNAME BRA THERE
Unconditional Branching Example Instruction #1 Instruction #2 BRA HERE Instruction #3 Instruction #4 HERE Instruction #5 Instruction #6 Never executed.
Infinite Loop Example LABELNAME Instructions #1 Instructions #2 … Instructions #n BRA LABELNAME
BRA Example START ORG $1000 BRAHERE MOVE.B #9,D0 MOVE.B #19,D1 ADD.B D1,D0 BRA BRAHERE END START
BRA Limitations • Can address maximum 32,768 addresses back, and 32,767 addresses forward. • Max forward = Address + 32,767. • Max backward = Address – 32,768. • If need to be farther, can use Jump (JMP).
BRA Limitations • Find the allowed range when BRA address is at $030000. $030000 = 196,608 Max forward = 196,608 + 32,767 = 229,375 = $37FFF. Max backward = 196,608 – 32,768 = 163,840 = $28000. $28000 < Range < $37FFF
JMP (Unconditional Jump) • Similar to BRA, but without limitations: • Jumps to any location in program. • Not limited to 16-bit displacements anymore.
Bcc (Conditional Branch) • Conditional branching: • Branch if condition is TRUE. • Execute/skip instructions results. • Addressing limitations similar to BRA: • 8-bit, 16-bit displacement. • -32,768 to 32,767.
Bcc (Conditional Branch) • Conditions tested using CMP. • (Antonakos, pg. 83) • Results stored in CCR. • Used to create: • Finite loops. • Do…while loops. • If…else blocks.
BGT D0.B > D1.B BGE D0.B ≥ D1.B Signed BEQ D0.B = D1.B BNE D0.B ≠ D1.B BLE D0.B ≤ D1.B BLT D0.B < D1.B Bcc BHI D0.B > D1.B BCC D0.B ≥ D1.B BEQ D0.B = D1.B Unsigned BNE D0.B ≠ D1.B BLS D0.B ≤ D1.B BCS D0.B < D1.B Conditions (cc) CMP.B D1,D0 *(Antonakos, pg. 84)
Why do we need signed/unsigned conditions? • Signed/unsigned numbers carry different values. • Example: $FF12: • Need to choose cc properly. 65,298 (unsigned) -238 (signed)
If treated as unsigned number, $FE = 254 $23 = 35 D0.B > D1.B If treated as signed number, $FE = -2 $23 = 35 D0.B < D1.B X N Z V C - 1 0 0 0 Bcc Example D0 = $000000FE D1 = $00000023 CMP.B D1,D0
Bcc Flowchart START Evaluate conditions using CMP Condition = TRUE? TRUE FALSE Go to LABEL Execute next instruction FINISH
Bcc Format LABELNAME Instructions #1 Instructions #2 … Instructions #n Test Condition Bcc LABELNAME Instructions after Bcc… If condition = TRUE If conditions = FALSE
Bcc Format If conditions = TRUE, Instructions 1 n will not be executed Test Condition Bcc LABEL Instructions #1 Instructions #2 … Instructions #n LABEL Instructions after LABEL… If conditions = FALSE, All instructions executed line-by-line.
If…Else Using Bcc • Check the byte stored in D3. If D3 > 10, clear the byte. Else, add 3 to the value stored in D3.B.
Flowchart START Move value to D3 True False D3 > 10? Clear D3 D3 = D3 + 3 FINISH
Assembly Code START ORG $1000 MOVE.B #11,D3 CMP.B #10,D3 BHI MORE BLE LESS MORE CLR.B D3 BRA FINISH LESS ADD.B #3,D3 BRA FINISH FINISH END START D3.B > 10 D3.B < 10
Bcc Example – If… • Check contents of D0: • If D0 > 10, add 54 to it. • If D0 ≤ 10, do nothing.
Flowchart START Move value to D0 True False D0 > 10? D0 = D0 + 54 FINISH
When D0 ≤ 10 START ORG $1000 MOVE.B #5,D0 CMP #10,D0 BLE DONTHNG ADD.B #54,D0 DONTHNG NOP END START If D0 ≤ 10
When D0 > 10 START ORG $1000 MOVE.B #15,D0 CMP #10,D0 BLE DONTHNG ADD.B #54,D0 DONTHNG NOP END START
Condition = FALSE, Executes next line
Program Flow MOVE.B #1,D0 CMP #10,D0 BLE DONTHNG ADD.B #54,D0 DONTHNG NOP END START If D0 ≤ 10 If D0 > 10