How function pointer written in C is mapped to assembly

We will take the simplest microcontroller to understand this because we will be having less instructions present in it. We here are taking atmega8 microcontroller instruction set to understand this.

Suppose we are having the following C code in which we will be using a function pointer to call a C function.


void (*ptr)(void);  

void fn(void){   //suppose this is present at the address 0x34 in program memory
 asm("nop");
}

int main(void) {
 ptr = fn;  
 ptr(); 
}
The first line ptr=fn is mapped to the following assembly instructions

ldi R24,$34 ;lower 8 bits addrress
ldi R25,$00 ;higher 8 bits address
sts $0063,R25 ;storing higher bits first
sts $0062,R24 ;storing lower bits then


The call ptr() is mapped to the following assembly instructions

lds R30,$0062 ;loading the lower bits
lds R31,$0063 ;loading the higher bits

icall ; icall will make the PC points to the address present in the Z register

Comments

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig