ARM Assembler Programming rules

Source: Internet
Author: User
1. Basic Concepts

atpcs (arm-thumb Procedure call standard)

A set of basic rules for calling between subroutines, including rules for using registers during subroutine calls, rules for using data stacks, and rules for passing parameters. With these rules, a separately compiled C language program can be called with the assembler program.
C-language subroutines compiled with the C language compiler of ads satisfy user-specified atpcs types. For assembly language, users are required to ensure that each sub-program satisfies the requirements of atpcs.

aapcs (ARM archtecture Procedure call standard)

2007 ARM Company formally launched the AAPCS Standard, aAPCs is Atpcs's improved version, currently, AAPCS and ATPCS are available standards.
2. Register usage rules

• Pass the parameters through the register R0~R3 between the sub-programs. At this time, register R0~R3 can be remembered as a0~a3. The called subroutine does not need to recover the contents of the register R0~R3 before returning.

• In subroutines, use register R4~R11 to save local variables. At this time, register R4~r11 can be remembered as v1~v8. If some registers in the register v1~v8 are used in the subroutine, the values of these registers must be saved when the subroutine enters, and the values of these registers must be restored before they are returned. In a thumb program, you can usually only use register R4~R7 to save local variables.

• Register R12 is used as the intermediate temporary register for the procedure call, which is recorded as IP. This usage rule is often used in connection code snippets between subroutines.
• Register R13 is used as a stack pointer and is recorded as an SP. Register R13 in subroutines cannot be used for other purposes.    The value of the Register SP when it enters the subroutine and when it exits the subroutine must be equal. • Register R14 is called the Connection register, which is recorded as LR. It is used to save the return address of the subroutine.    If the return address is saved in the subroutine, the register R14 can be used for other purposes. • Register R15 is a program counter, recorded as a PC. It cannot be used for other purposes.
3. Stack usage rules

The atpcs specifies that the stack is FD (full descending:sp points to the last pressed value, the data stack is grown from a high address to a low address) type, that is, the stack is decremented, and the operation on the stack is 8-byte aligned.    So frequently used directives have stmfd and LDMFD.       • For assembler, if an external call is included in the destination file, the following conditions must be met: (1) The stack of the external interface must be 8-byte aligned. (2) Use the PRESERVE8 pseudo-directive in the assembler to tell the connector that this assembler data is 8-byte aligned.
4. Parameter passing rules

• Depending on whether the number of parameters is fixed, sub-programs can be divided into a number of parameters fixed subroutine and the number of parameters can be changed subroutine. • The parameter passing rules for these two seed programs are not the same.
4.1 parameter number variable subroutine parameter passing rule

• For subroutines with variable number of arguments, you can use a register R0~R3 to pass parameters when the number of parameters is less than 4, and you can use the stack to pass parameters when there are more than 4 arguments. • When passing parameters, consider all parameters as Word data stored in contiguous memory word cells. Then, each word data is passed to the register R0,R1,R2 and R3 in turn. If the parameter is more than 4 One, the remaining The word data is passed to the stack. the order of the stacks is the opposite of the order of the parameters, that is, the last Word data is first entered into the stack.
4.2 parameter number fixed subroutine parameter passing rule

• If the system does not contain a hardware part of a floating-point operation, the floating-point parameter is converted to an integer parameter by the corresponding rule (omitted if there is no floating-point argument), and then the individual data is transferred to the register R0~R3 in turn. If there are more than 4 arguments, the remaining Word data is transferred to the stack in the same order as the parameter order, that is, the last Word data is first in the stack. When parameters are passed, all parameters are treated as Word data stored in contiguous memory word cells.
5. Subroutine results The rule returned in the Rule subroutine is as follows: • When the result is a 32-bit integer, it can be returned by a register R0; • When the result is a 64-bit integer, it can be returned by registers R0 and R1; When the result is a floating-point number, Can be returned by a register of floating-point arithmetic parts F0, D0, or s0; • When the result is a composite floating-point number (such as a plural), it can be returned by a register f0~fn or D0~DN; • For more bits of the result, it needs to be passed through memory.

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.