GNU compilation experience

Source: Internet
Author: User
1. The mov PC # 0x command can be used to redirect code. The number of jumps can be 32 bits, but it must be an even number of eight bits.
2. When using. Word in Code to define a variable, make sure that the address is not executed by the command, or put the definition of the variable into a data segment.
3. CMP commands are used for comparison, followed by BNE, BGE, Bgt, BLT, and ble jump commands.
4. You can use. Word to use the identifier as a constant.
For example:
Start:
Valueofstart:
. Word start
In this way, the start of the program is saved into the memory variable valueofstart.
5. ADR rn 0; indicates that ADR can be used instead of R0 later.
6. The tag exists in the compilation process, and the value corresponding to the tag itself is an absolute address. For example:
. Word label
The defined value is the absolute address corresponding to the label.
Some compilation statements convert them into absolute addresses during use, for example:
LDR r0, label
Equivalent to LDR r0, [label-PC]
Difference between LDR and ADR:
ADR only obtains the relative address of the label and obtains the relative address of the label. The code can be moved without changing the relative location of the label. LDR r0, = _ start obtains the absolute address of _ start. The code can be moved without changing the absolute position of the _ start tag, while LDR r0, _ start reads the value of the address of the current PC based on _ start. It is a location-independent code, it can be moved without changing the relative position of the _ start label.
LDR r0, _ start
ADR r0, _ start
LDR r0, = _ start
NOP
MoV PC, LR
_ Start:
NOP

During compilation, set Ro to 0x0c008000 when there are too many characters in the compiler. when there are too many other than 0c008000 <_ start-0x14>:
C008000: e59f000c LDR r0, [PC, #12]; c008014 <_ Start>
C008004: e28f0008 add r0, PC, #8; 0x8
C008008: e59f0008 LDR r0, [PC, #8]; c008018 <_ start + 0x4>
C00800c: e1a00000 Nop (mov r0, R0)
C008010: e1a0f00e mov PC, lr0c008014 <_ Start>:
C008014: e1a00000 Nop (mov r0, R0)
C008018: 0c008014 stceq 0, cr8, [R0],-#80 analysis: LDR r0, _ start reads the value from the memory address _ start. After executing this operation, R0 = 0xe1a00000adr R0 and _ start get the address of _ start to R0. However, please refer to the decompilation result, which is irrelevant to the position. In fact, the relative position obtained. For example, if the code runs at 0x0c008000, then R0 = 0x0c008014 is obtained for ADR R0 and _ start. If the code runs at address 0, it is 0x00000014. LDR r0, = _ start: Obtain the absolute address of the label _ start. This absolute address is determined during link. It seems that this is only an instruction, but it takes up two 32bit spaces. One is the instruction and the other is the data of _ start (because the value of _ start cannot be determined during compilation, in addition, the mov command cannot be used to assign a 32bit constant to R0. Therefore, an extra space is required to store the real data of _ start, which is 0x0c008014 ).
Therefore, we can see that this is an absolute addressing. No matter where the code runs, the result is R0 = 0x0c008014.

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.