ARM architecture and programming Learning (6)

Source: Internet
Author: User

ADR pseudocommand instance

; Set the name and attributes of the program

Area adrlabel, code, readonly

Entry

Start

; Jump to the subprogram func for execution

BL func

; Call angel_swireason_reportexception

; Adp_stopped_applicationexit

; Arm semihosting SWI

Stop

MoV r0, #0x18; assign 0x18 to R0 and 0x18 to the macro angel_swireason_reportexception.

LDR R1, = 0x20026; 0x20026 is assigned to R1, 0x20026 immediately number corresponds to the macro adp_stopped_applicationexit, indicating the program

; Exit normally

SWI 0x123456; ends the program and gives control to the debugger

Define a data buffer for generating the offset of the address label relative to the PC

Ltorg

Func

The following pseudo-command ADR is compiled into: Sub r0, PC, # offset to start

ADR r0, start

The following pseudo-command ADR is compiled into: Add R1, PC, # offset to dataarea

ADR R1, dataarea

The following pseudo-command ADR assembly error, because the second operand cannot be represented by dataarea + 4300

; ADR R2. dataarea + 4300

The following pseudo-command adrl is compiled into two commands:

; Add R2, PC, # offset1

; Add R2, R2, # offset2

Adrl R3, dataarea + 4300

; Returned from the subroutine func

MoV PC, LR

; 8000-byte storage units are saved from the current location

And initialize it to 0

Dataarea space 8000

; End Assembly

End

Jump to an instance using a jump table

; Set the program name and attributes

Area jump, code, readonly

; Number of subprograms in the jump table

Num equ 2

; Entry point for Program Execution

Entry

Start

Set three parameters, and then call the sub-program arithfunc to perform arithmetic operations.

MoV r0, #0

MoV R1, #3

MoV R2, #2

; Call the subroutine arithfunc

BL arithfunc

Stop

; Call angel_swireason_reportexception

; Adp_stopped_applicationexit

; Arm semihosting SWI

; Exit from the application

MoV r0, #0x18

LDR R1, = 0x20026

SWI 0x123456

Subprogram arithfunc entry point

Arithfunc

; Determine whether the parameter of the selected subroutine is within the valid range

CMP r0, # num

Movhs PC, LR

; Read the base address of the jump table

ADR R3, jumptable

Jump to the corresponding subroutine Based on the R0 Value

Ldr pc, [R3, R0, LSL #2]

The jump table jumptable stores the addresses of subprograms.

Here there are two subprograms: doadd and dosub.

When R0 is 0, select doadd.

When R0 is 1, select dosub

Jumptable

DCD doadd

DCD dosub

; Subroutine doadd to execute addition operation

Doadd

Add r0, R1, R2

MoV PC, LR

; Subroutine dosub executes the subtraction operation

Dosub

Sub r0, R1, R2

MoV PC, LR

; End Assembly

End

Examples of Using Pseudo-command LDR

; Set the name and attributes of the program

Area ldrlabel, code, readonly

; Entry point for Program Execution

Entry

Start

Jump to the subprogram func1 and func2 for execution

BL func1

BL func2

; Macro angel_swireason_reportexception

; Adp_stopped_applicationexit

; Arm semihosting SWI

; Exit from the application

MoV r0, #0x18

LDR R1, = 0x20026

SWI 0x123456

Func1

The following pseudocommands are compiled into: LDR r0, [PC, # offset to litpool1]

LDR r0, = start

The following pseudocommands are compiled into: LDR R1, [PC, # offset to litpool1]

LDR R1, = darea + 12

The following pseudocommands are compiled into: LDR R2, [PC, # offset to litpool1]

LDR R2, = darea + 6000

; Program return

MoV PC, LR

; Character buffer literal Pool 1

Ltorg

Func2

The following pseudo commands are compiled into: LDR R3, [PC, # offset to litpool 1]

; A total of character Buffers

LDR R3, = fig + 6000

; If the following pseudo commands are not commented out, errors will occur during compilation.

Because the character buffer litpool 2 exceeds the range that can be reached by the pseudo command

; LDR R4, = darea + 6004

; Program return

MoV PC, LR

; The storage unit of 8000 bytes is retained from the current address.

And initialize the content to 0.

Darea space 8000

; Character buffer litpool2 should start from here

; It is out of the range that can be reached by the preceding pseudocommand.

End

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.