CALL Instructions and Stack in AVR Microcontroller
Last Updated :
24 Apr, 2023
Introduction :
The AVR microcontroller is a type of microcontroller developed by Atmel Corporation. It is widely used in various embedded systems due to its low power consumption and high performance. In this context, I will discuss the introduction to CALL instructions and the stack in AVR microcontrollers.
CALL instructions are used to call a subroutine or a function in AVR microcontrollers. When the CALL instruction is executed, the current address of the program counter (PC) is pushed onto the stack, and the PC is loaded with the address of the subroutine. Once the subroutine is completed, the RET instruction is executed, which pops the saved address from the stack and loads it back into the PC. This allows the program to resume execution from the point it left off.
The stack is a special region of memory used to store temporary data in AVR microcontrollers. When a subroutine is called, the current values of the registers are saved onto the stack, and the subroutine can use the registers without interfering with the main program. When the subroutine is completed, the saved values are restored, and the program resumes execution from where it left off.
The stack in AVR microcontrollers grows downwards from a high memory address to a lower memory address. The stack pointer (SP) is a register that points to the current top of the stack. The SP is initialized at the beginning of the program, and as the stack grows, the SP is decremented. When the stack is popped, the SP is incremented.
It is essential to manage the stack properly in AVR microcontrollers to prevent stack overflow or underflow. Stack overflow occurs when the stack grows beyond its allocated size, overwriting other critical data. Stack underflow occurs when there are not enough items on the stack to be popped, resulting in undefined behavior.
CALL is a control transfer instruction that is used to call a particular subroutine. A subroutine is a block of instructions that need to be performed frequently. In AVR, there are 4 instructions for the call subroutine as following.
- CALL (call subroutine)
- RCALL (relative call subroutine)
- ICALL (indirect call to Z)
- EICALL (extended indirect call to Z)
CALL : In this 4-byte instruction, 10 bits are used for the opcode and the other 22 bits are used for the address of the target subroutine just as in the JMP instruction. In this, 4M address space of 000000-$3FFFFF for AVR and can be used to call subroutines within the given range of address. To make sure that the AVR knows where to come back after the execution of the subroutine, the microcontroller automatically saves the address of the instruction just below the CALL instruction on the stack. After finishing the execution of the subroutine, the RET instruction transfers control back to the caller. Hence, every subroutine has a RET instruction at the end. Stack : Stack is the part in the RAM of CPU to store information temporarily. The CPU needs this storage because there is only a limited number of registers. The register used to access the stack is called the stack pointer (SP) register. In I/O memory space, there are 2 registers named SPL (the low byte of SP) and SPH (the high byte of SP). The SP is implemented by these 2 registers. In AVRs with more than 256 bytes of memory have two 8-bit registers. On the other hand, if the memory is less than 256 bytes, SP is made up of only SPL, as an 8-bit register can only address 256 bytes of memory. The storing of the CPU information on the stack is called the PUSH operation, and the loading of the stack contents back into the CPU is known as POP operation. Pushing onto the stack : The stack pointer (SP) points to the top of the stack. As we push the data onto the stack, the data is saved where the SP is pointing to and the SP is decremented by one. To push a register onto a stack, we use PUSH instruction.
PUSH Rr;
Rr can be any general-purpose register (R0 - R31)
Popping from the stack : Popping the contents of the stack back into the register is the opposite function of pushing. When the POP instruction is executed, the SP is incremented by one and the top location of the stack is copied back to the register. This means that the stack is LIFO ( Last In First Out). To retrieve back the data from the stack, we use POP instruction.
POP Rr;
Rr can be any general-purpose register (R0 - R31)
Initializing stack pointers : Different AVRs have different amounts of RAM. In the AVR assembler, RAMEND specifies the address of the last RAM location. So, if we want to initialize the SP so that it points to the last memory location, we can simply load RAMEND into the SP. Notice that SP is made up of 2 registers, SPH and SPL. So, we load the high byte of RAMEND into SPH and the low byte of RAMEND into SPL. CALL instruction, RET instruction and the role of stack : When the CALL instruction is executed, the address of the instruction below the CALL instruction is pushed onto the stack. When the execution of that subroutine is finished and RET is executed, the address of the instruction below the CALL instruction is loaded in the program counter and it is executed.
Uses of CALL Instructions and Stack in AVR Microcontroller :
CALL instructions and the stack play a vital role in AVR microcontrollers, and they have several important uses:
- Function calls: CALL instructions are used to call subroutines or functions in AVR microcontrollers. This allows programs to be modular and organized, making it easier to write and maintain complex code.
- Interrupt handling: Interrupts are used to handle events that occur asynchronously to the program's normal execution. When an interrupt occurs, the AVR microcontroller saves the current program state on the stack and jumps to the interrupt service routine. After the ISR is completed, the AVR microcontroller restores the saved program state from the stack and resumes the normal program execution.
- Recursive functions: Recursive functions are functions that call themselves. The stack is used to store the program state of each recursive call, allowing the AVR microcontroller to return to the previous function call after the current one completes.
- Local variables: When a function is called, local variables are stored on the stack. This allows the function to use these variables without affecting other parts of the program.
- Parameter passing: When a function is called, parameters are passed to the function through registers or the stack. The stack is used to store the parameters, and the function can access them from the stack.
- Context switching: In some embedded systems, it is necessary to switch between different tasks or contexts. The stack is used to store the program state of each task, allowing the AVR microcontroller to switch between them efficiently.
Issues in CALL Instructions and Stack in AVR Microcontroller :
There are several issues that can arise if they are not used or managed properly. Some of the most common issues are:
- Stack overflow: If the stack grows beyond its allocated size, it can overwrite other critical data, causing unpredictable behavior or crashing the program.
- Stack underflow: If there are not enough items on the stack to be popped, it can cause undefined behavior or even crash the program.
- Interrupt latency: When an interrupt occurs, the AVR microcontroller saves the current program state on the stack before jumping to the ISR. If the ISR takes too long to execute, it can increase the interrupt latency, causing other interrupts to be missed or delayed.
- Recursive function depth: When a function calls itself recursively, it can quickly consume the available stack space, leading to stack overflow or unpredictable behavior.
- Stack pointer corruption: If the stack pointer is accidentally overwritten or corrupted, it can cause stack underflow or overflow, leading to program crashes or undefined behavior.
- Interrupt nesting: If interrupts occur while an ISR is already executing, it can lead to interrupt nesting, which can cause unexpected behavior or crashes if not properly managed.
Similar Reads
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Half Wave Rectifier A Half-wave rectifier is an electronic device that is used to convert Alternating current (AC) to Direct current (DC). A half-wave rectifier allows either a positive or negative half-cycle of AC to pass and blocks the other half-cycle. Half-wave rectifier selectively allows only one half-cycle of th
15 min read
Computer Organization and Architecture Tutorial In this Computer Organization and Architecture Tutorial, youâll learn all the basic to advanced concepts like pipelining, microprogrammed control, computer architecture, instruction design, and format. Computer Organization and Architecture is used to design computer systems. Computer architecture i
5 min read
Architecture of 8085 microprocessor A microprocessor is fabricated on a single integrated circuit (IC) or chip that is used as a central processing unit (CPU).The 8085 microprocessor is an 8-bit microprocessor that was developed by Intel in the mid-1970s. It was widely used in the early days of personal computing and was a popular cho
11 min read
Direct Memory Access (DMA) Controller in Computer Architecture In modern computer systems, transferring data between input/output devices and memory can be a slow process if the CPU is required to manage every step. To address this, a Direct Memory Access (DMA) Controller is utilized. A Direct Memory Access (DMA) Controller solves this by allowing I/O devices t
5 min read
Bridge Rectifier In the world of electronics, the bridge rectifier is a component that plays a role, in converting alternating current (AC) to direct current (DC). This article explores the concept of the bridge including how it's constructed how it operates, its benefits and drawbacks different types available, imp
11 min read
Number System and Base Conversions Electronic and digital systems use various number systems such as Decimal, Binary, Hexadecimal and Octal, which are essential in computing. Binary (base-2) is the foundation of digital systems.Hexadecimal (base-16) and Octal (base-8) are commonly used to simplify the representation of binary data. T
9 min read
8051 MicroController Architecture For many years, the 8051 microcontroller architecture is vital and important component in embedded systems design due to its common use. Its strong architecture and multiple uses in a variety of industries from consumer electronics to industrial automation are the main causes of its appeal.What is 8
6 min read
Boolean Algebra Boolean algebra is a type of algebra that is created by operating the binary system. In the year 1854, George Boole, an English mathematician, proposed this algebra. This is a variant of Aristotle's propositional logic that uses the symbols 0 and 1, or True and False. Boolean algebra is concerned wi
14 min read
Programmable peripheral interface 8255 PPI 8255 is a general purpose programmable I/O device designed to interface the CPU with its outside world such as ADC, DAC, keyboard etc. We can program it according to the given condition. It can be used with almost any microprocessor. It consists of three 8-bit bidirectional I/O ports i.e. PORT A
4 min read