ARM 汇编语言
一门语言通常有自己的关键字、代码规范、子程序调用、注释等,汇编语言也一样
汇编语言:将一系列与处理器相关的汇编指令用某种语法和结构组织在一起的程序语言形式
用特定汇编语法规范编写的汇编代码,可被完整地编译或嵌入其他高级语言
Android 中的 ARM 汇编使用 GNU 汇编格式,现在开始学习 GNU ARM 汇编的一般语法格式及特点
ARM 汇编程序结构
编译器在编译阶段会在内部将程序代码编译成与机器相关的汇编指令,上一节的 hello.c 的汇编代码就是一个完整的 ARM 汇编程序
现在编写一个新的程序 app.c,代码:
#include <stdio.h>
int add(int a, int b, int c, int d)
{
return a + b + c + d;
}
int main(int argc, char const *argv[])
{
printf("add: %d\n", add(1, 2, 3, 4));
return 0;
}
执行如下命令,生成 app.s 汇编代码:
系统、NDK 版本、CC 环境变量设置等信息见上一节
.text
.syntax unified
.cpu arm1022e
.eabi_attribute 6, 4 @ Tag_CPU_arch
.eabi_attribute 8, 1 @ Tag_ARM_ISA_use
.eabi_attribute 15, 1 @ Tag_ABI_PCS_RW_data
.eabi_attribute 16, 1 @ Tag_ABI_PCS_RO_data
.eabi_attribute 17, 2 @ Tag_ABI_PCS_GOT_use
.eabi_attribute 20, 1 @ Tag_ABI_FP_denormal
.eabi_attribute 21, 1 @ Tag_ABI_FP_exceptions
.eabi_attribute 23, 3 @ Tag_ABI_FP_number_model
.eabi_attribute 24, 1 @ Tag_ABI_align_needed
.eabi_attribute 25, 1 @ Tag_ABI_align_preserved
.eabi_attribute 18, 4 @ Tag_ABI_PCS_wchar_t
.eabi_attribute 26, 2 @ Tag_ABI_enum_size
.file "app.c"
.globl add
.align 2
.type add,%function
add: @ @add
.fnstart
.Leh_func_begin0:
@ BB#0: @ %entry
.pad #16
sub sp, sp, #16
str r0, [sp, #12]
str r1, [sp, #8]
str r2, [sp, #4]
str r3, [sp]
ldr r0, [sp, #12]
ldr r1, [sp, #8]
add r0, r0, r1
ldr r1, [sp, #4]
add r0, r0, r1
ldr r1, [sp]
add r0, r0, r1
add sp, sp, #16
bx lr
.Ltmp0:
.size add, .Ltmp0-add
.cantunwind
.fnend
.globl main
.align 2
.type main,%function
main: @ @main
.fnstart
.Leh_func_begin1:
@ BB#0: @ %entry
.save {r4, r5, r11, lr}
push {r4, r5, r11, lr}
.setfp r11, sp, #8
add r11, sp, #8
.pad #24
sub sp, sp, #24
ldr r2, .LCPI1_6
.LPC1_0:
add r2, pc, r2
ldr r3, .LCPI1_2
ldr r12, .LCPI1_3
ldr lr, .LCPI1_4
ldr r4, .LCPI1_5
ldr r5, .LCPI1_0
str r5, [r11, #-12]
str r0, [sp, #16]
str r1, [sp, #12]
mov r0, r3
mov r1, r12
str r2, [sp, #8] @ 4-byte Spill
mov r2, lr
mov r3, r4
bl add(PLT)
ldr r1, .LCPI1_1
ldr r2, [sp, #8] @ 4-byte Reload
add r1, r1, r2
str r0, [sp, #4] @ 4-byte Spill
mov r0, r1
ldr r1, [sp, #4] @ 4-byte Reload
bl printf(PLT)
ldr r1, .LCPI1_0
str r0, [sp] @ 4-byte Spill
mov r0, r1
sub sp, r11, #8
pop {r4, r5, r11, pc}
.align 2
@ BB#1:
.LCPI1_0:
.long 0 @ 0x0
.LCPI1_1:
.long .L.str(GOTOFF)
.LCPI1_2:
.long 1 @ 0x1
.LCPI1_3:
.long 2 @ 0x2
.LCPI1_4:
.long 3 @ 0x3
.LCPI1_5:
.long 4 @ 0x4
.LCPI1_6:
.long _GLOBAL_OFFSET_TABLE_-(.LPC1_0+8)
.Ltmp1:
.size main, .Ltmp1-main
.cantunwind
.fnend
.type .L.str,%object &