大多数理智的编译器编译成汇编,然后调用汇编器将其转换为对象。
unsigned int fun ( unsigned int a, unsigned int b)
{
return((a<<1)+(b^0xFF));
}
arm-none-eabi-gcc -O2 -c so.c -o so.o
arm-none-eabi-objdump -D so.o
00000000 <fun>:
0: e22110ff eor r1, r1, #255 ; 0xff
4: e0810080 add r0, r1, r0, lsl #1
8: e12fff1e bx lr
我发现这是读取输出的最简单方法,即使我允许组装它。
但你可以这样做
arm-none-eabi-gcc -O2 -S so.c -o so.s
或
arm-none-eabi-gcc -O2 -c -save-temps so.c -o so.o
看看so.s
cat so.s
.cpu arm7tdmi
.eabi_attribute 20, 1
.eabi_attribute 21, 1
.eabi_attribute 23, 3
.eabi_attribute 24, 1
.eabi_attribute 25, 1
.eabi_attribute 26, 1
.eabi_attribute 30, 2
.eabi_attribute 34, 0
.eabi_attribute 18, 4
.file "so.c"
.text
.align 2
.global fun
.arch armv4t
.syntax unified
.arm
.fpu softvfp
.type fun, %function
fun:
@ Function supports interworking.
@ args = 0, pretend = 0, frame = 0
@ frame_needed = 0, uses_anonymous_args = 0
@ link register save eliminated.
eor r1, r1, #255
add r0, r1, r0, lsl #1
bx lr
.size fun, .-fun
.ident "GCC: (GNU) 8.2.0"
你可以自己组装
arm-none-eabi-as so.s -o so.o
arm-none-eabi-objdump -D so.o
so.o: file format elf32-littlearm
Disassembly of section .text:
00000000 <fun>:
0: e22110ff eor r1, r1, #255 ; 0xff
4: e0810080 add r0, r1, r0, lsl #1
8: e12fff1e bx lr
并获得相同的对象,就好像您没有单独执行这些步骤一样。这也意味着您可以在汇编中编写自己的函数并将其链接到项目中,就像使用 C 编译对象一样。
.global fun
fun:
eor r1, r1, #255
add r0, r1, r0, lsl #1
bx lr
arm-none-eabi-as so.s -o so.o
arm-none-eabi-objdump -D so.o
Disassembly of section .text:
00000000 <fun>:
0: e22110ff eor r1, r1, #255 ; 0xff
4: e0810080 add r0, r1, r0, lsl #1
8: e12fff1e bx lr