1、arm-linux-gcc:
首先编写C程序,假设名字为test.c,保存test.c文件内容:
#include<stdio.h>
int main()
{
printf("hello.world!\n");
return 0;
}
在X86架构下的电脑上生成ARM架构的汇编代码有两种方式:
1、使用交叉编译工具链:arm-linux-gcc,指定-S选项可以生成汇编中间文件。
2、使用arm-linux-objdump反汇编arm二进制文件。
1、arm-linux-gcc:
首先编写C程序,假设名字为test.c,保存test.c文件内容:
#include<stdio.h>
int main()
{
printf("hello.world!\n");
return 0;
}
使用方法如下:
在使用arm-linux-gcc编译C源文件时,使用-S选项可以将C文件(test.c为例)编译到汇编阶段,生成arm汇编代码,使用方式如下:
arm-linux-gcc -march=armv7-a -mtune=cortex-a9 test.c -S -o test.asm
生成arm汇编文件test.asm。
说明:
-march可以指定目标ARM的架构可选参数见(man gcc)
-mtune(类似于-mcpu)可以具体到ARM处理器类型。
注意:
Specifying both -march= and -mcpu= is redundant, and may not in fact have done what you expected in previous compiler versions (maybe even depending on the order in which the arguments were given). The -march switch selects a "generic" ARMv7-A CPU, and -mcpu selects specifically a Cortex-A8 CPU with tuning specific for that core.
Either use "-march=armv7-a -mtune=cortex-a8", or just use "-mcpu=cortex-a8".
Either use "-march=armv7-a -mtune=cortex-a8", or just use "-mcpu=cortex-a8".
所以只用一个-mcpu=cortex-a9也可以
test.asm内容:
1 .arch armv4t
2 .fpu softvfp
3 .eabi_attribute 20, 1
4 .eabi_attribute 21, 1
5 .eabi_attribute 23, 3
6 .eabi_attribute 24, 1
7 .eabi_attribute 25, 1
8 .eabi_attribute 26, 2
9 .eabi_attribute 30, 6
10 .eabi_attribute 18, 4
11 .file "test.c"
12 .section .rodata
13 .align 2
14 .LC0:
15 .ascii "hello.world!\000"
16 .text
17 .align 2
18 .global main
19 .type main, %function
20 main:
21 .fnstart
22 .LFB2:
23 @ Function supports interworking.
24 @ args = 0, pretend = 0, frame = 0
25 @ frame_needed = 1, uses_anonymous_args = 0
26 stmfd sp!, {fp, lr}
27 .save {fp, lr}
28 .LCFI0:
29 .setfp fp, sp, #4
30 add fp, sp, #4
31 .LCFI1:
32 ldr r0, .L3
33 bl puts
34 mov r3, #0
35 mov r0, r3
36 sub sp, fp, #4
36 sub sp, fp, #4
37 ldmfd sp!, {fp, lr}
38 bx lr
39 .L4:
40 .align 2
41 .L3:
42 .word .LC0
43 .LFE2:
44 .fnend
45 .size main, .-main
46 .ident "GCC: (Sourcery G++ Lite 2009q1-176) 4.3.3"
47 .section .note.GNU-stack,"",%progbits