Lesson 2: ARM assembly learning

Publisher:平静宁静Latest update time:2016-08-06 Source: eefocusKeywords:ARM Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
First of all, we need to clarify what we are going to learn in this class:

1 Understand the working principle of ARM processor by learning ARM assembly language.

2 Write Bootloader in Assembly

The basic syntax of assembly here has been written in "ARM Embedded System Development". Here are some points that need special attention

When we learn assembly language, we are not required to write many complex programs in assembly language. That is impractical, and those can be written in C, which is a higher level language.

7 addressing modes:

1 immediate addressing ADD r0,r0,#1

2 register addressing ADD r0,r0,r1

3 Register indirect addressing LDR r0, [r1]

4-register indexing addressing: pre-indexing, automatic indexing, post-indexing

LDR r0,[r1,#4]

LDR r0,[r1,#4]!

LDR r0,[r1],#4

5 Stack addressing We usually use FD full decrement LDM {sp}! , {r1-r4}

For stack only

6-block copy addressing

LDMIA LDMIB LDMDA LDMDB

7Relative Addressing

BL label relative addressing

LDR pc, label or MOV pc, label absolute addressing

 

I will not introduce the specific instructions here. Here are some more:

1 In example LDMFD sp! {r1-r5, pc}^

When pc is added, adding ^ means copying spsr to cpsr.

2. Multiplication instructions are rarely used because they are inefficient. Even if they are used, they are replaced by displacement.

3AND is used to indicate zero, ORR is used to indicate 1, EOR is used to negate, and BIC is used to clear a bit (BIC is used to clear a bit of the comparison number, whichever bit is 1 becomes 0, which is the opposite of AND)

While TST is AND without result register and changes cpsr TEQ is XOR CMP is subtraction.

 

Next, let's talk about the focus of this lesson, the armlinux compiler gcc

Under win, armcc is used, while ours uses gcc

1 Any identifier ending with a colon is considered a label. When the label is a number from 0 to 9, it is a local label. Adding f after the local label indicates the label forward from the place where the label is referenced.

Adding b means backward

For example: subs r0, r0, #1

            bne 1f

2. Use the section pseudo-operation to define a segment, followed by the segment name

 The assembly system has predefined segment names. text is the code segment. data is the initialized data segment. bss is the uninitialized data segment (.bss must be before text)

3 Define the entry point, which is the start label by default. You can also use ENTRY (flag) to indicate the entry mark in the connection script. The connection script is introduced later.

.section .data

<...>

.section .bss

<...>

.section.text

.globl _start _start is global

_start: Code execution starts here.

4. .align 2(4,8,16) alignment

    No error is reported if the .end terminator is not added

    .include includes header files

     .global defines a global symbol

 

Top 5 Compiler Tools

arm-linux-as

 arm-linux-gcc

 arm-linux-ld

 arm-linux-objcopy

arm-linux-objdump

Introducing the steps to the birth of the next ARM program.

1. Write the program

2 Compile into .o file using arm-linux-gcc -c link.s -o link.o

If you use the -S option to compile a program written in C to generate an assembly file

 

3. Write a linker script and compile it into an ELF file: First, make it clear why you need to link: there are two points: 1. Address redirection. 2. Write a symbol table. There is a built-in default linker script in the gcc compiler, but it can only be loaded if there is an operating system. Because it is linked by the mapping address of the memory in the operating system. The target file of the gnu compiler is in ELF format. It consists of several sections. There are text segments, data segments, and bss segments. The task of the connector is to connect the text segments, data segments, and bss segments of multiple target files together. The linker script tells the connector what address to start putting these segments.

Such as link.lds

ENTRY(begin)

SECTION

{

.=0x30000000; means the target code starts at address 0x30000000

.text :{*(.text)}

.data :{*(.data)}

.bss :{*(.bss)}

}

Then generate an elf file through arm-linux-ld -nostadlib -o link.elf -Tlink.lds link.o main.o. Nostadlib means not to connect to the system library, -T means to use the link script. You can also use -Ttext address; address means the execution area address.

4 Generate a binary file. The binary file can be loaded into the memory

arm-linux-objcopy -o link.bin link.elf

5 Disassembly

You can also use objdump to disassemble

arm-linux-objdump -D link.elf > a.dis

arm-linux-objdump -D -b binary -m arm link.bin >a.dis

Keywords:ARM Reference address:Lesson 2: ARM assembly learning

Previous article:Lesson 1: S3C2440 programming
Next article:Lesson 3: Light up the LED light on the development board

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号