‘Hello World!’ in ARM assembly
来源:互联网 发布:中国农大网络远程教育 编辑:程序博客网 时间:2024/05/17 07:09
‘Hello World!’ in ARM assembly
14. 一月 2012 22:29Over the last few weeks, in an effort to port a small C library to the platform, I’ve been doing a fair bit of tinkering around with the Android NDK. The NDK is primarily intended to allow Android developers to write performance-critical portions of their apps in native C or C++, which interface with the Android Java API through JNI. As the C library in question required porting some x86 SIMD assembly, I figured it would be helpful for me to get to know the bare bones of the ARM architecture. As a means to this end, we can use the NDK’s cross-compiler as a standalone tool to write a simple ‘Hello World!’ console “app” in ARM assembly. As Android is effectively Linux under the hood, we can apply our x86 Linux assembly programming skills to the ARM platform.
First things first: ‘Hello World!’ in x86 assembly
Briefly, the method involves invoking system calls by talking directly to the underlying Linux kernel. An example of how to do this in x86 assembly is given here. As ARM uses a different ABI to x86 (registers are named different things, for a start), this code needs a tiny bit of modification.
Firstly, notice that system calls are identified numerically – in the x86 example, #1 refers to exit() and #4 refers to write(). To invoke a system call, we put its identifier in the EAX register, pass (up to 6) arguments in EBX, ECX, EDX, ESI, EDI, EBP, respectively, and interrupt 0x80 is generated. This is described in further detail here. In contrast, the ARM ‘EABI’ calling convention uses a different method which is described vaguely in these patch notes. We can glean that, on ARM, the system call identifier is put in register R7, arguments are passed in R0-R6 (respecting “EABI arrangement” where appropriate, i.e. 64-bit arguments), and the kernel is called with the ‘SWI 0’ instruction.
Secondly, as they are not guaranteed to be the same on each platform, we must look up the system call identifiers for exit() and write(). For this we refer to the Linux kernel source – $LINUX_SOURCE_ROOT/arch/arm/include/asm/unistd.h, specifically. As it turns out, these two system calls do have the same identifiers on both x86 and ARM platforms.
To ARM
So our assembly code, in GAS syntax, looks very much like (see inline comments for details):
Assembling
Save the above as hello.S and run it through the GNU cross-assembler provided with the NDK. I will assume that you have the prebuilt NDK toolchain directory in your PATH (in my case here, /Users/peterdn/android-ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/darwin-x86/bin):
Deploying to Android
For many, the easiest way to test the above binary is by deploying it to an Android device with an ARM processor. This also means we can take advantage of the insanely useful ‘adb’ tool. If you happen to be running bog-standard Linux on an ARM device, the binary should still run, providing your kernel supports the newer EABI (I believe 2.6.15 and above).
To deploy and test on Android, simply run:
It is also possible to run the binary locally on your device using the Android Terminal Emulator, as below:
Enjoy!
Currently rated 4.0 by 3 people
上一篇:I need to learn ARM assembly, and I use Linux.
下一篇:An exploration of ARM assembly language
- SHTML是什么_SSI有什么用...
- shell中字符串操作
- 卡尔曼滤波的原理说明...
- 关于java中的“错误:找不到或...
- shell中的特殊字符
- linux dhcp peizhi roc
- 关于Unix文件的软链接
- 求教这个命令什么意思,我是新...
- sed -e "/grep/d" 是什么意思...
- 谁能够帮我解决LINUX 2.6 10...
- ‘Hello World!’ in ARM assembly
- ‘Hello World!’ in ARM assembly
- Linux Assembly "Hello World" Tutorial, CS 200
- Hello world in Java
- Hello World in ODE
- Hello Gtk+ in Linux Assembly Language
- Hello World(Java) in Eclipse
- Hello World(C++) in Eclipse
- puts "Hello world" in VC++
- Hello World in D Language
- 一 Hello World in ABAP
- Hello world in a container
- Eclipse Plug-in Hello world
- ARM assembly in Android Apps(1)
- linux arm programming in assembly language
- ARM开发板上的Hello World
- 一个Hello World的Arm汇编指令
- arm assembly
- C#委托的介绍(delegate、Action、Func、predicate)
- Using QEMU for Embedded Systems Development, Part 3
- I need to learn ARM assembly, and I use Linux.
- 290. Word Pattern
- easy ui 隐藏textbox
- ‘Hello World!’ in ARM assembly
- 虚拟机中PXE-MOF:Exiting intel PXE ROM.Operating system not found
- hibernate 4.3.X遇到的一些问题收集
- [leetcode] 339. Nested List Weight Sum 解题报告
- An exploration of ARM assembly language
- java 抽象方法
- Android自定义View小结篇
- A number of problems from coded in ARM assembly language Problems
- svn