program control
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/1.jpg)
Program Control
ECE511: Digital System & Microprocessor
![Page 2: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/2.jpg)
What we are going to learn in this session: Program Control:
What are they.Available instructions.How they effect program flow.
![Page 3: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/3.jpg)
Introduction
![Page 4: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/4.jpg)
Program Control Group
Instructions that perform Conditional execution.Unconditional execution.Subroutine handling.Achieves this through branching.
Manipulate PC.
![Page 5: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/5.jpg)
The Program Control Group
Program Control
Conditional Branch
Branch Always
Decrement & Branch
Conditional Set
Branch to Subroutine
Jump to Subroutine
Unconditional Jump
Return from Subroutine
Return from Subroutine & Restore CCR
![Page 6: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/6.jpg)
Unconditional Branch
![Page 7: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/7.jpg)
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.
![Page 8: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/8.jpg)
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.
![Page 9: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/9.jpg)
BRA Format
BRA <ea> BRA $001000
BRA LABELNAME BRA THERE
Example:
![Page 10: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/10.jpg)
Unconditional Branching Example
Instruction #1Instruction #2
BRA HERE
Instruction #3Instruction #4
HERE Instruction #5Instruction #6
Never executed.
![Page 11: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/11.jpg)
Infinite Loop Example
LABELNAME Instructions #1
Instructions #2
…
Instructions #n
BRA LABELNAME
![Page 12: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/12.jpg)
BRA Example
START ORG $1000
BRAHERE MOVE.B #9,D0MOVE.B #19,D1ADD.B D1,D0BRA BRAHERE
END START
![Page 13: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/13.jpg)
![Page 14: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/14.jpg)
![Page 15: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/15.jpg)
![Page 16: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/16.jpg)
![Page 17: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/17.jpg)
![Page 18: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/18.jpg)
![Page 19: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/19.jpg)
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).
![Page 20: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/20.jpg)
BRA Limitations
Find the allowed range when BRA address is at $030000.
$030000 = 196,608Max forward = 196,608 + 32,767 = 229,375 = $37FFF.Max backward = 196,608 – 32,768 = 163,840 = $28000.
$28000 < Range < $37FFF
![Page 21: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/21.jpg)
![Page 22: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/22.jpg)
JMP (Unconditional Jump)
Similar to BRA, but without limitations:Jumps to any location in program.Not limited to 16-bit displacements anymore.
![Page 23: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/23.jpg)
![Page 24: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/24.jpg)
Conditional Branch
![Page 25: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/25.jpg)
Bcc
![Page 26: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/26.jpg)
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.
![Page 27: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/27.jpg)
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.
![Page 28: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/28.jpg)
Conditions (cc)
Bcc
Unsigned
Signed
BGT
BGE
BEQ
BNE
BLE
BLT
BHI
BCC
BEQ
BNE
BLS
BCS
D0.B > D1.B
D0.B ≥ D1.B
D0.B = D1.B
D0.B ≠ D1.B
D0.B ≤ D1.B
D0.B < D1.B
D0.B > D1.B
D0.B ≥ D1.B
D0.B = D1.B
D0.B ≠ D1.B
D0.B ≤ D1.B
D0.B < D1.B
CMP.B D1,D0
*(Antonakos, pg. 84)
![Page 29: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/29.jpg)
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)
![Page 30: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/30.jpg)
Bcc ExampleD0 = $000000FED1 = $00000023CMP.B D1,D0
X N Z V C
- 1 0 0 0
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
![Page 31: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/31.jpg)
Bcc Flowchart
START
Evaluate conditions using CMP
Condition = TRUE?
Go to LABEL Execute next instruction
FINISH
TRUE FALSE
![Page 32: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/32.jpg)
Bcc Format
LABELNAMEInstructions #1Instructions #2
…Instructions #nTest ConditionBcc LABELNAME
Instructions after Bcc…
If condition = TRUE
If conditions = FALSE
![Page 33: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/33.jpg)
Bcc Format
Test Condition Bcc LABELInstructions #1Instructions #2
…Instructions #n
LABEL Instructions after LABEL…
If conditions = TRUE,Instructions 1 n will not be executed
If conditions = FALSE,All instructions executedline-by-line.
![Page 34: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/34.jpg)
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.
![Page 35: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/35.jpg)
FlowchartSTART
Move valueto D3
D3 > 10?
Clear D3 D3 = D3 + 3
FINISH
True False
![Page 36: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/36.jpg)
Assembly CodeSTART ORG $1000
MOVE.B #11,D3
CMP.B #10,D3BHI MOREBLE LESS
MORE CLR.B D3BRA FINISH
LESS ADD.B #3,D3BRA FINISH
FINISH END START
D3.B > 10
D3.B < 10
![Page 37: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/37.jpg)
Bcc Example – If…
Check contents of D0: If D0 > 10, add 54 to it. If D0 ≤ 10, do nothing.
![Page 38: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/38.jpg)
Flowchart
START
Move valueto D0
D0 > 10?
D0 = D0 + 54
FINISH
True False
![Page 39: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/39.jpg)
When D0 ≤ 10
START ORG $1000
MOVE.B #5,D0CMP #10,D0
BLE DONTHNG
ADD.B #54,D0
DONTHNG NOP
END START
If D0 ≤ 10
![Page 40: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/40.jpg)
![Page 41: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/41.jpg)
![Page 42: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/42.jpg)
Condition = TRUE
![Page 43: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/43.jpg)
![Page 44: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/44.jpg)
When D0 > 10
START ORG $1000
MOVE.B #15,D0CMP #10,D0
BLE DONTHNG
ADD.B #54,D0
DONTHNG NOP
END START
![Page 45: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/45.jpg)
![Page 46: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/46.jpg)
![Page 47: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/47.jpg)
Condition = FALSE,Executes next line
![Page 48: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/48.jpg)
![Page 49: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/49.jpg)
This line also executed.
![Page 50: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/50.jpg)
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
![Page 51: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/51.jpg)
Bcc Example: Do..While Loop
Add the contents of memory locations $2000, $2001, $2002 together using the do..while loop. Store the results inside D0.
![Page 52: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/52.jpg)
FlowchartSTART
D1 = 0 D0 = 0
D1 = 3?
Go to next mem. location
FINISH
True
False
Add mem. to D0
D1 = D1 + 1
![Page 53: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/53.jpg)
Example ProgramSTART ORG $1000
MOVE.B #$12,$2000 ; PUT $12 INTO ADDRESS $2000MOVE.B #$34,$2001 ; PUT $34 INTO ADDRESS $2001MOVE.B #$56,$2002 ; PUT $56 INTO ADDRESS $2002
MOVE.B #0,D1 ; D1 AS COUNTERCLR.L D0 ; D0 CLEARED, SUM STORED HERE
LEA $2000,A0 ; MOVE ADDRESS $2000 TO A0
LOOP ADD.B (A0)+,D0 ; ADD MEMORY CONTENT TO D0; INCREMENT A0 BY 1 TO POINT TO NEXT
; MEM. LOCATIONADD.B #1,D1 ; INCREMENT COUNTER BY 1CMP.B #3,D1 ; BNE LOOP ; IF COUNTER ≠ 3, LOOP BACK
END START
![Page 54: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/54.jpg)
Program Flow
LOOP ADD.B (A0)+,D0ADD.B #1,D1 CMP.B #3,D1
BNE LOOP
END START
If D1 ≠ 3
If D1 = 3
![Page 55: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/55.jpg)
DBcc
![Page 56: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/56.jpg)
DBcc (Test, Decrement and Branch)
Similar to Bcc, except:Now adds a counter.Counter decremented at each pass.Loop exits when:
Condition is TRUE, or Counter reaches -1.
![Page 57: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/57.jpg)
DBcc (Test, Decrement and Branch)
Counter:Word value in data register (Dn.W).Decremented after each pass. -1 ≤ Range ≤ 32,767.
![Page 58: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/58.jpg)
Difference between DBcc and Bcc
Bcc DBcc
Condition tested is TRUE
Condition tested is FALSE or when
counter = -1.
Go to next line when..Condition tested
is FALSE.
Branches to LABEL when..
Condition tested is TRUE or when
counter = -1.
![Page 59: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/59.jpg)
Difference between Bcc & DBcc
MOVE.B #1,D0
CMP.B #1,D0
BEQ LABEL
Instruction #1
Instruction #2
LABEL MOVE.B #1,D2
MOVE.B#3,D7
MOVE.B #1,D0
CMP.B #1,D0
DBEQ D7,LABEL
Instruction #1
Instruction #2
LABEL MOVE.B #1,D2
![Page 60: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/60.jpg)
DBcc Format
Set D0 = no. of repetitions -1
NEXT Instruction #1Instruction #2Instruction #3Test ConditionDBcc D0,NEXT
Instruction #4
If FALSE, or D0 ≠-1, D0 = D0 – 1Go to NEXT
If TRUE, or D0 = -1, go to next line
![Page 61: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/61.jpg)
Creating for Loops using DBF
You can create for… loops using DBF (Decrement and Branch if False).
In DBF, result is always false:Loop only determined by counter.Same characteristics with for loops.
Counter reaches -1: If n loops needed, counter = n - 1.
![Page 62: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/62.jpg)
Creating for… Loops using DBF
START
Evaluate conditions using CMP
Counter = -1 ?
Go to LABELExecute next instruction
FINISH
TRUEFALSE
Decrement counter
![Page 63: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/63.jpg)
Example
Write a program that fills memory locations $2000 to $2009 with value $FF.
$00$2001$2000
$2003$2002
$2005$2004
$2007$2006
$2009$2008 $00
$00$00
$00$00$00
$00
$00
$00
$FF$2001$2000
$2003$2002
$2005$2004
$2007$2006
$2009$2008 $FF
$FF$FF
$FF$FF$FF
$FF
$FF
$FF
![Page 64: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/64.jpg)
Solution
Lets say D2 is the counter. N = 10,
D2 = N – 1D2 = 10 – 1D2 = 9
![Page 65: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/65.jpg)
Program
LEA $2000,A0
MOVE.B #9,D2
LOOP MOVE.B #$FF,(A0)+
DBF D2,LOOP
![Page 66: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/66.jpg)
Example
Create a program that displays “Hello World!” 100 times.
![Page 67: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/67.jpg)
Solution
Lets say D2 is the counter. N = 100,
D2 = N – 1D2 = 100 – 1 = 99D2 = $0063.
![Page 68: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/68.jpg)
Displaying the Text
TRAP #15 used by Easy68k for text I/O:D0, D1 and A1 must be reserved:
D0 set to 0 to display text. A1 is starting address for ‘Hello World!’ string. D1 used to indicate string length: 12 characters.
![Page 69: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/69.jpg)
Step 1: Put ‘Hello World’ into Memory
ORG $1000
HELLO DC.B 'Hello World!'
DC.B 0
![Page 70: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/70.jpg)
![Page 71: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/71.jpg)
Step 2: Set Variables
START ORG $2000
MOVE.L #0,D0MOVE.W #99,D2MOVEA.L #HELLO,A1MOVE.W #12,D1
Display string mode
Counter = 99
Move string address to A1 String length
![Page 72: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/72.jpg)
Step 3: Loop to Display Text
NEXT TRAP #15
DBF D2,NEXT
END START
D2 = -1?D2 = D2 -1
![Page 73: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/73.jpg)
Assembly Code
ORG $1000HELLO DC.B 'Hello World!'
DC.B 0
START ORG $2000
MOVE.L #0,D0 ; SET TO OUTPUT STRINGMOVE.W #99,D2 ; SET COUNTER TO 99MOVEA.L #HELLO,A1 ; MOVE DC ADDRESS TO A1MOVE.W #0012,D1 ; THIS IS LENGTH OF CHARACTER
NEXT TRAP #15DBF D2,NEXT ; WILL EXIT WHEN D2 REACHES -1
END START
![Page 74: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/74.jpg)
Result
![Page 75: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/75.jpg)
Conditional Set (Scc)
![Page 76: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/76.jpg)
Scc (Set According to Condition)
Check conditions and adjust byte: If condition TRUE, set all bits to 1. If false, set all to 0.
![Page 77: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/77.jpg)
Scc Flowchart
START
Evaluate conditions using CMP
Condition = TRUE?
Set all bits in lower byte ($FF).
Clear all bits in lower byte ($00).
FINISH
TRUE FALSE
![Page 78: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/78.jpg)
Scc ExampleD0 = $12345678D1 = $12345678CMP.B D0,D1SEQ D2
D1.B ($78) = D0.B ($78), condition is TRUE.
0 0 0 0 0 0 0 0D2.B =
1 1 1 1 1 1 1 1D2.B =
Final D2 = $000000FF
D2.B = $FF if D1=D0, else D2.B = $00
*Original D2 = $00000000
![Page 79: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/79.jpg)
Scc ExampleD0 = $12345678D1 = $10000056CMP.B D0,D1SGT D1
D1.B ($56) < D0.B ($78), condition is FALSE.
0 1 0 1 0 1 1 0D1.B =
0 0 0 0 0 0 0 0D1.B =
Final D1 = $10000000
D1.B = $FF if D1>D0, else D1.B = $00
![Page 80: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/80.jpg)
Subroutine Control
![Page 81: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/81.jpg)
Branch to Subroutine (BSR)
Similarities to BRA:Branches unconditionally to another location.Same limitations (32,676 forward/back).
Differences:Location is a sub-routine.Return address pushed to stack.Subroutine returns using RTS.
![Page 82: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/82.jpg)
SubroutinesMain Program
SR1
![Page 83: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/83.jpg)
Jump to Subroutine (JSR)
Similar to BSR, but without addressing limit.Can jump anywhere.
Features:The location is a sub-routine.Return address pushed to stack.Subroutine returns using RTS.
![Page 84: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/84.jpg)
RTS (Return from Subroutine)
Used to return from subroutine. Pops return address from stack. Complements BSR & JSR:
Put at end of subroutine.Reloads PC from stack.Execution resumes at new PC.
![Page 85: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/85.jpg)
RTR (Return, Restore CCR)
Used to return from subroutine, and restore CCR.
Pops return address + CCR from stack. CCR not saved automatically by BSR,
JSR:Must write yourself.
![Page 86: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/86.jpg)
How BSR worksMAIN PROGRAM
$1000 MOVE.B #100,D0
$1002 BSR SUBR
$1004 MOVE.B D0,D1
SUBROUTINE SUBR
$2000 MULU D0,D0
$2002 RTS
$2004
STACK POINTER
$FFFB
$FFFD
$FFFF
1. Program executes normally until BSR is encountered.
SP
PC $1004
![Page 87: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/87.jpg)
How BSR worksMAIN PROGRAM
$1000 MOVE.B #100,D0
$1002 BSR SUBR
$1004 MOVE.B D0,D1
SUBROUTINE SUBR
$2000 MULU D0,D0
$2002 RTS
$2004
STACK POINTER
$FFFB
$FFFD
$FFFF
$001004
2. BSR saves current PC onto stack.
SP
3. Branches to SUBR by updating PC.
PC $2000
![Page 88: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/88.jpg)
How BSR worksMAIN PROGRAM
$1000 MOVE.B #100,D0
$1002 BSR SUBR
$1004 MOVE.B D0,D1
SUBROUTINE SUBR
$2000 MULU D0,D0
$2002 RTS
$2004
STACK POINTER
$FFFB
$FFFD
$FFFF
$001004SP
4. Execution resumes here, until encounters RTS.
PC $2004
![Page 89: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/89.jpg)
How BSR worksMAIN PROGRAM
$1000 MOVE.B #100,D0
$1002 BSR SUBR
$1004 MOVE.B D0,D1
SUBROUTINE SUBR
$2000 MULU D0,D0
$2002 RTS
$2004
STACK POINTER
$FFFB
$FFFD
$FFFF
$001004SP
5. When RTS is encountered, M68k pops the stack and loads into PC.
PC $1004
![Page 90: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/90.jpg)
How BSR worksMAIN PROGRAM
$1000 MOVE.B #100,D0
$1002 BSR SUBR
$1004 MOVE.B D0,D1
SUBROUTINE SUBR
$2000 MULU D0,D0
$2002 RTS
$2004
STACK POINTER
$FFFB
$FFFD
$FFFFSP
6. Execution continues normally where BSR left off.
PC $1004
![Page 91: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/91.jpg)
Sample Program
ORG $1000
AAA MULU D0,D0RTS
START ORG $2000
MOVE.B #100,D0
BSR AAA
MOVE.B D0,D1
END START
![Page 92: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/92.jpg)
![Page 93: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/93.jpg)
![Page 94: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/94.jpg)
![Page 95: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/95.jpg)
![Page 96: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/96.jpg)
![Page 97: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/97.jpg)
Conclusion
![Page 98: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/98.jpg)
Summary of Instructions
JMPBRA
Perform unconditional branching.Application
AbsoluteRelative
Method
UnlimitedLimited addressing range
(16-bit signed displacement).Limitations
Modifies PC to point to new location.
Addressing Mode
![Page 99: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/99.jpg)
Summary of InstructionsDBccBcc
Description
Method
Scc
Performs conditional branching
Same as Bcc, has counter.
Conditionally sets/clears
lower byte based ontest result.
Branch to label if condition
true, Else execute next instruction.
Set byte to $FF is condition true, else set to $00.
Decrement, branch to label if condition
false, execute next if condition true or counter = -1.
Applications If, if..else, for, while For Testing conditions.
N/ALimited addressing range
(16-bit signed displacement).Limitations
![Page 100: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/100.jpg)
Summary of InstructionsJSRBSR
Branch unconditionally to subroutine.Application
AbsoluteRelative
Branching Method
UnlimitedLimited addressing range
(16-bit signed displacement).Limitations
Modifies PC to point to new location. Saves return address to stack.
Addressing Mode
RTRRTS
Used to return from subroutine.Description
MethodLoads address from stack, put into PC.
Loads address & PC from stack, put into PC, CCR.
![Page 101: Program Control](https://reader035.vdocuments.us/reader035/viewer/2022062519/56814dd0550346895dbb2b20/html5/thumbnails/101.jpg)
The End
Please read:
Antonakos, pg. 83-90.