Lecture-4-Branching
Lecture-4-Branching
Lecture 4
Branching
CSE 3442/5442
Embedded Systems I
Based heavily on slides by Dr. Gergely Záruba and Dr. Roger Walker
Program ROM
2
PIC18 Program ROM Space
Review from Last Lecture
25H MOVLW
34H ADDLW
11H ADDLW
3
DECFSZ Instruction
DECFSZ fileReg, d ; Decrement fileReg and Skip next instruction if new value is 0
; if d==0 or d==w put new decremented value in WREG
; if d==1 or d==f put …. in fileReg
4
DECFSZ Instruction
7
Adds 3 to WREG 10 times
COUNT EQU 0x25 0x000 0
… 0
MOVLW D’10’ “COUNT” 0x025 0
MOVWF COUNT … 0
MOVLW 0
0
AGAIN ADDLW 3
0
DECFSZ COUNT, F
0
GOTO AGAIN
(Program ROM) 0xFFF
WREG
0
8
Adds 3 to WREG 10 times
COUNT EQU 0x25 0x000 0
… 0
MOVLW D’10’ “COUNT” 0x025 0
MOVWF COUNT … 0
MOVLW 0
0
AGAIN ADDLW 3
0
DECFSZ COUNT, F
0
GOTO AGAIN
(Program ROM) 0xFFF
WREG
10
9
Adds 3 to WREG 10 times
COUNT EQU 0x25 0x000 0
… 0
MOVLW D’10’ “COUNT” 0x025 10
MOVWF COUNT … 0
MOVLW 0
0
AGAIN ADDLW 3
0
DECFSZ COUNT, F
0
GOTO AGAIN
(Program ROM) 0xFFF
WREG
10
10
Adds 3 to WREG 10 times
COUNT EQU 0x25 0x000 0
… 0
MOVLW D’10’ “COUNT” 0x025 10
MOVWF COUNT … 0
MOVLW 0
0
AGAIN ADDLW 3
0
DECFSZ COUNT, F
0
GOTO AGAIN
(Program ROM) 0xFFF
WREG
0
11
Adds 3 to WREG 10 times
COUNT EQU 0x25 0x000 0
… 0
MOVLW D’10’ “COUNT” 0x025 10
MOVWF COUNT … 0
MOVLW 0
0
AGAIN ADDLW 3
0
DECFSZ COUNT, F
0
GOTO AGAIN
(Program ROM) 0xFFF
WREG
3
12
Adds 3 to WREG 10 times
COUNT EQU 0x25 0x000 0
… 0
MOVLW D’10’ “COUNT” 0x025 9
MOVWF COUNT … 0
MOVLW 0
0
AGAIN ADDLW 3 F–1F 0
DECFSZ COUNT, F 10 – 1 COUNT
0
GOTO AGAIN 9 != 0, No Skip
(Program ROM) 0xFFF
WREG
3
13
Adds 3 to WREG 10 times
COUNT EQU 0x25 0x000 0
… 0
MOVLW D’10’ “COUNT” 0x025 9
MOVWF COUNT … 0
MOVLW 0
0
AGAIN ADDLW 3
0
DECFSZ COUNT, F
0
GOTO AGAIN
(Program ROM) 0xFFF
WREG
3
14
Adds 3 to WREG 10 times
COUNT EQU 0x25 0x000 0
… 0
MOVLW D’10’ “COUNT” 0x025 9
MOVWF COUNT … 0
MOVLW 0
0
AGAIN ADDLW 3
0
DECFSZ COUNT, F
0
GOTO AGAIN
(Program ROM) 0xFFF
WREG
6
15
Adds 3 to WREG 10 times
COUNT EQU 0x25 0x000 0
… 0
MOVLW D’10’ “COUNT” 0x025 8
MOVWF COUNT … 0
MOVLW 0
0
AGAIN ADDLW 3 F–1F 0
DECFSZ COUNT, F 9 – 1 COUNT
0
GOTO AGAIN 8 != 0, No Skip
(Program ROM) 0xFFF
WREG
6
16
Adds 3 to WREG 10 times
cont…
COUNT EQU 0x25 0x000 0
… 0
MOVLW D’10’ “COUNT” 0x025 1
MOVWF COUNT … 0
MOVLW 0
0
AGAIN ADDLW 3
0
DECFSZ COUNT, F
0
GOTO AGAIN
(Program ROM) 0xFFF
WREG
30
17
Adds 3 to WREG 10 times
cont…
COUNT EQU 0x25 0x000 0
… 0
MOVLW D’10’ “COUNT” 0x025 0
MOVWF COUNT … 0
MOVLW 0
0
AGAIN ADDLW 3 F–1F 0
DECFSZ COUNT, F 1 – 1 COUNT
0
GOTO AGAIN 0 == 0, Skip
(Program ROM) 0xFFF
WREG
30
18
Adds 3 to WREG 10 times
cont…
COUNT EQU 0x25 0x000 0
… 0
MOVLW D’10’ “COUNT” 0x025 0
MOVWF COUNT … 0
MOVLW 0
0
AGAIN ADDLW 3
0
DECFSZ COUNT, F Skip the very 0
GOTO AGAIN … next instruction
(Program ROM) 0xFFF
WREG
30
19
Another way: Branch Non-Zero
BNZ n
• Adds 3 to WREG 10 times
20
DECFSZ vs. BNZ
24
BRA (Branch Unconditionally)
Instruction Address Range
BRA n
25
GOTO Instruction
26
Stack
27
Stack
• Subroutines require stacks
• CALL and RCALL instructions can create subroutines (RETURN)
– They are jumps but put the current PC onto the stack
• Program Counter needs to be stored so microcontroller knows
where to return
• Stack thus has 21-bit words
– Needs to be longer than one unit as there may be nested subroutines
– Stack is separate RAM close to the CPU
• Separate 5-bit register (SP) for keeping track of stack (relative
address)
– SP is incremented from 0!
• User has to “stack” (store) other registers.
28
PIC Stack 31 × 21
29
CALL Instruction
CALL k
30
CALL Instruction
31
Assembly Main Program That
Calls Subroutine (MPLAB ex.)
32
Simple Pipeline vs.
Non-pipeline
Most instructions take one or two cycles to execute
33
Branch Instructions can take
more cycles to execute
34
Branch Instructions can take
more cycles to execute
MOVLW D’10’
MOVWF COUNT
MOVLW 0
AGAIN ADDLW 3
DECF COUNT, F
BNZ AGAIN
MOVWF PORTB
35
What is in an Instruction Cycle
37