Code Restoration Technology ARM Assembly Introduction Tutorial (I) Hello World!
1. Objectives
- Why learn ARM assembly?
No reason.
- Is it useful to learn ARM assembly?
It’s useless
- Can I find a job after learning ARM assembly?
None of the popular large-scale software is written in assembly language, so what do you think?
- Do you still need to learn ARM assembly?
Why not learn it? You have nothing to do anyway. Knowing some low-level languages and getting close to the essence of computer operation is still very cool.
2. Steps
Hello World!
Boss’s father, was also engaged in research and development when he was young. After retirement, he had some free time and planned to practice calligraphy.
One day, the weather was fine and the old boss had finished three rounds of drinks. He was in a relaxed mood and wrote down the following poem on the fine rice paper that the young boss had given him:Hello World!
.text
.globl _start
_start:
mov %r0, $1 // fd 1 (stdout)
ldr %r1, =message
mov %r2, $message_len
mov %r7, $4 // syscall 4 (write)
swi $0
mov %r0, $0 // exit status 0 (ok)
mov %r7, $1 // syscall 1 (exit)
swi $0
.data
message:
.ascii "Hello World!\n"
message_len = . - message
Compile
To compile this ARM assembly source code, there are two options
- Install the compiler on your Android phone.
- Use the Android NDK cross-compilation tool to compile on your computer.
Here we introduce solution 2, cross-compiling on the computer.
My development machine is MacOs 10.14, Android NDK is installed on
/Users/h1yx/Library/Android/sdk/ndk/21.3.6528147
Then under it
toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/
The directory can be found
arm-linux-androideabi-as and arm-linux-androideabi-ld
Windows students should be able to find arm-linux-androideabi-as.exe and arm-linux-androideabi-ld.exe in similar directories
This is the ARM assembly compiler and linker
arm-linux-androideabi-as -o hello.o hello.S
arm-linux-androideabi-ld -o hello hello.o
adb push hello /data/local/tmp/hello
adb shell chmod +x /data/local/tmp/hello
adb shell /data/local/tmp/hello
Perfect, my first ARM assembly program is running, and it is alsoHello World!Boss should have a different understanding from us.
Pay attention
The first word of each line of ARM assembly code is called an assembly instruction.
r0-rX are called registers, which can be understood as variables that come with the CPU. The access speed is much faster than the memory, but the number is limited.
There are three instructions in the hello program: mov ldr swi
- MOV data operation instruction
mov %r0, $1
mov %r2, $message_len
- LDR memory operation instruction
ldr %r1, =message
- SWI soft interrupt instruction
In fact, here we can understand it as a call to the system API.
Call system API write
mov %r7, $4 // syscall 4 (write)
swi $0
The parameters of this write API need to be checked in the system manual. https://chromium.googlesource.com/chromiumos/docs/+/master/constants/syscalls.md