Arm 64 Assembly

From csn
Jump to navigation Jump to search

The following instructions will work for you on a Raspberry Pi.

You will need to ensure that you have the correct pacakges.

sudo apt install gcc-9-arm-linux-gnueabi

Then we can create some arm code

vim arm_intro.asm

Then insert the following arm code. Note that the commands will be different based on whether you are using 32 bit or 64 bit arm. The following documents describe the differences in the register sets. You can find the 32 bit and 64 bit syscalls here: https://chromium.googlesource.com/chromiumos/docs/+/HEAD/constants/syscalls.md

The 64 bit arm assembly can look like this:

#ARM assembly Tutorial 

.global _start
.section .text

start:
	mov x8, #0x5d
        mov x0, #41
  
        svc 0

.section .data

You can assemble the code with:

as arm_intro.asm -o arm_intro.o
gcc arm_intro.o -o arm_intro.elf -nostdlib

You can then run the code with:

./arm_intro.elf

Then

echo $?

This should return the number 41

A hello world example

Check out the code below for an arm example:

#ARM assembly Tutorial 

.global _start
.section .text

#In linux there are 3 file descriptors
#STDIN - 0
#STDOUT - 1
#STDERR - 2

#We are going to write a string to STDOUT
#If you look at Google's ARM64 system call table then write is 0x40
#We need to set x8, to x40
#We need to set x0, to the file discriptor we are going to write to, which is 1 
#We need to set x1, to the data we are going to write
#We need to set x2, to the length of the data that we write

start:
	mov x8, #0x40 /*to set the register to write */
	mov x0, #1 /* to set the file descriptor to out */
	ldr x1, =message /* to move the data into a register */
	mov x2, #13 /* to set the length of the data in that register */
	svc 0

	mov x8, #0x5d /* to exit cleanly */
	svc 0

.section .data
	message:
	.ascii "Hello, World\n"