C言語→アセンブリ
以下のソースをThumb(16bit)、ARM(32bit)へコンパイルして機械語の長さ(ビット数)を確認する
int add(int a, int b) {
return a + b;
}
C言語→Thumb
機械語を確認すると全て16bit長になっている。
testuser@CasaOS:~/test$ arm-none-eabi-gcc -mthumb -mcpu=cortex-m3 -O0 -c test.c -o test.o
testuser@CasaOS:~/test$ arm-none-eabi-objdump -d test.o
test.o: file format elf32-littlearm
Disassembly of section .text:
00000000 <add>:
0: b480 push {r7}
2: b083 sub sp, #12
4: af00 add r7, sp, #0
6: 6078 str r0, [r7, #4]
8: 6039 str r1, [r7, #0]
a: 687a ldr r2, [r7, #4]
c: 683b ldr r3, [r7, #0]
e: 4413 add r3, r2
10: 4618 mov r0, r3
12: 370c adds r7, #12
14: 46bd mov sp, r7
16: bc80 pop {r7}
18: 4770 bx lr
C言語→ARM
機械語を確認すると全て32bit長になっている。
testuser@CasaOS:~/test$ arm-none-eabi-gcc -marm -mcpu=cortex-a9 -O0 -c test.c -o test_arm.o
testuser@CasaOS:~/test$ arm-none-eabi-objdump -d test_arm.o
test_arm.o: file format elf32-littlearm
Disassembly of section .text:
00000000 <add>:
0: e52db004 push {fp} @ (str fp, [sp, #-4]!)
4: e28db000 add fp, sp, #0
8: e24dd00c sub sp, sp, #12
c: e50b0008 str r0, [fp, #-8]
10: e50b100c str r1, [fp, #-12]
14: e51b2008 ldr r2, [fp, #-8]
18: e51b300c ldr r3, [fp, #-12]
1c: e0823003 add r3, r2, r3
20: e1a00003 mov r0, r3
24: e28bd000 add sp, fp, #0
28: e49db004 pop {fp} @ (ldr fp, [sp], #4)
2c: e12fff1e bx lr
直接アセンブリで書く
Thumb(16bit)、ARM(32bit)それぞれ実行できるかを確認する。
Thumb
ThumbでADDS r0, r0, r1は使えない。 adds r0, #2`のように片方は即値
.global _start
.thumb
.thumb_func
.syntax unified
_start:
MOVS r0, #1
ADDS r0, #2
MOVS r7, #1
SVC 0
testuser@CasaOS:~/atest$ as -o test.o test.s -mcpu=cortex-a5 -mthumb
testuser@CasaOS:~/atest$ ld -o test test.o -e _start
testuser@CasaOS:~/atest$ objdump -d test
test: file format elf32-littlearm
Disassembly of section .text:
00010054 <_start>:
10054: 2001 movs r0, #1
10056: 3002 adds r0, #2
10058: 2701 movs r7, #1
1005a: df00 svc 0
testuser@CasaOS:~/atest$ gdb ./test
GNU gdb (Debian 13.1-3) 13.1
Copyright (C) 2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "arm-linux-gnueabihf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./test...
(No debugging symbols found in ./test)
(gdb) break *0x1005a
Breakpoint 1 at 0x1005a
(gdb) run
Starting program: /home/testuser/atest/test
Breakpoint 1, 0x0001005a in _start ()
(gdb) info registers r0
r0 0x3 3
ARM(32bit)
.global _start
.arm
_start:
MOV r0, #1
MOV r1, #2
ADD r0, r0, r1
MOV r7, #1 @ syscall: exit
SVC 0
testuser@CasaOS:~/atest$ as -o test.o test.s
testuser@CasaOS:~/atest$ ld -o test test.o -e _start
testuser@CasaOS:~/atest$ objdump -d test
test: file format elf32-littlearm
Disassembly of section .text:
00010054 <_start>:
10054: e3a00001 mov r0, #1
10058: e3a01002 mov r1, #2
1005c: e0800001 add r0, r0, r1
10060: e3a07001 mov r7, #1
10064: ef000000 svc 0x00000000
testuser@CasaOS:~/atest$ gdb test
GNU gdb (Debian 13.1-3) 13.1
Copyright (C) 2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "arm-linux-gnueabihf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...
(No debugging symbols found in test)
(gdb) break *0x10064
Breakpoint 1 at 0x10064
(gdb) run
Starting program: /home/testuser/atest/test
Breakpoint 1, 0x00010064 in _start ()
(gdb) info registers r0
r0 0x3 3
(gdb)
動作環境(Linux / ARM 版)
OS: Armbian 23.08.0-trunk (Debian 12 Bookworm)
Kernel: Linux 6.1.38-meson (armv7l)
CPU: ARM Cortex-A5, 4 cores, Little Endian
Architecture: armv7l
GNU assembler: 2.40 (arm-linux-gnueabihf) using BFD version (GNU Binutils for Debian) 2.40
GNU ld: (GNU Binutils for Debian) 2.40
GDB: GNU gdb (Debian 13.1-3) 13.1
備考: Windows10からSSH接続して使用