MPS - Ch03 - Jump - Call
MPS - Ch03 - Jump - Call
Chapter 3
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Topics
◼ Introduction to jump and call
◼ Jump
◼ Call
◼ Stack
◼ Calling a function
◼ Time Delay
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
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;
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
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 Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Jump and Call (Continued)
◼ Example 1: Not executing the
next instruction, because of
condition. 1 void main ()
executed. 5 if (a == 8)
6 c = 6;
7 else
8 c = 7;
9 c = a + 3;
}
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Jump and Call (Continued)
◼ Example 2: In this example
the next instruction will not
be executed because of 1 void main ()
loop. 2 {
3 int a, c = 0;
◼ In the following example, 4 for(a = 2; a < 4; a++)
follows:
6 a = c + 2;
7 }
◼ Line 4 8
9
◼ Line 5
◼ Again, line 4
◼ Again line 5
◼ Line 6
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Jump and Call (Continued)
◼ Example 3: Not executing
the next instruction, because
of calling a function. 1
Code
void func1 ();
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
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 loop and condition
◼ Call: used for making function calls
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Topics
◼ Introduction to jump and call
◼ Jump
◼ Call
◼ Stack
◼ Calling a function/subroutine
◼ Time Delay
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Branch (or Program control) Instructions
▪ Jump (unconditional jump/branch)
jmp label1
Jump to label1 – no information about where we jumped from is saved. This
is a one way trip
◼ Branch (conditional jump)
breq label2 ; Branch if equal to location label2
brlo label3 ; Branch if lower to location label3
If the Branch test fails – the next line of code is executed
If the Branch test is successful, the program jumps to the location specified
▪ Call, Return
rcall mysub
ret
Call subroutine mysub. The program saves information about where it currently
is executing and then jumps to the code at mysub. When the subroutine is
finished it calls ret, which then returns to where the rcall was made.
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Jump
◼ Jump changes the Program Counter (PC) and causes
the CPU to execute an instruction other than the next
instruction.
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
JMP
◼ JMP PC = operand = 22-bit const
Opcode = 10010101110
◼ Example:
1001 0100 0000 1100 0000 0000 0000 0110
◼ Operand = 0000000000000000000110
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Direct Program Addressing, JMP and CALL
Syntax: Operands:
JMP k 0 ≤ k ≤ FLASHEND < 4M = 222
(ATMega32: FLASHEND = 0x3FFF)
31 Machine code 0
Address Code
◼ In JMP, the operand, PC: 0002
0001
0000
0007
contains the address of 0000 .ORG 0
value 0006
0006 LBL_NAME:
Machine code: 0006 ADD R16,R17
940C 0006
0006 0007 JMP LBL_NAME
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
RJMP (Relative jump)
◼ RJMP PC = PC + operand
1100 XXXX XXXX XXXX
◼ Operand = 000000000110
◼ PC = PC + 000000000110
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Relative Program Addressing, RJMP and RCALL
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
RJMP
◼ When RJMP is
executed:
◼ The operand
PC: 0003
0007
0006
0002
0001
0000 Address Code
+0
+F 0000 .ORG 0
will be added 0005 0000 LDI R16, 15
value of PC 002
C002 0002 RJMP LBL_NAME
opCode operand 0003 LDI R18, 4
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 Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
IJMP (Indirect jump)
◼ IJMP PC = Z register
1001 0100 0000 1001
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Conditional Jump in AVR
SREG: I T H S V N Z C
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Usages of Conditional jump
◼ Conditions
◼ Loop
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Conditions
◼ When b is subtracted from a:
◼ The result is zero, when a is equal to b a
◼ Carry will be set when a < b -b
◼ Compare:
◼ CP Rd,Rr ; 0 ≤ d, r ≤ 31 → Rd – Rr sets flags
◼ CPC Rd,Rr ; Rd – Rr – C sets flags
◼ CPI Rd,K ; 0 ≤ K ≤ 255 → Rd – K sets flags
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Example 1
◼ Write a program that if R20 is equal to R21 then
R22 increases.
R20 is changed, R21 is unchanged
◼ Solution 1: if (R20 == R21)
No
INC R22
NEXT: increment R22
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
CP Instruction
◼ CP (Compare) instruction performs a subtraction
Syntax: CP destination, source
Computes: destination – source
◼ Destination operand is NOT modified
◼ Any general purpose register (Rn) can be
Destination or Source .
◼ Flags: H, C, N , V, S , and Z are affected
◼ Examples: assume R2 = 5, R1 = 10, and R0 = 5
CP R1, R0 ; C = 0, S = 0, Z = 0
CP R2, R1 ; C = 1, S = 1, Z = 0
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Unsigned Comparison
◼ CP can perform unsigned and signed comparisons
◼ The destination and source operands can be unsigned or signed
◼ For unsigned comparison, we examine Z and C flags
Unsigned Comparison C Z Branches are executed
unsigned destination < unsigned source 1 0 BRLO, BRCS, BRNE, BRMI
unsigned destination > unsigned source 0 0 BRSH, BRCC, BRNE, BRPL
destination = source 0 1 BRSH, BRCC, BREQ, BRPL
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Signed Comparison
◼ For signed comparison, we examine S, V, and Z
Signed Comparison Flags Branches are excecuted
signed destination < signed source S≠V BRLT, BRNE
signed destination > signed source S = V, Z = 0 BRGE, BRNE
destination = source Z=1 BRGE, BREQ
INC R22
L1: increment R22
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Example 3
◼ Write a program that if R26 >= R24 then R22
increases.
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Example 4: IF and ELSE
R17 = 5
R17 = 5;
if (R20 > R21)
R22++;
else if (R20 > R21)
No
R22--;
R17 ++; Yes
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Loop
◼ Write a program that executes the instruction
“ADD R30,R31” 9 times.
R16 = 9
◼ Solution:
.ORG 00 ADD R30,R31
No
END
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Loop
◼ Write a program that calculates the result of
9+8+7+…+1
R16 = 9
R17 = 0
◼ Solution:
.ORG 00 R17 = R17 + R16
No
END
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Loop
◼ Write a program that calculates the result of
20+19+18+17+…+1
R16 = 20
R17 = 0
◼ Solution:
R17 = R17 + R16
.ORG 00
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
L2: RJMP L2 ;Wait here forever Yes
if (R16 > 0)
No
END
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Loop
for (init; condition; calculation) init
{
do something
Do something
} calculation
Yes
Condition
No
END
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
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
LDI R17,2 R16 = R16 + 2
END
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Topics
◼ Introduction to jump and call
◼ Jump
◼ Call
◼ Stack
◼ Calling a function/subroutine
◼ Time Delay
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Call Topics
◼ Stack, Push and Pop
◼ Calling a function/subroutine
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Plate and stack analogy to computer stack operation
❑ The stack is an area of memory
❑ The stack pointer (SP) is the address of the last value pushed onto the stack. (In
AVR MCUs, SP = the address of the available location for next PUSH
operation) Usually, the stack is used for storing data when subroutines are called.
❑ The stack is a last-in-first-out, i.e., LIFO structure so the last thing stored in the
stack is the first thing retrieved.
❑ A mechanical analogy will help you learn how the stack operates. One common
mechanical stack is the plate rack used in some cafeterias:
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Stack
◼ PUSH Rr ◼ POP Rd
[SP] = Rr SP = SP + 1
SP = SP - 1 Rd = [SP]
SP
Stack
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Stack
◼ The stack is implemented in SRAM, and SP always points to the
top of the stack and decrements with a PUSH instruction.
◼ Thus, to utilize the stack using the PUSH and POP instructions,
one must initialize SP properly.
◼ On AVRs, SP is initialized to the end of SRAM
◼ Other AVR documentation suggest that at reset, SP is initialized to
0x00, which is where the 32 general purpose registers
start—see the AVR memory map, so programmer has to
initialize SP
; Point the stack to end of SRAM.
ldi r23,LOW(RAMEND) ; Load LSB of last SRAM address in r23
out spl,r23
ldi r23,HIGH(RAMEND) ; Load MSB of last SRAM address in r23
out sph,r23
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Stack
SP is located in
I/O space so we
use IN and OUT
instructions RAMEND is defined in “m32def.inc”
.include “m32def.inc”
; Point the stack to end of SRAM.
ldi r23, LOW(RAMEND)
LOW RAMEND ; Load LSB of last SRAM address in r23
out spl,r23
out
ldi r23,HIGH(RAMEND
HIGH RAMEND ) ; Load MSB of last SRAM address in r23
out sph,r23
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Stack
Address Code
ORG 0
000B POP R0
Memory
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Calling a function/subroutine
❑ Subroutine calls are a special type of branch where we return to one instruction below the
calling instruction.
- Provision must be made to save the return address, since it cannot be written into ROM.
940E 000A
000A 0006
0006 CALL FUNC_NAME
opCode operand 0008
00 08 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 Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Why subroutines?
◼ Divide and Conquer – Allow you to focus on one small
“chunk” of the problem at a time.
◼ Code Organization – Gives the code organization and
structure. A small step into the world of object-oriented
programming.
◼ Hierarchical Design – Moves information about the program
at the appropriate level of detail.
◼ Code Readability – Allows others to read and understand the
program in digestible “bites” instead of all at once.
◼ Encapsulation – Insulates the rest of the program from
changes made within a procedure.
◼ Team Development – Helps multiple programmers to work on
the program in parallel; a first step to configuration control.
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Subroutine Call and Return Instructions
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Push/Pop and Call/Ret
◼ PUSH Rn ◼ POP Rn
◼ [SP] Rn ◼ SP SP + 1
◼ SP SP – 1 ◼ Rn [SP]
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
How to send information to and/or from
the calling program
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Ex: larger.asm
; larger.asm RESET:
.include "m32def.inc" LDI temp, low(RAMEND)
RJMP RESET OUT SPL, temp
.def temp =r16 LDI temp, high(RAMEND)
.def a = r17 OUT SPH, temp ; init SP
.def b = r18 main:
larger: LDI a, 0xFF
CP a, b LDI b, 0x46
BRLT return RCALL larger
RET loop:
return: RJMP loop
MOV a, b
RET
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Topics
◼ Introduction to jump and call
◼ Jump
◼ Call
◼ Stack
◼ Calling a function
◼ Time Delay
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
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 Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
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 Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Time delay
machine cycle
LDI R16, 100
1
AGAIN: ADD R17,R16 *100
1
DEC R16 1 *100
Branch penalty
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Time delay
machine cycle
LDI R16, 50
1
AGAIN: NOP 1 *50
NOP 1 *50
DEC R16 1 *50
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
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 Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Example 3-20 (1/2)
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Example 3-20 (2/2)
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Modification on Example 3-20
.include "M32DEF.inc"
.org 0 DELAY_1S:
LDI R20,32
LDI R16,HIGH(RAMEND) L1: LDI R21,200
OUT SPH,R16 L2: LDI R22,5
LDI R16,LOW(RAMEND) ; The modification is need so that
OUT SPL,R16 ; we get an almost 1 sec delay on
; AVR simulator
LDI R16,0X55
L3:
BACK: NOP
COM R16 NOP
OUT PORTB,R16 DEC R22
CALL DELAY_1S BRNE L3
DEC R21
RJMP BACK
BRNE L2
DEC R20
BRNE L1
RET
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.