Wei Dongshan YY Open Course Notes (2) -- assembly, segment, stack, relocation/link address, location-independent?

Source: Internet
Author: User



1. Do you want to learn assembly?
I can only understand a little, but it is not necessary at work. Once used, it is a big problem.

LDR: Load READ memory
LDR r0, [R1]: r1 stores the address value. Read the 4-byte content from this address and store it in R0.

STR: stroe write memory
STR r0, [R1]: r1 stores the address value and stores the 4-byte data in R0 to this address.



The ultimate goal of all compilation and C programs is to read and write an address.


2. Why should a program be divided into code segments, data segments, and BSS segments?
Program commands are read-only and can be classified as one type so that the running time can be placed on devices such as Rom,
Of course, it can also be stored in the memory, but this memory does not need to be written.

Program global variables are readable and writable. They can be classified as one type and stored in memory.

Assume that the program has 1 million global variables whose initial values are 0,
Do you want to store these 1 million 0 variables in the eventually compiled binfile?
Certainly not so silly, don't save!
What should I do?
Classify them as one type, and only record their memory start address and end address.
Clear all the memory before running the program.



3. Role of stack
3.1
Set the stack before calling the C function,
Because some values (such as the return address) must be saved at the beginning of the C function, these values must be stored in the memory,
This memory is agreed to use the SP register to specify the location,
This memory is the stack

3.2 used to save local variables



4. Relocation/link address
When a global variable is used, the memory is read and written according to its address.
This address is closely related to the starting address of the link script.
You can also know:
To access the global variables correctly, it should be on its link address before the program runs.

4.1 When NAND is started, the first 4 K of NAND will be automatically copied to the on-chip SRAM and run
If the program is much larger than 4 K, the code of the first 4 K should completely copy the program to the SDRAM
Q: Where should I copy the large amount of SDRAM?
A: copy the link to it.
Because global variables and so on are accessed by link addresses, this address should have a correct value before running the program.


We can also obtain the bare-board frame:
A. necessary hardware initialization: watchdog, clock, and SDRAM
B. Relocation: Read the program to the link address in the memory on flash.
C. Clear BSS
D. Call the c Function


5. Location-independent code
Assume that the link address of the program is 0x30000000, which is on SDRAM,
One power-on operation starts from 0.
Q: Why can the code starting with address 0 be correctly executed?
A: They are written using location-independent code.
A. Do not access global variables/static variables
B. Use the relative redirect command B and Bl during redirect.

Test1:
B Test2 // how do I achieve redirection?
Current B value + (Test2-test1)
....
....
Test2:


For more information, see <full embedded Linux application development manual> corner stone and <3.1.2 arm-Linux-LD Options>

Wei Dongshan YY Open Course Notes (2) -- assembly, segment, stack, relocation/link address, location-independent?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.