ARM compilation Learning-writing a simple arm assembler

Source: Internet
Author: User
Tags constant define local expression numeric lowercase mixed

https://blog.csdn.net/zqixiao_09/article/details/50725254


First, let's start by looking at a simple assembler program:

        Area ff,code,readonly   ; Declaration Code snippet  
        CODE32  ; declared as 32-bit ARM instruction  
        entry   ; Declaration procedure entry  
    start  
        ; b instruction  
        ; 1.b Jump Range +_ 32M B + Label  
        ; b start  
        ; b stop  
        ; 2.BL sub function call  
        ; The address of the prefetch instruction is stored in LR (R14)  
        ; 3.bx Sub-function returns  
        mov r0, #9 C15/>mov R1, #15  
        mov r5, #9  
        bl func  
        ; int func (int a,int b)  
    stop  
        b stop  
    func  
        mov R5, #1   
    Loop  
        cmp r0,r1  
        beq stop1  
        subgt r0,r0,r1  
        sublt r1,r1,r0  
        b loop  
    stop1  
        bx LR  
        End  


As can be seen, the arm assembler is annotated with the ";" number.

First, assembly language program format

A complete arm assembly consists of two parts: the declaration , the actual code snippet , and the two parts.

1. Statement

make a declaration before you start a program:

1) Declare the code snippet:

Define a segment with an area directive that describes the related properties of the defined segment. (description segment's name, Segment's properties)

2) Declare the ARM directive:

Use CODE32 or CODE16 to declare that the program is a 32-bit arm instruction or 16-bit thumb instruction.

3) Declare the entry of the program:

Use the entry directive to identify the entry point of the program.

Note:

These 3 statements are indispensable.

After the program finishes, declare the program with the end directive. Each assembler segment must have an end directive that indicates the end of the code snippet.

2. Paragraph

1) in Arm assembly language program, the code is organized as a unit of program segment . A segment is a relatively independent instruction or data series that has a specific name.

2) Classification of segments

Code Snippet: The contents of a code snippet are code execution

Data Segments: Data segments hold the data that the code needs to run.

Note: A assembler program has at least one code snippet. If the program is longer, you can split it into multiple code snippets and data segments. Multiple segments eventually form an executable image file when the program compiles the connection.

3) segment has the following properties

øreadonly

Øreadwrite

Second, assembly language statement format

[LABEL] Operation [OPERAND] [; COMMENT]

Label Field Operation Mnemonic field Operand field Comment field

1. Label field (lable)

The 1> label field is used to represent the address, variable, procedure name, address, and constant of the data for the instruction.

The 2> label is an identifier that can be named by itself, which can be mixed with uppercase and lowercase letters, usually beginning with a letter, consisting of letters, numbers, underscores, and so on.

The 3> statement label cannot have the same name as the register name, instruction mnemonic, pseudo-directive (manipulation) mnemonic, and variable name.

4> statement labels must be written at the beginning of a line and cannot be left blank.

2. Operation Mnemonic Value Field (operation)

1> Operation mnemonic Value field can be a mnemonic for instruction, pseudo-operation, macro-instruction, or pseudo-instruction.

2> ARM Assembler is case sensitive, in assembly language programming, each instruction mnemonic can be all uppercase, or all in lowercase, but not allowed in an instruction large, lowercase mixed.

3> All instructions cannot be written at the beginning of the line, there must be a space in front of the instruction, and then write the instruction.

There must be a space between the 4> instruction mnemonic and the following operand or operation register, and you cannot use commas between them.

3. Operand field (OPERAND)

The Operand field represents the object of the operation, the operand can be a constant, a variable, a label, a register name, or an expression, and the different objects must be separated by a comma ",".

iii. ARM Instruction set format

opcode {<cond>}{s} <Rd>,<Rn> {, <operand2>}

1. The items in the <> are required, and the items in the {} are optional.

2. OpCode indicates the instruction mnemonic .

Cond: Represents the execution condition .

S: Indicates whether the value of the CPSR register is affected .

Rd: Represents the target register .

Rn: A register representing the first operand .

Operand2: Represents a 2nd operand .

3, "Operand2" has the following form:

1> #immed_8r: constant expression

Eg:mov R0, #1

ADD R0,r1, #0X0F

2>rm: Register form.

That is, in the Register mode, the operand is the value of the register.

Eg:mov pc,r0

ADD R1,R1,R2

3>rm,shift: Register shift mode.

The shift result of the register as the operand, when the RM value remains unchanged.

Øasr #n: Represents arithmetic right shift n bits.

ØLSR #n: Represents the logical right shift n bits.

Øror #n: Indicates that the loop is shifted right by n bits.

Ørrx #n: The loop with the extension right shifts n bits.

ØLSL #n: Shift the logical left n bits.

4, using the condition code "cond" can achieve efficient logic operation, improve the efficiency of the Code.

Ø all arm directives can be executed conditionally.

Øthumb instruction only B (jump) instruction has conditional execution function.

Note: If the condition code is not indicated in execution, the default is unconditional (AL) execution.

iv. symbols commonly used in assembler programs

In assembly language programming, various symbols are often used to denote variables, constants, and addresses.

Ø symbols consist of uppercase and lowercase letters, numbers, and underscores.

Ø symbols are case-sensitive, and large, lowercase symbols with the same name are considered by the compiler to be two different symbols.

Ø the symbol must be unique within its scope, that is, the symbol of the same name cannot be found within its scope of action.

Ø the custom symbol name cannot be the same as the reserved word of the system.

Symbol names should not have the same name as directives or pseudo-directives.

1. Variables in the program:

Variables supported by the 1>ARM assembler include numeric variables, logical variables, and string variables

2> in arm assembler programming, you can declare global variables using the GBLA,GBLL,GBLS pseudo-definition, declare local variables using LCLA,LCLL,LCLS, and use Seta,setl and sets to initialize them by line.

2. Constants in the program

Constants supported by the 1>arm assembler are numeric constants, logical constants, and string constants.

3. Variable substitution in the program

A variable in a 1> program can be taken by a constant in a substitution operation. The substitution operator is "$"

2> Use Example:

LCLS    S1  
LCLS    S2    ; define local string variables S1 and S2  
S1      sets        "test!"  
S2      Sets        



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.