A First ARMv8 Assembly Program
Notes
- Again, you know how to write assembly code.
- But
- We will be using a different assembler (or just gcc)
- And a different computer (Archimedes)
- And a different assembly language
- Hello World
- name it hello.s
- When gcc sees a .s extension, it knows it is assembly code
-
.global main .data message: .asciz "Hello World!\n" .text main: sub sp, sp, #16 stur x29, [sp, #0] stur x30, [sp, #8] mov x29, sp ldr x0, =message bl printf mov w0, #0 ldur x29, [sp, #0] ldur x30, [sp, #8] add sp, sp, #16 ret
- name it hello.s
- Not much new here
-
.global mainbecause we are using the c compiler -
message: .asciz "Hello World!\n"- z means the compiler should terminate with a 0, that is nice
- \n as part of the string, that is nice.
-
sub sp, sp, #16- Constants start with a #
- Typing I seem to be messing up constantly and putting a $, just be warned.
- Stack grows down
- sp ← sp - 16
-
stur x29, [sp, #0]- stur : store register
- x29 is the source in this case, a change form the normal
- [sp, #0]: sp is the base address, #0 is the offset
- M[sp + 0= ← x29
- This is equivalent to pushing the frame pointer
- ARM does not have a push or pop instruction
- Push the link register
- In a second
bl=call - But puts the PC in x30, so we need to save this to know where to return.
- In a second
-
mov x29, sp- mov is an assembler pseudo-instruction
- Make the fp be the sp
-
ldr x0, =message- Load x0, the first parameter with the address of the message
-
bl printf- Go to the subroutine printf
- Store the PC in x30
-
ldur x29, [sp, #0]- x29 ← M[sp + 0];
- Restore the frame pointer
- Restore the link register too!
-
add sp, sp, $16- sp ← sp - 16
-
ret- pc ← x30
-
- The Makefile:
OBJS = hello ASFLAGS = -g all: ${OBJS} clean: rm -f ${OBJS} - Note the ASFLAGS for debugging