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

Chapter 3

The document discusses jump and call instructions in AVR microcontrollers. It explains that these instructions allow a program to execute code in a non-sequential order by jumping or calling functions. It provides examples of when these instructions are needed and describes unconditional and conditional jumps.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

Chapter 3

The document discusses jump and call instructions in AVR microcontrollers. It explains that these instructions allow a program to execute code in a non-sequential order by jumping or calling functions. It provides examples of when these instructions are needed and describes unconditional and conditional jumps.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 50

Jump & Call

Chapter 3

The AVR microcontroller


and embedded
systems
using assembly and c

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Topics
 Introduction to jump and call instructions
 Jump
 Call
 Stack
 Calling a function
 Program/code execution time
 Generate time delay in software

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Jump and Call
 CPU executes instructions one
after another.
1 void main ()
 For example in the following C 2 {
program, CPU first executes the 3 a = b + c;

instruction of line 3 (adds b and 4 c -= 2;

c), then executes the instruction 5 d = a + c;


6 }
of line 4.

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Jump and Call (Continued)
 But sometimes we need the CPU to execute, an
instruction other than the next instruction. For
example:
 When we use a conditional instruction (if)
 When we make a loop
 When we call a function

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Jump and Call (Continued)
 Example 1: Not executing the
next instruction, because of
condition. 1 void main ()
 In the following example, the 2 {

instruction of line 6 is not


3 int a = 2;
4 int c = 3;
executed. 5 if (a == 8)
6 c = 6;
7 else
8 c = 7;
9 c = a + 3;
}

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Jump and Call (Continued)
 Example 2: In this example
the next instruction will not
be executed because of 1 void main ()
loop. 2 {
3
In the following example,
int a, c = 0;

4 for(a = 2; a < 4; a++)

the order of execution is as 5 c += a;

follows: 6
7 }
a = c + 2;

 Line 4 8
9
 Line 5
 Again, line 4
 Again line 5
 Line 6
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Jump and Call (Continued)
 Example 3: Not executing
the next instruction, because
of calling a function. 1
Code
void func1 ();
 In the following example, 2 void main ()

the instruction of line 6 is


3 {
4 int a = 2, c = 3;
not executed after line 5. 5 func1 ();
6 c = a + 3;
7 }
8 void func1 (){
9 int d = 5 / 2;
10 }
11

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Jump and Call (Continued)
 In the assembly language, there are 2 groups of
instructions that make the CPU execute an
instruction other than the next instruction. The
instructions are:
 Jump: used for making loops and checking
condition
 Call: used for making function calls

 CALL DELAY – delay();


 CALL FUNC1 – func1();

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Jump
 Jump changes the Program Counter (PC)
and causes the CPU to execute an instruction
other than the next instruction.

 The processors manipulates/updates the PC


to change the sequence of a program

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Jump
There are 2 kinds of Jump
 Unconditional Jump: When CPU executes an
unconditional jump, it jumps unconditionally (without
checking any condition) to the target location.
 Example: RJMP and JMP instructions
 Conditional Jump: When CPU executes a conditional
jump, it checks a condition, if the condition is true then it
jumps to the target location; otherwise, it executes the next
instruction.

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Unconditional Jump in AVR
 There are 3 unconditional jump
instructions in AVR: RJMP,
JMP, and IJMP Code

 We label the location where we 1 LDI R16, 0


2 LDI R17, 2
want to jump, using a unique 3 L1:
L1: ADD R16, R17
name, followed by ‘:’ 4 L1
RJMP L1
5 SUB R10,R15
 Then, in front of the jump
instruction we mention the
name of the label.
 This causes the CPU to jump to
the location we have labeled,
instead of executing the next
instruction.
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Ways of specifying the jump target
 There are 3 ways to provide the jump address:
 PC = operand - JMP L1
 PC = PC + operand - RJMP addrLabel
 PC = Z register - IJMP

 Operand provides the address of the location to


jump to.

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
JMP
 JMP PC = operand
1001 010X XXXX 110X XXXX XXXX XXXX XXXX
 10 bit opcode (bits in red) and a 22 bit operand (x)
 2^22 locations – the entire program ROM
 Can jump anywhere in the program ROM
 Example:

 910014010000000C1100
0 0000
0 0000
0 0000
6 0110

 Operand = 0000000000000000000110

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
JMP

Address Code
 In JMP, the operand, PC: 0002
0001
0000
0007
contains the address of 0000 .ORG 0

the destination 0000 LDI R16, 15


Machine code:
 When an JMP is 0001 LDI R17, 5

executed: 940C 0006


0006 0002 JMP LBL_NAME

 PC is loaded with opCode operand 0004 LDI R18, 4

the operand 0005 ADD R18, R17

value 0006
0006 LBL_NAME:
Machine code: 0006 ADD R16,R17

940C 0006
0006 0007 JMP LBL_NAME
opCode operand 0009

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
RJMP (Relative jump)
 RJMP PC = PC + operand
 2^12 – 4096 locations
1100 XXXX XXXX XXXX

 Example: 1100 0000 0000 0110

 Operand = 000000000110
 PC = PC + 000000000110

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
IJMP (Indirect jump)
 IJMP PC = Z register
1001 0100 0000 1001

 The instruction has no operand.


 the Program counter is loaded with
the contents of Z register.
 For example, if Z points to location

100, by executing IJMP, the CPU


jumps to location 100.
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
RJMP
 When RJMP is
executed:
PC: 0007
0003
0006
0002
0001
0000
The operand
Address Code

+0
+F 0000 .ORG 0
will be added 0005 0000 LDI R16, 15

to the Machine code: 0001 LDI R17, 5


002
C002 0002 RJMP LBL_NAME
current value opCode operand 0003 LDI R18, 4

of PC 0004 ADD R18, R17


0005
0005 LBL_NAME:
Machine code: 0005 ADD R16,R17
CFFE
FFE 0006 RJMP LBL_NAME
opCode operand 0007

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Conditional Jump in AVR
SREG: I T H S V N Z C
 The conditional jump instructions in AVR are as follows:
Instruction Abbreviation of Comment
BREQ lbl Branch if Equal Jump to location lbl if Z = 1,
BRNE lbl Branch if Not Equal Jump if Z = 0, to location lbl
BRCS lbl Branch if Carry Set Jump to location lbl, if C = 1
BRLO lbl Branch if Lower
BRCC lbl Branch if Carry Cleared Jump to location lbl, if C = 0
BRSH lbl Branch if Same or Higher
BRMI lbl Branch if Minus Jump to location lbl, if N = 1
BRPL lbl Branch if Plus Jump if N = 0
BRGE lbl Branch if Greater or Equal Jump if S = 0
BRLTlbl Branch if Less Than Jump if S = 1
BRHS lbl Branch if Half Carry Set If H = 1 then jump to lbl
BRHC lbl Branch if Half Carry Cleared if H = 0 then jump to lbl
BRTS Branch if T flag Set If T = 1 then jump to lbl
BRTC Branch if T flag Cleared If T = 0 then jump to lbl
BRIS Branch if I flag set If I = 1 then jump to lbl
BRIC Branch if I flag cleared If I = 0 then jump to lbl
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Usages of Conditional jump
 Check for a condition
 Loop – repeating N times

 Let’s say we need to compare two values A and B


 A = B => A-B = 0 => Z = 1, BREQ, BRNE
 A < B => A-B < 0 => C = 1, BRCS
 A > B => B-A < 0 => C = 1, when B < A
 This can be done by subtracting B from A: A - B

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Conditions
 When b is subtracted from a: (A-B)
 The result is zero, when a is equal to b a
 Carry will be set when a < b -b

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 1
 Write a program to check if R20 is equal to R21.
If true then increment R22, else do nothing.

 Solution: if (R20 == R21)


No

SUB R20,R21 ;Z will be set if R20 == R21


BRNE NEXT ;if Not Equal jump to next Yes

INC R22
NEXT: increment R22

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 2
 Write a program to check if R26 < R24 then R22
increases, else do nothing.

 Solution: if (R26 < R24)


No

SUB R26,R24 ;Z will be set if R20 == R21


Yes
BRCC L1 ;if Carry cleared jump to L1
INC R22
increment R22
L1:

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 3
 Write a program that if R26 >= R24 then R22
increases.

 Solution: if (R26 < R24)


No

SUB R26,R24 ;Z will be set if R20 == R21


Yes
BRCS L1 ;if Carry set jump to L1
INC R22
increment R22
L1:

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 4: IF and ELSE
R17 = 5;
R17 = 5
if (R20 > R21)
R22++;
else
No
R22--; if (R20 > R21)
R17 ++;
Yes

increment R22
LDI R17,5
SUB R21,R20
Decrement R22
BRCS IF_LABEL
DEC R22
JMP NEXT
IF_LABEL:
increment R17
INC R22
NEXT:
INC R17

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Loop
 Write a program that executes the instruction
“ADD R30,R31” 9 times.
R16 = 9

 Solution:
.ORG 00 ADD R30,R31

LDI R16,9 ;R16 = 9


L1: ADD R30,R31
R16 = R16 - 1
DEC R16 ;R16 = R16 - 1
BRNE L1 ;if Z = 0
L2: RJMP L2 ;Wait here forever
Yes
if (R16 > 0)

No

END

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Loop
 Write a program that calculates the result of
9+8+7+…+1
R16 = 9
R17 = 0
 Solution:
.ORG 00 R17 = R17 + R16

LDI R16, 9 ;R16 = 9


LDI R17, 0 ;R17 = 0
R16 = R16 - 1
L1: ADD R17,R16 ;R17 = R17 + R16
DEC R16 ;R16 = R16 - 1
BRNE L1 ;if Z = 0
Yes
L2: RJMP L2 ;Wait here forever if (R16 > 0)

9+8+7+6+....+1 No

END

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Loop
 Write a program that calculates the result of
20+19+18+17+…+1
R16 = 20
R17 = 0
 Solution:
.ORG 00 R17 = R17 + R16

LDI R16, 20 ;R16 = 20


LDI R17, 0 ;R17 = 0
R16 = R16 - 1
L1: ADD R17,R16 ;R17 = R17 + R16
DEC R16 ;R16 = R16 - 1
BRNE L1 ;if Z = 0
Yes
L2: RJMP L2 ;Wait here forever if (R16 > 0)

No

END

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Loop
for (init; condition; calculation) init

{
do something
Do something

} calculation

For (i=9; I >=0; i--) Yes


Condition

{ No

ADD R30,R31 END

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Loop
 Write a program that calculates 1+3+5+…+27
 Solution:
R20 = 0
R16 = 1

LDI R20,0
LDI R16,1 R20 = R20 + R16

L1:ADD R20,R16
R16 = R16 + 2
INC R16
INC R16
LDI R17,27 ;R17 = 27
Yes
R16 <= 27
SUB R17,R16
BRCC L1 ;if R16 <= 27 jump L1 No

END

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Loop
 Write a program that calculates 0+2+4+…+28
 Solution:
R20 = 0
R16 = 1

LDI R20,0
LDI R16,0 R20 = R20 + R16

L1:ADD R20,R16
R16 = R16 + 2
INC R16
INC R16
LDI R17,28 ;R17 = 27
Yes
R16 <= 27
SUB R17,R16
BRCC L1 ;if R16 <= 27 jump L1 No

END

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Call Topics
 Stack, Push and Pop
 Calling a function

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Initializing the Stack Pointer

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Stack

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Stack Bottom

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Stack Pointer

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Stack
 PUSH Rr  POP Rd
[SP] = Rr
SP = SP + 1
SP = SP - 1
Rd = [SP]

SP

Stack

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Stack

Address Code
ORG 0
0000 LDI R16,HIGH(RAMEND)
0001 OUT SPH,R16
R20: $10
$00 R22: $30
$00
0002 LDI R16,LOW(RAMEND)
R21: $00
$20 R0: $00 0003 OUT SPL,R16
0004 LDI R20,0x10
0005 LDI R21, 0x20

0006 LDI R22,0x30


SP 0000 0007 PUSH $10
R20

0008 PUSH $20


R21
0009 PUSH $30
R22

000A POP R21


000B POP R0

000C POP R20

000D L1: RJMP L1

Memory
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Calling a Function

 To execute a call: Address Code


0000 LDI R16,HIGH(RAMEND)
 Address of the next
0001 OUT SPH,R16
instruction is saved 0002 LDI R16,LOW(RAMEND)
 PC is loaded with 0003 OUT SPL,R16
the appropriate 0004 LDI R20,15

value Machine code: 0005 LDI R21,5


940E 000A
000A 0006
0006 CALL FUNC_NAME
opCode operand 00 08
0008 INC R20
0009 L1: RJMP L1
000A FUNC_NAME:
000A ADD R20,R21
SP 000B SUBI R20,3
PC: 000C
000B
0006
0005
0004
0009
0008 000C RET
000D
Stack
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Time delay

40 PIN DIP
(XCK/T0) PB0 1 40 PA0 (ADC0)
(T1) PB1 2 39 PA1 (ADC1)
(INT2/AIN0) PB2 3 38 PA2 (ADC2)
(OC0/AIN1) PB3 4 MEGA32 37 PA3 (ADC3)
(SS) PB4 5 36 PA4 (ADC4)
(MOSI) PB5 6 35 PA5 (ADC5)
(MISO) PB6 7 34 PA6 (ADC6)
(SCK) PB7 8 33 PA7 (ADC7)
RESET 9 32 AREF RAM EEPROM Timers
VCC 10 31 AGND
GND 11 30 AVCC PROGRAM
XTAL2 12 29 PC7 (TOSC2) Flash ROM
XTAL1 13 28 PC6 (TOSC1)
Program Data
(RXD) PD0 14 27 PC5 (TDI)
Bus Bus
(TXD) PD1 15 26 PC4 (TDO) CPU
(INT0) PD2 16 25 PC3 (TMS)
(INT1) PD3 17 24 PC2 (TCK)
(OC1B) PD4 18 23 PC1 (SDA)
(OC1A) PD5 19 22 PC0 (SCL)
(ICP) PD6 20 21 PD7 (OC2)
Interrupt Other
OSC Ports
Unit Peripherals

I/O
PINS
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Software Delay

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Software Delay

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Time Delay

Generate a delay of 500 ms.

Time Delay = number of clock cycles x time period of the clock cycle

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Time delay
machine cycle
LDI R16, 19 1
LDI R20, 95 1
LDI R21, 5 1
ADD R16, R20 1
ADD R16, R21 1
5

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Time delay

machine cycle
LDI R16, 100
1
AGAIN: ADD R17,R16 *100
1
*100
DEC R16 1
BRNE AGAIN 1/2 *100

Branch penalty

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Time delay

machine cycle
LDI R16, 50
1
AGAIN: NOP 1 *50
NOP 1 *50

DEC R16 1 *50

BRNE AGAIN 1/2 *50

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Time delay

machine cycle
LDI R17, 20
1
L1: LDI R16, 50
1 *20
L2: NOP
1 *20 * 50
NOP 1 *20 * 50
DEC R16 1 *20 * 50
BRNE L2 1/2 *20 * 50
DEC R17 1 *20
BRNE L1 1/2 *20

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example with 2 loops

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example with 3 loops

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Address Code
0000 .ORG 0
0000 LDS R16, 15
0001 LDS R17, 5
0002 RJMP L1
0003 LDS R18, 4
0004 ADD R18, R17
0005 STS 0x90,R18
0006 L1: SUB R16,R17
0007 STS 0x90,R16
0008 RJMP LBL_NAME
0009
000A

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy