ARM 64 Assembly Series — Load and Store

  • The AArch64 architecture supports a single instruction set called A64 which consists of fixed-length 32 bit instructions that can be used to: Load and store data, change the address of the next instruction to be executed, perform arithmetic or logical operations, perform a special operation
  • AArch64 is a load-store architecture, which means that only load and store instructions can access the memory.
  • The load register ldr and store register str instructions are used to transfer: bytes (8 bits), half-words (16 bits), words (32 bits) and double words (64 bits) from a memory address to registers or from registers to a memory address.

Loading and Storing Data

Single register

  • The op refers to the instruction mnemonic, which can be ldr or str (capitalisation is optional)
  • The <sz> refers to the size of the data to be transferred (see below)
  • The Rn refers to the source or destination register
  • The <address> refers to the memory address to which or from the data will be transferred
ldr x1, <address>       //store 64 bits from <address> to X1
str x1, <address> //store 64 bits from X1 to <address>
-----------------ldr w1, <address> //store 32 bits from <address> to w1
str w1, <address> //store 32 bits from w1 to address
ldrb x1,[x2]       //store the least significant byte from *x2 to x1strh x1,[x2],#3    //store a half word (2 bytes) from x1 to *x2 and set x2 = x2 + 3strsh w0,[w3]      //store a half word (2 bytes) from w0 to *w3 and sign extend it 
https://armkeil.blob.core.windows.net/developer/Files/pdf/graphics-and-multimedia/ARMv8_InstructionSetOverview.pdf

Pair of registers

  • Load or store Rn to <address>
  • Increase <address> according to the size of Rn (4 bytes for 32 bit transfer or 8 for 64 bit transfer)
  • Load or store the second register to the (increased) address
ldp w0, w1, [x2]         
stp x29,x30, [sp, #-16]!
ldp x29,x30, [sp], #16
Disassembling a function with Ghidra

Example

qemu-system-aarch64 -m 1024 -M raspi3b -kernel kernel8.img -dtb bcm2710-rpi-3-b-plus.dtb -sd 2022-01-28-raspios-bullseye-arm64.img -append "console=ttyAMA0 root=/dev/mmcblk0p2 rw rootwait rootfstype=ext4" -nographic -device usb-net,netdev=net0 -netdev user,id=net0,hostfwd=tcp::5555-:22
syscall conventions depending on the architecture
0x7ffffffb20: 0x000000000000000a, 0x7ffffffb28: 0x0000000000000014

Food for though

int x[] = {1,2,3,4,5};x[0] = 6;
x[1] = x[2];
x[3] = x[0];
.global _start_start:
ldr r0, =x
@ write your program here.data
x: .word 1,2,3,4,5

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store