stack changes when calling function in c

C code:

Assembler code:


The important extraction that can make usknow about stack is:

first of all, in function main, beforecalling function f, the instruction subtracts esp by 8 because there are 2parameters need to be passed into function f. when the two parameters arepassed into stack function f is called which will push the return address intostack.

Then in function f, the first thing need tobe done is save the previous stack base pointer by pushing it into stack andthen set the current stack pointer as base stack pointer of current function. Thefunction f uses epb to retrieve parameters passed into function f in the main.

The code declares two variables in the functionf which locates in the stack. We can see that the esp is subtracted by 16 toallocate a block of stack memory for storing variables. Here we just allocatetwo variables but the instruction allocates 16 bytes for us. It seems that 16bytes are the minimum size of stack memory that can be allocated in the stack.

Before return from function f the savedprevious stack base pointer need to be set back by poping the stack(popl %ebp).Last instruction ret in the function f will pop the return address saved in thestack and jump to that address.

So the major structure of the stack for theabove program is as follows:


From the code we can use that ebp is usedto retrieve variable in the stack. It severs as a base pointer as its nameindicates.

In a nutshell, when a function is called in c, first of all, the parameters are passed into stack, then function return address, previous stack frame address, and other variables declared inside the function called.

### STM32F LwIP DNS Implementation Example Tutorial For implementing Domain Name System (DNS) functionality using Lightweight IP (LwIP) on an STM32F series microcontroller, the setup involves configuring both hardware and software components to ensure proper network communication. The following sections provide a detailed guide. #### Configuration of Network Interface The first aspect is setting up the Ethernet interface or any other suitable network interface that connects the STM32 device with external networks. This includes initializing the PHY chip connected through RMII/MII interfaces as well as configuring MAC settings within the STM32 HAL library[^1]. ```c // Initialize ETH peripheral void MX_ETH_Init(void) { /* USER CODE BEGIN ETH_Init 0 */ /* USER CODE END ETH_Init 0 */ /* USER CODE BEGIN ETH_Init 1 */ /* USER CODE END ETH_Init 1 */ heth.Instance = ETH; heth.Init.MACAddr = {0x00, 0x80, 0xE1, 0x00, 0x00, 0x00}; heth.Init.MediaInterface = ETH_MEDIA_INTERFACE_RMII; // Additional configurations... } ``` #### Initializing LwIP Stack After ensuring physical connectivity, initialize the LwIP stack by calling `tcpip_init()`. It's important here not only to start TCP/IP processes but also register callback functions which handle incoming packets from different protocols including UDP used for DNS queries. ```c err_t lwip_init(void){ err_t ret_err; ip_addr_t local_ipaddr; ip_addr_set(&local_ipaddr, &my_local_ip); netif_add(&gnetif, &local_ipaddr, IP_ADDR_ANY, IP_ADDR_ANY, NULL, ethernetif_init, tcpip_input); netif_set_default(&gnetif); netif_set_up(&gnetif); ret_err = dhcp_start(&gnetif); return ret_err; } ``` #### Implementing DNS Client Functionality To implement DNS client features specifically, utilize APIs provided by LwIP such as `dns_gethostbyname()` function. Before invoking this API though, make sure DNS server addresses are properly configured either statically during initialization phase or dynamically via DHCP service. ```c struct dns_data { struct ip_addr *addr; }; static void my_dns_found(const char *name, struct ip_addr *ipaddr, void *callback_arg) { struct dns_data* data = (struct dns_data*)callback_arg; if(ipaddr != NULL && data->addr != NULL){ memcpy(data->addr, ipaddr,sizeof(struct ip_addr)); } } int resolve_domain_name(char *hostname, struct ip_addr **resolved_ip) { int result=-1; struct dns_data data={NULL}; data.addr=*resolved_ip; result=dns_gethostbyname(hostname,*resolved_ip,&data.my_dns_found,(void*)&data); return result; } ``` --related questions-- 1. How does one configure static IP address assignment instead of dynamic allocation? 2. What modifications would be necessary when adapting these examples for WiFi modules rather than wired Ethernet connections? 3. Can you explain how to integrate mbedTLS into this system for secure socket communications over TLS/SSL? 4. Is it possible to use multiple DNS servers simultaneously in case one becomes unavailable? If so, what changes need to occur in code structure presented above? 5. Are there specific considerations regarding power management while maintaining active internet connection states on battery-powered devices like those based around STM32 MCUs?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值