ARM assembly language series based on Android 3: ARM assembly language program structure

Publisher:徽宗古泉Latest update time:2016-07-01 Source: eefocusKeywords:Android Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Chapter List

Part 1: Introduction to ARM assembly language 
Part 2: Analysis of the process of generating ARM assembly programs from C/C++ programs Part 
3: ARM assembly language program structure 
Part 4: Addressing methods of ARM processors 
Part 5: ARM instruction set and Thumb instruction set 
Part 6: NEON instruction set and VFP instruction set

The ARM assembly of the Android platform is in the GNU ARM assembly format, and the assembler used is GAS (GNU Assembler). GAS has its own set of syntax structures. For details, please refer to the official manual of GAS syntax structure.

Let's first look at a complete ARM assembly program:

C code:

#include 

int main(int argc, char* argv[]){
    printf("Hello ARM!\n");
    return 0;
}

The corresponding assembly code:

    .arch armv5te
    .fpu softvfp
    .eabi_attribute 20, 1
    .eabi_attribute 21, 1
    .eabi_attribute 23, 3
    .eabi_attribute 24, 1
    .eabi_attribute 25, 1
    .eabi_attribute 26, 2
    .eabi_attribute 30, 6
    .eabi_attribute 18, 4
    .file "hello.c"
    .section .rodata
    .align 2
.LC0:
    .ascii "Hello ARM!\000"
    .text
    .align 2
    .global main
    .type main, %function
main:
    @args = 0, pretend = 0, frame = 8
    @frame_needed = 1, uses_anonymous_args = 0
    stmfd sp!, {fp, lr}
    add fp, sp, #4
    sub sp, sp, #8
    str r0, [fp, #-8]
    str r1, [fp, #-12]
    ldr r3, .L3
.LPIC0:
    add r3, pc, r3
    mov r0, r3
    bl puts(PLT)
    mov r3, #0
    mov r0, r3
    sub sp, fp, #4
    ldmfd sp!, {fp, pc}
.L4:
    .align 2
.L3:
    .word .LC0-(.LPIC0+8)
    .size main, .-main
    .ident "GCC: (GNU) 4.4.3"
    .section .note.GNU-stack,"",%progbits

The following section describes the structure of this code.

1. Processor architecture definition

    .arch armv5te @processor architecture
    .fpu softvfp @coprocessor type
    .eabi_attribute 20, 1 @Interface attribute
    .eabi_attribute 21, 1
    .eabi_attribute 23, 3
    .eabi_attribute 24, 1
    .eabi_attribute 25, 1
    .eabi_attribute 26, 2
    .eabi_attribute 30, 6
    .eabi_attribute 18, 4

1.1 Processor Architecture

.arch specifies the ARM processor architecture. armv5te means that the code of this program can run on processors with armv5te architecture.

1.2 Coprocessor Type

.fpu specifies the type of coprocessor. softvfp means using the floating point library to simulate coprocessor operations. You can also use vfpv2 and vfpv3 to specify the built-in coprocessor.

1.3 Interface attributes

.eabi_attrbute specifies some interfaces.

EABI (Embedded Application Binary Interface) is a set of interface specifications specified by ARM, and the Android system implements it.

Two-part definition

    .file "hello.c"
    .section .rodata
    .align 2
.LC0:
    .ascii "Hello ARM!\000"
    .text
    .align 2
    .global main
    .type main, %function
main:
    @args = 0, pretend = 0, frame = 8
    @frame_needed = 1, uses_anonymous_args = 0
    stmfd sp!, {fp, lr}
    add fp, sp, #4
    sub sp, sp, #8
    str r0, [fp, #-8]
    str r1, [fp, #-12]
    ldr r3, .L3
.LPIC0:
    add r3, pc, r3
    mov r0, r3
    bl puts(PLT)
    mov r3, #0
    mov r0, r3
    sub sp, fp, #4
    ldmfd sp!, {fp, pc}
.L4:
    .align 2
.L3:
    .word .LC0-(.LPIC0+8)
    .size main, .-main
    .ident "GCC: (GNU) 4.4.3"
    .section .note.GNU-stack,"",%progbits

The definition format of the ARM middle section is as follows:

.section name, "flags", %type, flag_specific_arguments
  • name: segment name
  • flags: Segment attributes, such as read, write, and executable.
  • type: The type of segment, such as progbits means that the segment contains data, and note means that the data contained in the segment is not used by the program itself.
  • flag_specific_arguments: Specifies some platform-dependent parameters.

3. Notes and Numbers

GNU ARM supports two ways of adding comments.

/* */ type comments

/* args = 0, pretend = 0, frame = 8 */
/* frame_needed = 1, uses_anonymous_args = 0 */

@-type annotation

@args = 0, pretend = 0, frame = 8
@frame_needed = 1, uses_anonymous_args = 0

Four assembler instructions

.file "hello.c"
    .section .rodata
    .align 2
.LC0:
    .ascii "Hello ARM!\000"
    .text
    .align 2
    .global main
    .type main, %function

All instructions in the program that start with . are assembler instructions. Assembler instructions are related to the assembler and do not belong to the ARM instruction set.

  • .file: Specifies the source file name.
  • .align: Specifies the alignment of the code, and the value following it is a power of 2.
  • .ascii: declares a string.
  • .global: declares global symbols, which are symbols accessible outside this program.
  • .type: Specifies the type of the symbol.
  • .word: used to store address values.
  • .size: Sets the size of the specified symbol.
  • .ident: Compiler identifier, no practical use.

Five subroutines and parameter passing

A subroutine represents an independent function in the code. In many cases, a subroutine and code are the same concept. The way to declare a function in assembly is as follows:

.global function name
.type function name%Function
Function name:
    ...
    Function body
    ...

Then during the function call, the parameter passing method is as follows:

ARM assembly language stipulates that the four registers R0 to R3 are used to pass the first to fourth parameters of a function call, and the parameters beyond that are passed through the stack. The R0 register is used to store the return value of a function call. The called function does not need to restore the contents of these registers before returning.

Keywords:Android Reference address:ARM assembly language series based on Android 3: ARM assembly language program structure

Previous article:ARM assembly language series based on Android VI: NEON instruction set and VFP instruction set
Next article:ARM assembly language series based on Android 5: ARM instruction set and Thumb instruction set

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号