ARM Assembler Programming Simple example

Source: Internet
Author: User
Tags constant lowercase printf processing instruction

ARM Assembler Programming Simple example

1). Basic Concepts

(2) Register  such as R0, R1 and other arm assembly programming  is essentially for the CPU register programming.

(3) Instruction code  direct control Cpu such as MOV includes jump instruction, data processing instruction, multiplication instruction, PSR Access instruction, loading or storing instruction, data exchange instruction, shift instruction, etc.

(4) Pseudo-operation acts on the compiler, mostly for definition and control. such as area includes symbol definition, data definition, control, etc.

(5) The label is only an identification. In a jump statement,  can point to the location of the identification number that you want to jump to. The  designator in the ARM assembly represents the address of an address in the  segment to determine the address value of the label outside the  segment at assembly time when connecting

(6) Symbol: That is, the label (representing the address), variable name, numeric constant name, and so on. The naming rules for symbols are as follows

a. Symbols consist of uppercase and lowercase letters, numbers, and underscores

b. In addition to the local designator  the other symbols cannot begin with a number

c. Symbols are case-sensitive  and all characters are meaningful

d. Symbols at their scope you have to be unique.

e. Symbols cannot have the same name as a system-internal or system-predefined symbol

f. Symbols do not have the same name as instruction mnemonics or pseudo-directives.

2). Segment definition

 program code in assembly language with relatively independent instructions or sequences of data

Section is divided into data section, code snippet. A assembler has at least one code snippet

(1) Code snippet

Area defines a segment  and describes the related properties of the defined segment code used to indicate the code snippet

ENTRY identifies the entry point of the program. End for the program.

(2) Data segment

Area dataareadata,biinit,allgn=2

DISPBUF SPACE 200

RCVBUF SPACE 200

Data is used to indicate the segment

space allocates 200 bytes of storage unit and initializes it to 0

3). assembly Language Structure

Label [instruction or pseudo-operation]

All labels must be written in a line of shelf  followed by no colons

All instructions cannot be shelf written.

Instruction mnemonic case Sensitive  cannot be case mixed  can only be all uppercase or lowercase

; for comment

@ Code line comment  with;

#整行注释或直接操作数前缀

\ is a newline character.

ENTRY for the entrance of the program

End is the ending of the program

common structure of ARM programs

1. Sub-functions and main functions

Using the BL directive, the command will store the returned PC value in LR

Area example,code,readonly; declaring code Snippets Example

ENTRY; procedure Entrance

Start

MOV R0, #0; Sets the arguments that will be passed to the child the con's arguments are stored in r0 and R1

MOV R1, #10

BL Add_sum

; Calling subroutine Add_sum

B over; Jump to the over label and enter the end

Add_sum

Add r0,r0,r1; Implement two-number addition

MOV PC,LR; The subroutine returns the result returned in r0

Over

END

1. Macro Definition (MACRO, MEND) '

Format:

macro

{$ label name} Macro name {$ parameter 1$ parameter 2 ""}

Instruction sequence

MEND

Macro, MEND pseudo-directives can define a piece of code as a whole  called a macro directive  calls the code multiple times in a program through a macro directive.

{} is optional

$ marking when a macro is expanded  labels are replaced with user-defined symbols the first line of the macro definition body should declare the prototype of the macro (containing the macro name, the required parameters)  and then you can call the sequence of instructions in the assembler by the macro name

Write in front of a code snippet or data segment

Mexit jumping out of a macro

Example: a macro with no parameters returns a child function

macro

MOV_PC_LR; Macro Name

MOV PC,LR; The subroutine returns the result returned in r0

MEND

Area example,code,readonly; declaring code Snippets Example

ENTRY; procedure Entrance

Start

MOV R0, #0

MOV_PC_LR; Call macro to represent the end of a child function

END

Example:parameters macro macro definition from macro pseudo-directive to mend end and can use parameters. # define # similar to C

Macro definition

Call $Function, $dat 1, $dat 2; The macro name is call with 3 parameters

IMPORT $Function; Declare outer sub-Cheng start

MOV R0, $dat 1; Set subroutine parameters, r0= $dat 1

MOV R1, $dat 2

BL $Function; Call the last sentence of the sub Cheng

MEND; end of macro definition

Call FADD1, #3, #2; the macro is called after three arguments

After the assembly preprocessing, the macro call will be expanded to the following list

import FADD1

MOV R0, #3

MOV R1, #3

BL FADD

Mutual invocation between C and arm assembler programs

1. Assembler calls C subroutine

To ensure that parameters are passed correctly when the program calls,  must comply with ATPCS.

In a C program, a function cannot be defined as a static function. In assembler, you need to declare C sub-functions by using the import pseudo-operation in assembly language

2. Assembler accesses global C variables

The global variables defined in the C language can be accessed indirectly through the address of the C global variable in the assembler program.

Introducing the C global variable with import in the  the name of the C global variable is considered to be a label in the assembler. Access the address represented by the number by LDR and STR directives

C code

int i=3;

int SUM5 (int a, int b, int c, int d)

{

return (a+b+c+d+i);

}

Assembly code

Area example,code,readonly; declaring code Snippets Example

IMPORT SUM5

IMPORT I

ENTRY; procedure Entrance

Start

LDR r1,i; Read I into the R1

MOV R0, #2

ADD R0,R0,R1

STR r0,i; Write register value to I

MOV R3, #4; Set arguments, write parameters to R0-r3

MOV R2, #3

MOV R1, #2

MOV R0, #1

BL SUM5; Calling subroutine Add_sum

B over; Jump to over label  enter end

Over

END

3. Calling assembler subroutine in C language

In order to ensure proper pass-through of parameters in the program call  the assembler must use export pseudo-operation to declare assembler subroutine  and use extern extension declaration assembler in C language

4. Calling assembly global variables in the C language

The Assembly uses DCD to allocate space for global variables and assign values and define a label to represent that storage location.

Export the label  in the Assembly the label is the global variable  the variable in the C program with the extern extension declaration name

Assembly code

EXPORT func1

EXPORT tmp

Area example,code,readonly; declaring code Snippets Example

TMP; global variable name

DCD 0x0005; Global variables create memory space and assign initial value

func1; Name of the child function

Add r0,r0,r1; Implement two-number addition

ADD R0,R0,R2

ADD R0,R0,R3

MOV PC,LR; The subroutine returns the result returned in r0

END

C code

extern int func1 (int a,int b,int c,int D);

extern int tmp;

int main (int argc,char **argv)

{

int a=1,b=2,c=3,d=4;

int z=func1 (A,B,C,TMP);

printf ("%d", z);

return 0;

}

5. Inline compilation in C language

Some operations C language programs cannot be implemented such as changing the CPSP register value to initialize the stack pointer register SP etc. these can only be done by the Assembly.

However, some factors, such as programming simplicity, sometimes need to be implemented in C source code. In this case, you need to embed a small number of assembly code in C.

The inline assembly cannot directly refer to the C variable definition, which must be done through the ATPCS syntax format as follows:

__asm{

inline assembly

}

Example:

 embedding the assembly in the C language

int F ()

{

C function

__asm{

Inline assembly Disable Interrupt Example

MRS R0,CPSR

ORR R0,r0, #0x80

MSR cpsr_c,r0

}

}

int main (int argc,char **argv)

{

int A;

int Z=f (a);

printf ("%d", z);

return 0;

}

The different characteristics of the built-in assembly relative to the General Assembly are given in the following

1) The operand can be a register, a constant, or a C-expression. Can be a char, short, or int type and is an unsigned number operation

2) The # number before the constant can be omitted

3) only instruction B can use the designator in C program, instruction BL can not be used

4) Pseudo-operation for memory allocation in assembly language is not supported

5) inline assembly is not supported by "." Indicator or PC gets the current instruction address

6) Do not support LDR rn,=expression pseudo-instruction and use MOV rn,expression instruction to assign value to register

7) label expression not supported

8) ADR and ADRL pseudo-directives are not supported

9) does not support BX and BLX instructions

10) No value can be assigned to the PC

11) Replace & represent hexadecimal number with 0x prefix

12) Do not use the Register addressing variable

The register list of LDM and STM directives only allows physical registers

14) must be careful to use physical registers such as R0-R3,LR and PC

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.