0% found this document useful (0 votes)
2 views

Lecture-4-Branching

Uploaded by

Sheri Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture-4-Branching

Uploaded by

Sheri Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

The University of Texas at Arlington

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

In executing this instruction, the specified fileReg is


5
decremented, and if contents zero skips next instruction
DECFSZ Instruction

• Adds 3 to WREG 10 times

COUNT EQU 0x25


MOVLW D’10’ ;10  WREG
MOVWF COUNT ;10 (W)  COUNT (0x25)
MOVLW 0 ;0  WREG
AGAIN ADDLW 3
DECFSZ COUNT,F
GOTO AGAIN
….
6
Adds 3 to WREG 10 times
COUNT EQU 0x25 0x000 0
… 0
MOVLW D’10’ 0x025 0
MOVWF COUNT … 0
MOVLW 0
0
AGAIN ADDLW 3
0
DECFSZ COUNT, F
0
GOTO AGAIN
(Program ROM) 0xFFF
WREG
0

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–1F 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–1F 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–1F 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

COUNT EQU 0x25


MOVLW D’10’
MOVWF COUNT
MOVLW 0
AGAIN ADDLW 3
DECF COUNT,F
BNZ AGAIN

20
DECFSZ vs. BNZ

Adds 3 to WREG 10 times

COUNT EQU 0x25 COUNT EQU 0x25

MOVLW D’10’ MOVLW D’10’


MOVWF COUNT MOVWF COUNT
MOVLW 0 MOVLW 0
AGAIN ADDLW 3 AGAIN ADDLW 3
DECFSZ COUNT,F DECF COUNT,F
GOTO AGAIN BNZ AGAIN
…. ….

“All inclusive” instruction vs. using the status register


21
Nested Loops
“Loops inside Loops”
• Repeat an action more than 255 times
• Branching is done based on the status
register which reflects the last instruction
• Textbook Ex: pdf pg 118
• Do something 700 times
for( i  700) {action} // but 700 > 255 so…
for( i  70 )
for( j  10 )
{action} 22
Other Conditional Branch
Instructions
BC (C=1)
BNC
BZ (Z=1)
BNZ
BN (N=1)
BNN
BOV (OV=1)
BNOV

Note: (All conditional branches are 2 bytes thus 23


represent short jumps, within ~ +/-128 bits to PC)
Jumping Range in Program ROM
for Conditional Branches

24
BRA (Branch Unconditionally)
Instruction Address Range

BRA n

25
GOTO Instruction

GOTO k 4 Byte 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

Each micro operation takes one clock cycle, thus


36
instruction rate is a quarter of the clock rate.
Questions?

• Textbook Ch. 3-1, 3-2 for Branching


examples and more details
• Start reading Chapter 4
– PIC I/O

37

You might also like