未加星标

Exploring AArch64 assembler Chapter 1

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

AArch64 is a new 64 bit mode that is part of the ARMv8 architecture presented in 2011 by ARM. It has been progressively been deployed in smartphones and servers. So I think it is a good moment to learn a bit more about the assembler of this architecture.

Hardware availability

Single board computers with ARMv6/ARMv7 architecture are easily available nowadays. One of the most popular choices is the Raspberry Pi .

In contrast, single-board computers that support the 64-bit mode of ARMv8 are less common but they are slowly becoming more popular these days. For instance the Pine64 , the ODROID-C2 , the Dragonboard 410c , etc. Any of them will do and in general they differ on the specific System on Chip being used.

Note: the Raspberry Pi 3 has a CPU (Cortex-A53) that implements the 64-bit mode of the ARMv8 architecture and technically could run a 64-bit system. But the software system provided by the Raspberry Foundation is only for 32-bit and there are no official plans for a 64-bit system.

Software alternative

Does this mean that without hardware it is not possible to play with AArch64? No! We can still do many things using a cross-toolchain and QEMU in user mode.

Example for Ubuntu 16.04

Just install QEMU and a cross-toolchain for AArch64.

$ sudo apt-get install qemu-user gcc-aarch64-linux-gnu

Now test you can run a “Hello world” written in C. Create a hello.c file with the following contents.

#include <stdio.h> int main(int argc, char *argv[]) { printf("Hello AArch64!\n"); return 0; }

Now compile it with the cross-compiler for AArch64 that we have installed earlier (the -static flag is important).

$ aarch64-linux-gnu-gcc -static -o hello hello.c

Check it is a AArch64 binary.

$ file hello hello: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, for GNU/Linux 3.7.0, BuildID[sha1]=97c2bc66dbe4393aab9e4885df8e223a6baa235a, not stripped

Trying to run it should fail with some confusing error.

$ ./hello -bash: ./hello: No such file or directory

But we can run it using the QEMU for AArch64 that we installed earlier.

$ qemu-aarch64 ./hello Hello AArch64!

Yay!

Note: If you use this option, remember always to run your programs using qemu-aarch64 .

Our first AArch64 assembler program

Let’s write a very simple program that just returns an error code of two.

// first.s .text .globl main main: mov w0, #2 ret

Let’s assemble it.

$ aarch64-linux-gnu-as -c first.s

And now link it, for convenience we will use gcc .

$ aarch64-linux-gnu-gcc -static -o first first.o

Run it and check the return.

$ ./first $ echo $? 2

Yay!

Let’s go through each line of the code above.

// first.s .text

Line 1 is just a comment with the name of the file used in this example. Line 2 is an assembler directive that means “now come instructions of the program”. This is because we can also express data in an assembler file (data goes after a .data directive).

.globl main

This is another assembler directive that means main is going to be a global symbol. This means that when constructing the final program, this file will have the global main symbol that is needed by the C library to start a program.

main: mov w0, #2 // w0 ← 2 ret // return

This is the entry point of our program. Line 6 itself is just a label for the symbol main (that we mentioned above it was a global symbol). Lines 7 and 8 are two instructions. The first one just sets the register x0 to be 2 (we will see what registers are in the next chapter). The second one returns from the main , effectively finishing our program.

When finishing a program, the contents of the register x0 are used to determine the error code of the program. This is the reason why echo $? above prints 2 .

Reference documentation

Documentation for the AArch64 instruction set can be found in the ARM Architecture Reference Manual ARMv8, for ARMv8-A architecture profile

That’s all for today.

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

主题: Raspberry PiCPULinuxUbuntu
分页:12
转载请注明
本文标题:Exploring AArch64 assembler Chapter 1
本站链接:http://www.codesec.net/view/482794.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(46)