s3c2440 bare metal-bss clearing principle and implementation

Publisher:工号待定Latest update time:2023-08-09 Source: elecfansKeywords:s3c2440 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Introduction of bss clearing (why we need to clear bss) Let’s give an example first:

#include "s3c2440_soc.h"

#include "uart.h"


char g_Char = 'A'; //.data

char g_Char3 = 'a';

const char g_Char2 = 'B'; //.rodata

int g_A = 0; //bss

int g_B; //bss


int main(void)

{

uart0_init();


puts("nrg_A = ");

printHex(g_A);

puts("nr");


while (1)

{

putchar(g_Char);

g_Char++; /* When nor starts, this code is invalid and will be valid after relocating to the baseaddr of sdram*/


putchar(g_Char3);

g_Char3++;

delay(1000000);

}

return 0;

}

We burned the program in and printed g_A, but found that the value of g_A was not 0, but a random value. why?


At this time, we have completed the relocation and copied the code to sdram, and then the address immediately following sdram is the base address of .bss. At this time, the memory in the bss segment has not been processed in any way, so it is random.


Then after relocating the code, we need to clear the data in the .bss section on the sdram, because we know that bss is a global variable that is not initialized and has an initial value of 0.


2.How to clear bss

Of course, you only need to write all 0s to the bss section. We write the link script as follows:


 SECTIONS

 {

     . = 0x30000000;


     . = ALIGN(4);

     .text      :

     {

       *(.text)

     }


     . = ALIGN(4);

     .rodata : { *(.rodata) }


     . = ALIGN(4);

     .data : { *(.data) }


     . = ALIGN(4);

     __bss_start = .;

     .bss : { *(.bss) *(.COMMON) }

     _end = .;

 }

Then write start.s, and the code to clear the bss section is as follows:


/* Clear BSS segment*/

ldr r1, =__bss_start

ldr r2, =_end

mov r3, #0

clean:

strb r3, [r1]

add r1, r1, #1

cmp r1, r2

ble clean


bl main

halt:

b stop

We burned the program again and printed g_A, but found that the value of g_A was 0. Essentially, it is to clear the relocated bss segment data to 0.


Keywords:s3c2440 Reference address:s3c2440 bare metal-bss clearing principle and implementation

Previous article:s3c2440 bare metal - code relocation, bss clearing optimization and position-independent code
Next article:s3c2440 bare metal-code relocation (2. Programming to implement code relocation)

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号