Part 4 – Linux ARM assembles first program

First 64-bit assembly

Let’s start by creating the first assembler on the first AArch64, the 64-bit code I ran on an ARMv8 server. There is a Chinese Huawei kunpeng/Feiteng, and a non-Chinese ampere or Cavium.

first64.s

.arch armv8-a
.global _start
.text
_start:
	mov x8, 93
	svc 0
Copy the code

Compile:

as -g -o first64.o first64.s

ld -o first64 first64.o

You can run it on an ARMV8 with no output, but echo $? To see the results.

The first 32-bit assembly

Raspberry PI 3B as the environment, this is a 32-bit ARM processor, the following 32 bits of code are running in my raspberry PI.

The first procedure is as follows:

.global main /* 'main' is our entry point and must be global */
 
main:          /* This is main */
    mov r0, #2 /* Put a 2 inside the register r0 */
    bx lr      /* Return from main */
Copy the code
as -o first.o first.s
Copy the code
gcc -o first first.o
Copy the code

Then execute:

./first

Run the echo command to view the result

$echo $?

2

Here BX is branch and Exchange. Branching means we are changing the flow of instructions. The instruction simply leaves the main function, effectively ending our program.

This code can not be compiled through the Palace Museum on amrv8, can not find the bx command.

Makefile

Make the Makefile for subsequent compilation as follows:

# Makefile
all: first
 
first: first.o
	gcc -o $@ $+
 
first.o : first.s
	as -o $@ $<
 
clean:
	rm -vf first *.o
Copy the code