Program Control ECE 511 Digital System Microprocessor What

  • Slides: 101
Download presentation
Program Control ECE 511: Digital System & Microprocessor

Program Control ECE 511: Digital System & Microprocessor

What we are going to learn in this session: n Program Control: ¨ What

What we are going to learn in this session: n Program Control: ¨ What are they. ¨ Available instructions. ¨ How they effect program flow.

Introduction

Introduction

Program Control Group n Instructions that perform ¨ Conditional execution. ¨ Unconditional execution. ¨

Program Control Group n Instructions that perform ¨ Conditional execution. ¨ Unconditional execution. ¨ Subroutine handling. ¨ Achieves this through branching. n Manipulate PC.

The Program Control Group Branch Always Conditional Branch Decrement & Branch Conditional Set Program

The Program Control Group Branch Always Conditional Branch Decrement & Branch Conditional Set Program Control Branch to Subroutine Unconditional Jump to Subroutine Return from Subroutine & Restore CCR

Unconditional Branch

Unconditional Branch

BRA (Branch Always) Performs unconditional branching. n Used to create infinite loops. n ¨

BRA (Branch Always) Performs unconditional branching. n Used to create infinite loops. n ¨ Repeats the same instructions over and over. ¨ Doesn’t stop until M 68 k resets/turned off.

BRA (Branch Always) n Uses relative addressing mode: ¨ Has limitations. ¨ 16 -bit

BRA (Branch Always) n Uses relative addressing mode: ¨ Has limitations. ¨ 16 -bit signed displacement: -32, 768 to 32, 767.

BRA Format Example: BRA <ea> BRA $001000 BRA LABELNAME BRA THERE

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

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

Infinite Loop Example LABELNAME Instructions #1 Instructions #2 … Instructions #n BRA LABELNAME

BRA Example START ORG $1000 BRAHERE MOVE. B ADD. B BRA #9, D 0

BRA Example START ORG $1000 BRAHERE MOVE. B ADD. B BRA #9, D 0 #19, D 1, D 0 BRAHERE END START

BRA Limitations n Can address maximum 32, 768 addresses back, and 32, 767 addresses

BRA Limitations n 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 n Find the allowed range when BRA address is at $030000 =

BRA Limitations n Find the allowed range when BRA address is at $030000 = 196, 608 Max forward = 196, 608 + 32, 767 = 229, 375 = $37 FFF. Max backward = 196, 608 – 32, 768 = 163, 840 = $28000 < Range < $37 FFF

JMP (Unconditional Jump) n Similar to BRA, but without limitations: ¨ Jumps to any

JMP (Unconditional Jump) n Similar to BRA, but without limitations: ¨ Jumps to any location in program. ¨ Not limited to 16 -bit displacements anymore.

Conditional Branch

Conditional Branch

Bcc

Bcc

Bcc (Conditional Branch) n Conditional branching: ¨ Branch if condition is TRUE. ¨ Execute/skip

Bcc (Conditional Branch) n Conditional branching: ¨ Branch if condition is TRUE. ¨ Execute/skip instructions results. n Addressing limitations similar to BRA: ¨ 16 -bit displacement. ¨ -32, 768 to 32, 767.

Bcc (Conditional Branch) n Conditions tested using CMP. ¨ (Antonakos, pg. 83) ¨ Results

Bcc (Conditional Branch) n Conditions tested using CMP. ¨ (Antonakos, pg. 83) ¨ Results stored in CCR. n Used to create: ¨ Finite loops. ¨ Do…while loops. ¨ If…else blocks.

Conditions (cc) CMP. B D 1, D 0 Signed BGT D 0. B >

Conditions (cc) CMP. B D 1, D 0 Signed BGT D 0. B > D 1. B BGE D 0. B ≥ D 1. B BEQ D 0. B = D 1. B BNE D 0. B ≠ D 1. B BLE D 0. B ≤ D 1. B BLT D 0. B < D 1. B BHI D 0. B > D 1. B BCC D 0. B ≥ D 1. B BEQ D 0. B = D 1. B BNE D 0. B ≠ D 1. B BLS D 0. B ≤ D 1. B BCS D 0. B < D 1. B Bcc Unsigned *(Antonakos, pg. 84)

Why do we need signed/unsigned conditions? Signed/unsigned numbers carry different values. n Example: $FF

Why do we need signed/unsigned conditions? Signed/unsigned numbers carry different values. n Example: $FF 12: n 65, 298 (unsigned) n Need to choose cc properly. -238 (signed)

Bcc Example D 0 = $000000 FE D 1 = $00000023 CMP. B D

Bcc Example D 0 = $000000 FE D 1 = $00000023 CMP. B D 1, D 0 X N Z V C - 0 0 1 0 If treated as unsigned number, If treated as signed number, $FE = 254 $23 = 35 $FE = -2 $23 = 35 D 0. B > D 1. B D 0. B < D 1. B

Bcc Flowchart START Evaluate conditions using CMP TRUE Condition = TRUE? Go to LABEL

Bcc Flowchart START Evaluate conditions using CMP TRUE Condition = TRUE? Go to LABEL FALSE Execute next instruction FINISH

Bcc Format LABELNAME If condition = TRUE Instructions #1 Instructions #2 … Instructions #n

Bcc Format LABELNAME If condition = TRUE Instructions #1 Instructions #2 … Instructions #n Test Condition Bcc LABELNAME If conditions = FALSE Instructions after Bcc…

Bcc Format If conditions = TRUE, Instructions 1 n will not be executed LABEL

Bcc Format If conditions = TRUE, Instructions 1 n will not be executed LABEL Test Condition Bcc LABEL Instructions #1 Instructions #2 … Instructions #n If conditions = FALSE, All instructions executed line-by-line. Instructions after LABEL…

If…Else Using Bcc n Check the byte stored in D 3. If D 3

If…Else Using Bcc n Check the byte stored in D 3. If D 3 > 10, clear the byte. Else, add 3 to the value stored in D 3. B.

Flowchart START Move value to D 3 True D 3 > 10? Clear D

Flowchart START Move value to D 3 True D 3 > 10? Clear D 3 False D 3 = D 3 + 3 FINISH

Assembly Code START ORG $1000 MOVE. B #11, D 3 CMP. B BGT BLE

Assembly Code START ORG $1000 MOVE. B #11, D 3 CMP. B BGT BLE #10, D 3 MORE LESS MORE CLR. B BRA D 3 FINISH LESS ADD. B BRA #3, D 3 FINISH END START D 3. B > 10 D 3. B < 10

Bcc Example – If… n Check contents of D 0: ¨ If D 0

Bcc Example – If… n Check contents of D 0: ¨ If D 0 > 10, add 54 to it. ¨ If D 0 ≤ 10, do nothing.

Flowchart START Move value to D 0 True False D 0 > 10? D

Flowchart START Move value to D 0 True False D 0 > 10? D 0 = D 0 + 54 FINISH

When D 0 ≤ 10 START If D 0 ≤ 10 DONTHNG ORG $1000

When D 0 ≤ 10 START If D 0 ≤ 10 DONTHNG ORG $1000 MOVE. B CMP #5, D 0 #10, D 0 BLE DONTHNG ADD. B #54, D 0 NOP END START

Condition = TRUE

Condition = TRUE

When D 0 > 10 START DONTHNG ORG $1000 MOVE. B CMP #15, D

When D 0 > 10 START DONTHNG ORG $1000 MOVE. B CMP #15, D 0 #10, D 0 BLE DONTHNG ADD. B #54, D 0 NOP END START

Condition = FALSE, Executes next line

Condition = FALSE, Executes next line

This line also executed.

This line also executed.

Program Flow If D 0 ≤ 10 DONTHNG MOVE. B CMP BLE ADD. B

Program Flow If D 0 ≤ 10 DONTHNG MOVE. B CMP BLE ADD. B #1, D 0 #10, D 0 DONTHNG #54, D 0 NOP END START If D 0 > 10

Bcc Example: Do. . While Loop n Add the contents of memory locations $2000,

Bcc Example: Do. . While Loop n Add the contents of memory locations $2000, $2001, $2002 together using the do. . while loop. Store the results inside D 0.

Flowchart START D 1 = 0 D 0 = 0 D 1 = 3?

Flowchart START D 1 = 0 D 0 = 0 D 1 = 3? True False Add mem. to D 0 Go to next mem. location D 1 = D 1 + 1 FINISH

Example Program START LOOP ORG $1000 MOVE. B #$12, $2000 MOVE. B #$34, $2001

Example Program START LOOP ORG $1000 MOVE. B #$12, $2000 MOVE. B #$34, $2001 MOVE. B #$56, $2002 ; PUT $12 INTO ADDRESS $2000 ; PUT $34 INTO ADDRESS $2001 ; PUT $56 INTO ADDRESS $2002 MOVE. B #0, D 1 CLR. L D 0 ; D 1 AS COUNTER ; D 0 CLEARED, SUM STORED HERE LEA $2000, A 0 ; MOVE ADDRESS $2000 TO A 0 ADD. B (A 0)+, D 0 ADD. B CMP. B BNE #1, D 1 #3, D 1 LOOP ; ADD MEMORY CONTENT TO D 0 ; INCREMENT A 0 BY 1 TO POINT TO NEXT ; MEM. LOCATION ; INCREMENT COUNTER BY 1 ; ; IF COUNTER ≠ 3, LOOP BACK END START

Program Flow LOOP If D 1 ≠ 3 ADD. B CMP. B BNE (A

Program Flow LOOP If D 1 ≠ 3 ADD. B CMP. B BNE (A 0)+, D 0 #1, D 1 #3, D 1 LOOP If D 1 = 3 END START

DBcc

DBcc

DBcc (Test, Decrement and Branch) n Similar to Bcc, except: ¨ Now adds a

DBcc (Test, Decrement and Branch) n Similar to Bcc, except: ¨ Now adds a counter. ¨ Counter decremented at each pass. ¨ Loop exits when: Condition is TRUE, or n Counter reaches -1. n

DBcc (Test, Decrement and Branch) n Counter: ¨ Word value in data register (Dn.

DBcc (Test, Decrement and Branch) n Counter: ¨ Word value in data register (Dn. W). ¨ Decremented after each pass. ¨ 0 ≤ Range ≤ 32, 767.

Difference between DBcc and Bcc Branches to LABEL when. . Condition tested is TRUE

Difference between DBcc and Bcc Branches to LABEL when. . Condition tested is TRUE Go to next line when. . Condition tested is FALSE. DBcc Condition tested is FALSE and when counter ≠ -1. Condition tested is TRUE or when counter = -1.

DBcc Flowchart START Evaluate condition using CMP Decrement counter FALSE TRUE Counter = -1

DBcc Flowchart START Evaluate condition using CMP Decrement counter FALSE TRUE Counter = -1 ? Go to LABEL Execute next instruction FINISH

Try It Yourself START ORG $1000 MOVE. W MOVE. B CMP. B BEQ MOVE.

Try It Yourself START ORG $1000 MOVE. W MOVE. B CMP. B BEQ MOVE. B LABEL START ORG #10, D 6 #4, D 0 #1, D 0 LABEL #$12, D 4 #$34, D 5 MOVE. B END BEQ #1, D 2 START $1000 MOVE. W #10, D 6 MOVE. B #4, D 0 CMP. B #1, D 0 DBEQ D 6, LABEL MOVE. B #$12, D 4 MOVE. B #$34, D 5 LABEL MOVE. B END DBEQ #1, D 2 START

Creating for Loops using DBF You can create for… loops using DBF (Decrement and

Creating for Loops using DBF You can create for… loops using DBF (Decrement and Branch if False). n In DBF, result is always false: n ¨ Loop only determined by counter. ¨ Same characteristics with for loops. n Counter reaches -1: ¨ If n loops needed, counter = n - 1.

DBF Format Set D 0 = no. of repetitions -1 NEXT If FALSE, or

DBF Format Set D 0 = no. of repetitions -1 NEXT If FALSE, or D 0 ≠-1, D 0 = D 0 – 1 Go to NEXT Instruction #1 Instruction #2 Instruction #3 DBF D 0, NEXT Instruction #4 If TRUE, or D 0 = -1, go to next line

Example n Write a program that fills memory locations $2000 to $2009 with value

Example n Write a program that fills memory locations $2000 to $2009 with value $FF. $2000 $2001 $2002 $2003 $2004 $2005 $2006 $2007 $2008 $2009 $00 $00 $00 $2000 $2001 $2002 $2003 $2004 $2005 $2006 $2007 $2008 $2009 $FF $FF $FF

Solution Lets say D 2 is the counter. n N = 10, n ¨

Solution Lets say D 2 is the counter. n N = 10, n ¨ D 2 =N– 1 ¨ D 2 = 10 – 1 ¨ D 2 = 9

Program START LOOP ORG $1000 LEA MOVE. B $2000, A 0 #9, D 2

Program START LOOP ORG $1000 LEA MOVE. B $2000, A 0 #9, D 2 MOVE. B DBF #$FF, (A 0)+ D 2, LOOP END START

Example n Create a program that displays “Hello World!” 100 times.

Example n Create a program that displays “Hello World!” 100 times.

Solution Lets say D 2 is the counter. n N = 100, n ¨

Solution Lets say D 2 is the counter. n N = 100, n ¨ D 2 =N– 1 ¨ D 2 = 100 – 1 = 99 ¨ D 2 = $0063.

Displaying the Text n TRAP #15 used by Easy 68 k for text I/O:

Displaying the Text n TRAP #15 used by Easy 68 k for text I/O: ¨ D 0, D 1 and A 1 must be reserved: D 0 set to 0 to display text. n A 1 is starting address for ‘Hello World!’ string. n D 1 used to indicate string length: 12 characters. n

Step 1: Put ‘Hello World’ into Memory HELLO ORG DC. B $1000 'Hello World!'

Step 1: Put ‘Hello World’ into Memory HELLO ORG DC. B $1000 'Hello World!' 0

Step 2: Set Variables START ORG $2000 MOVE. L MOVE. W MOVEA. L MOVE.

Step 2: Set Variables START ORG $2000 MOVE. L MOVE. W MOVEA. L MOVE. W #0, D 0 #99, D 2 #HELLO, A 1 #12, D 1 Display string mode Counter = 99 Move string address to A 1 String length

Step 3: Loop to Display Text NEXT D 2 = D 2 -1 TRAP

Step 3: Loop to Display Text NEXT D 2 = D 2 -1 TRAP DBF #15 D 2, NEXT D 2 = -1? END START

Assembly Code HELLO ORG DC. B $1000 'Hello World!' 0 START ORG $2000 MOVE.

Assembly Code HELLO ORG DC. B $1000 'Hello World!' 0 START ORG $2000 MOVE. L MOVE. W MOVEA. L MOVE. W #0, D 0 #99, D 2 #HELLO, A 1 #12, D 1 ; ; TRAP DBF #15 D 2, NEXT ; WILL EXIT WHEN D 2 REACHES -1 END START NEXT SET TO OUTPUT STRING SET COUNTER TO 99 MOVE DC ADDRESS TO A 1 THIS IS LENGTH OF CHARACTER

Result

Result

Conditional Set (Scc)

Conditional Set (Scc)

Scc (Set According to Condition) n Check conditions and adjust byte: ¨ If condition

Scc (Set According to Condition) n Check conditions and adjust byte: ¨ If condition TRUE, set all bits to 1. ¨ If false, set all to 0.

Scc Flowchart START Evaluate conditions using CMP TRUE Condition = TRUE? Set all bits

Scc Flowchart START Evaluate conditions using CMP TRUE Condition = TRUE? Set all bits in lower byte ($FF). FALSE Clear all bits in lower byte ($00). FINISH

Scc Example D 0 = $12345678 D 1 = $12345678 CMP. B D 0,

Scc Example D 0 = $12345678 D 1 = $12345678 CMP. B D 0, D 1 SEQ D 2 *Original D 2 = $0000 D 2. B = $FF if D 1=D 0, else D 2. B = $00 D 1. B ($78) = D 0. B ($78), condition is TRUE. D 2. B = 0 0 0 0 D 2. B = 1 1 1 1 Final D 2 = $000000 FF

Try It Yourself START ORG MOVE. L CMP. B SEQ END $1000 #$12345678, D

Try It Yourself START ORG MOVE. L CMP. B SEQ END $1000 #$12345678, D 1 D 0, D 1 D 2 START

Scc Example D 0 = $12345678 D 1 = $10000056 CMP. B D 0,

Scc Example D 0 = $12345678 D 1 = $10000056 CMP. B D 0, D 1 SGT D 1. B = $FF if D 1>D 0, else D 1. B = $00 D 1. B ($56) < D 0. B ($78), condition is FALSE. D 1. B = 0 1 0 1 1 0 D 1. B = 0 0 0 0 Final D 1 = $10000000

Subroutine Control

Subroutine Control

Branch to Subroutine (BSR) n Similarities to BRA: ¨ Branches unconditionally to another location.

Branch to Subroutine (BSR) n Similarities to BRA: ¨ Branches unconditionally to another location. ¨ Same limitations (32, 676 forward/back). n Differences: ¨ Location is a sub-routine. ¨ Return address pushed to stack. ¨ Subroutine returns using RTS.

Subroutines Main Program SR 1

Subroutines Main Program SR 1

Jump to Subroutine (JSR) n Similar to BSR, but without addressing limit. ¨ Can

Jump to Subroutine (JSR) n Similar to BSR, but without addressing limit. ¨ Can n jump anywhere. Features: ¨ The location is a sub-routine. ¨ Return address pushed to stack. ¨ Subroutine returns using RTS.

RTS (Return from Subroutine) Used to return from subroutine. n Pops return address from

RTS (Return from Subroutine) Used to return from subroutine. n Pops return address from stack. n Complements BSR & JSR: n ¨ Put at end of subroutine. ¨ Reloads PC from stack. ¨ Execution resumes at new PC.

How BSR works MAIN PROGRAM $1000 $1002 $1004 MOVE. B #100, D 0 BSR

How BSR works MAIN PROGRAM $1000 $1002 $1004 MOVE. B #100, D 0 BSR SUBR 1. Program executes normally until BSR is encountered. MOVE. B D 0, D 1 PC STACK POINTER $FFFB $FFFD SP $FFFF SUBROUTINE SUBR $2000 $2002 $2004 MULU D 0, D 0 RTS $1004

How BSR works MAIN PROGRAM $1000 $1002 $1004 MOVE. B #100, D 0 BSR

How BSR works MAIN PROGRAM $1000 $1002 $1004 MOVE. B #100, D 0 BSR PC $2000 SUBR MOVE. B D 0, D 1 3. Branches to SUBR by updating PC. 2. BSR saves current PC onto stack. STACK POINTER SUBROUTINE SUBR $FFFB SP $FFFD $FFFF $001004 $2000 $2002 $2004 MULU D 0, D 0 RTS

How BSR works MAIN PROGRAM $1000 $1002 $1004 MOVE. B #100, D 0 BSR

How BSR works MAIN PROGRAM $1000 $1002 $1004 MOVE. B #100, D 0 BSR SUBR MOVE. B D 0, D 1 PC STACK POINTER SUBROUTINE SUBR $FFFB SP $FFFD $FFFF $001004 $2000 $2002 $2004 4. Execution resumes here, until encounters RTS. MULU D 0, D 0 RTS

How BSR works MAIN PROGRAM $1000 $1002 $1004 MOVE. B #100, D 0 BSR

How BSR works MAIN PROGRAM $1000 $1002 $1004 MOVE. B #100, D 0 BSR SUBR MOVE. B D 0, D 1 PC 5. When RTS is encountered, M 68 k pops the stack and loads into PC. STACK POINTER SUBROUTINE SUBR $FFFB SP $FFFD $FFFF $001004 $2000 $2002 $2004 MULU D 0, D 0 RTS $1004

How BSR works MAIN PROGRAM $1000 $1002 $1004 MOVE. B #100, D 0 BSR

How BSR works MAIN PROGRAM $1000 $1002 $1004 MOVE. B #100, D 0 BSR SUBR MOVE. B D 0, D 1 PC 6. Execution continues normally where BSR left off. STACK POINTER $FFFB $FFFD SP $FFFF SUBROUTINE SUBR $2000 $2002 $2004 MULU D 0, D 0 RTS $1004

Sample Program AAA START ORG MULU RTS $1000 D 0, D 0 ORG MOVE.

Sample Program AAA START ORG MULU RTS $1000 D 0, D 0 ORG MOVE. B BSR MOVE. B $2000 #100, D 0 AAA D 0, D 1 END START

Conclusion

Conclusion

Summary of Instructions BRA JMP Application Perform unconditional branching. Method Modifies PC to point

Summary of Instructions BRA JMP Application Perform unconditional branching. Method Modifies PC to point to new location. Addressing Mode Relative Absolute Limitations Limited addressing range (16 -bit signed displacement). Unlimited

Summary of Instructions Description Method Applications Limitations Bcc DBcc Performs conditional branching Same as

Summary of Instructions Description Method Applications Limitations Bcc DBcc Performs conditional branching Same as Bcc, has counter. Scc Conditionally sets/clears lower byte based on test result. Decrement, branch Branch to label if condition Set byte to $FF if condition false, execute next if is condition true, Else execute condition true else set to $00. next instruction. or counter = -1. If, if. . else, for, while For Testing conditions. Limited addressing range (16 -bit signed displacement). N/A

Summary of Instructions BSR Application Branching Method Addressing Mode Limitations Branch unconditionally to subroutine.

Summary of Instructions BSR Application Branching Method Addressing Mode Limitations Branch unconditionally to subroutine. Modifies PC to point to new location. Saves return address to stack. Relative Absolute Limited addressing range (16 -bit signed displacement). RTS Description Method JSR Unlimited RTR Used to return from subroutine. Loads address from stack, put into PC. Loads address & PC from stack, put into PC, CCR.

The End Please read: Antonakos, pg. 83 -90.

The End Please read: Antonakos, pg. 83 -90.