9. AArch64
This lab represents a short interlude from our hardware focus. For the time being we’ll shift our attention to the AArch64 Instruction Set Architecture (ISA). An AArch64 instruction can be written in two ways:
Through human-readable assembly code; or
Through machine code which is understood directly by the processor.
In this lab we’ll first get a feeling for the ISA by writing a few simple functions in assembly code. Once this is accomplished, we’ll learn how to directly use and understand machine code in Section 10. Understanding machine code is not only helpful but crucial when studying the structure of processors.
9.1. Copying Data
AArch64 is a load-store architecture (see Fig. 9.1.1). This means that instructions either perform memory accesses or operate on data in registers. Note that an instruction may not do both, i.e., access memory and process data, at the same time.
A memory access instruction transfers data from memory to the registers (load) or transfers data from the registers to memory (store). In this task we’ll copy data which is located at one memory location to another memory location. Since we cannot directly move data between two memory locations, we first load the data from the first location to the registers and then write it back to the target memory location. For this task we’ll use AArch64’s general purpose registers which are shown in Fig. 9.1.1.
Optional Note
Certain recent extensions of the Arm architecture violate the concept of a strict “load-store architecture” 🙄. One such example is the LDADD instruction which loads data from memory, adds a value in a register to it, and writes the result back to memory.
#include <cstdint>
#include <cstdlib>
#include <iostream>
extern "C" {
void copy_c( uint64_t const * i_a,
uint64_t * o_b );
// TODO: uncomment
//void copy_asm( uint64_t const * i_a,
// uint64_t * o_b );
}
int main() {
uint64_t l_a[7] = { 1, 21, 43, 78, 89, 91, 93 };
uint64_t l_b_0[7] = { 0 };
uint64_t l_b_1[7] = { 0 };
// copy_c
std::cout << "### calling copy_c ###" << std::endl;
copy_c( l_a,
l_b_0 );
for( unsigned short l_va = 0; l_va < 7; l_va++ ) {
std::cout << l_a[l_va] << " / " << l_b_0[l_va] << std::endl;
}
// copy_asm
std::cout << "### calling copy_asm ###" << std::endl;
// TODO: uncomment
// copy_asm( l_a,
// l_b_1 );
for( unsigned short l_va = 0; l_va < 7; l_va++ ) {
std::cout << l_a[l_va] << " / " << l_b_1[l_va] << std::endl;
}
return EXIT_SUCCESS;
}
#include <stdint.h>
void copy_c( uint64_t const * i_a,
uint64_t * i_b ) {
uint64_t l_tmp_0 = i_a[0];
uint64_t l_tmp_1 = i_a[1];
uint64_t l_tmp_2 = i_a[2];
uint64_t l_tmp_3 = i_a[3];
uint64_t l_tmp_4 = i_a[4];
uint64_t l_tmp_5 = i_a[5];
uint64_t l_tmp_6 = i_a[6];
i_b[0] = l_tmp_0;
i_b[1] = l_tmp_1;
i_b[2] = l_tmp_2;
i_b[3] = l_tmp_3;
i_b[4] = l_tmp_4;
i_b[5] = l_tmp_5;
i_b[6] = l_tmp_6;
}
.text
.align 4
.type copy_asm, %function
.global copy_asm
copy_asm:
// TODO: Implement copy_asm
ret
.size copy_asm, (. - copy_asm)
The code in Listing 9.1.1 and Listing 9.1.3 provides the required boilerplate for your kernel.
Further, a reference implementation of the copy function in C is given in Listing 9.1.2.
Your task is to copy Listing 9.1.1’s seven 64-bit unsigned integer in array l_a
to array l_b_1
by implementing the function copy_asm
in assembly language.
Note
Use the instructions LDR (immediate) and STR (immediate) for the loads and stores in your implementation.
Do not implement any stack transfers and only use the first 18 general purpose registers, i.e., R0
- R17
to adhere the procedure call standard.
Use the flags -pedantic -Wall -Wextra -Werror
whenever invoking gcc
or g++
.
Do this not only here but in all tasks.
Tasks
Implement the function
copy_asm
in the filecopy_asm.s
. Use the template in Listing 9.1.3 for your implementation. Follow the ideas of the C implementation in Listing 9.1.2, i.e., do not use any loops in your code.Compile the C kernel
copy_c
given in Listing 9.1.2 using the optimization flag-O2
. Disassemble the compiler-generated machine code. Briefly explain the obtained assembly code.Implement a new function
copy_asm_loop
in the filecopy_asm.s
. In this implementation use a loop to copy the seven values.
9.2. Adding Two Arrays
Great, we are able to move data from A to B. Even better if we could process our data, don’t you think? Let’s do another simple example for this!
Assume that you have two memory addresses which are stored in the pointers l_a
and l_b
.
Each address is the start of some 64-bit unsigned integer values consecutively stored in memory.
For example, if you have 10 values, each array is 10 64 bits = 640 bits large.
This is the same as 80 bytes per array or 160 bytes for all values together.
Now, our goal is to add the values in the two arrays l_a
and l_b
, and store the result at a third location in memory.
Getting the data into the general purpose registers and back to memory is simple, we just programmed a kernel for this in Section 9.1.
The only missing piece of the puzzle is an instruction which processes the data and effectively adds the values in two general purpose registers.
For this, we once again have a look at the base instructions of the ISA.
ADD (shifted register) is a suitable instruction.
#include <cstdint>
#include <cstdlib>
#include <iostream>
extern "C" {
void add_c( uint64_t i_n_values,
uint64_t const * i_a,
uint64_t const * i_b,
uint64_t * o_c );
void add_asm( uint64_t i_n_values,
uint64_t const * i_a,
uint64_t const * i_b,
uint64_t * o_c );
}
int main() {
uint64_t l_n_values = 10;
// init pointers
uint64_t * l_a = nullptr;
uint64_t * l_b = nullptr;
uint64_t * l_c_0 = nullptr;
uint64_t * l_c_1 = nullptr;
// allocate memory
l_a = (uint64_t *) new uint64_t[ l_n_values ];
l_b = (uint64_t *) new uint64_t[ l_n_values ];
l_c_0 = (uint64_t *) new uint64_t[ l_n_values ];
l_c_1 = (uint64_t *) new uint64_t[ l_n_values ];
// init arrays
for( std::size_t l_va = 0; l_va < l_n_values; l_va++ ) {
l_a[l_va] = l_va;
l_b[l_va] = l_va*2;
l_c_0[l_va] = 0;
l_c_1[l_va] = 0;
}
// add_c
std::cout << "### calling add_c ###" << std::endl;
add_c( l_n_values,
l_a,
l_b,
l_c_0 );
for( std::size_t l_va = 0; l_va < l_n_values; l_va++ ) {
std::cout << l_a[l_va] << " / " << l_b[l_va] << " / " << l_c_0[l_va] << std::endl;
}
// add_asm
std::cout << "### calling add_asm ###" << std::endl;
add_asm( l_n_values,
l_a,
l_b,
l_c_1 );
for( std::size_t l_va = 0; l_va < l_n_values; l_va++ ) {
std::cout << l_a[l_va] << " / " << l_b[l_va] << " / " << l_c_1[l_va] << std::endl;
}
// free memory
delete [] l_a;
delete [] l_b;
delete [] l_c_0;
delete [] l_c_1;
return EXIT_SUCCESS;
}
#include <stdint.h>
void add_c( uint64_t i_n_values,
uint64_t const * i_a,
uint64_t const * i_b,
uint64_t * o_c ) {
for( uint64_t l_va = 0; l_va < i_n_values; l_va++ ) {
uint64_t l_tmp_a = i_a[l_va];
uint64_t l_tmp_b = i_b[l_va];
uint64_t l_tmp_c = l_tmp_a + l_tmp_b;
o_c[l_va] = l_tmp_c;
}
}
Once again, to supercharge your coding, a template for the required C++ driver is given in Listing 9.2.1. Further, a reference C implementation of the addition kernel is given in Listing 9.2.2. Thus, the only missing part is the assembly kernel: Time to get to work!
Tasks
Implement the function
add_asm
in assembly language and use the fileadd_asm.s
for your implementation. Follow the ideas of the C implementation in Listing 9.2.2.Compile the C kernel
add_c
given in Listing 9.2.2 using the optimization flag-O2
. Disassemble the compiler-generated machine code. Briefly explain the obtained assembly code!
9.3. Computing Fibonacci Numbers
Let’s program something useful for a change 😂. The Fibonacci numbers are given by the following sequence:
Listing 9.3.1 provides the usual C++ driver. As shown in line 6 and 7, the C and assembly functions take the id as input and return the respective Fibonacci number, i.e., . Once again, we’ll get started by implementing a C function which is somewhat close to assembly code. This will then be our recipe for the assembly variant.
1#include <cstdint>
2#include <cstdlib>
3#include <iostream>
4
5extern "C" {
6 uint64_t fibonacci_c( uint64_t i_id );
7 uint64_t fibonacci_asm( uint64_t i_id );
8}
9
10int main() {
11 uint64_t l_id = 5;
12 uint64_t l_number_0 = 0;
13 uint64_t l_number_1 = 0;
14
15 // fibonacci_c
16 std::cout << "### fibonacci_c ###" << std::endl;
17 l_number_0 = fibonacci_c( l_id );
18
19 std::cout << "id / number: " << l_id << " / " << l_number_0 << std::endl;
20
21 // fibonacci_asm
22 std::cout << "### fibonacci_asm ###" << std::endl;
23 l_number_1 = fibonacci_asm( l_id );
24
25 std::cout << "id / number: " << l_id << " / " << l_number_1 << std::endl;
26
27 return EXIT_SUCCESS;
28}
Tasks
Implement the reference version
fibonacci_c
in the filefibonacci_c.c
. Try to keep your implementation close to what you would do in assembly language.Implement the assembly version
fibonacci_asm
in the filefibonacci_asm.s
. Keep your implementation dynamic, i.e., the function should accept as input argument. This is also underlined by the function declaration’s argumentuint64_t i_id
in line 7 of Listing 9.3.1.Hint
Keep in mind the procedure call standard, i.e., the compiler will make the input
i_id
available inX0
. You have to return theuint64_t
result inX0
as well.