| 1. | ARM 64-bit Assembly |
| 2. | Simplest programs |
| 3. | Integers |
| 4. | Main memory |
| 5. | Arrays |
| 6. | Branching and loops |
| 7. | Functions |
| 8. | Floating point |
2. Simplest Programs in ARM 64-bit Assembly Language
1. Very simple program
The classic Hello world program is too complex for introduction to assembly language. If we want to make the program complete and if we want to run the program, then the code gets quite long and advanced. In this section we will identify a program that is simpler than the Hello world. We will write that simplest possible program in C. In fact, the program will be so simple that it will be the same in C and C++. Then, we will write the program in assembly language.
1.1. The simplest program in C and C++
Create a file with name simplest.c. The content of the file should be this text:
int main(){
return 7;
}
Open the terminal window, go to the folder that contains the file simplest.c, and run the compiler
gcc simplest.c -o simplestProgramHopefully you didn't make a mistake and the program compiled successfully. You should be able to see the binary called
simplestProgram. The binary can be executed with the command
./simplestProgramNothing visible will happen. However, the program has returned the value
7 to the operating system. More precisely, simplestProgram returned the exit code 7 to terminal, which was the process that called simplestProgram. We can now ask the process terminal to print the value returned by the program. We need to write the following command in the terminal
echo $?Then, the terminal will show the number
7.
1.2. Exit codes
The command return 7; returns a value to the terminal process. That value is called exit code. The terminal process keeps the exit code of the last program that it executed. The code is kept until the next program is executed in the same terminal. The exit code of the last program is displayed with the command echo $?.
Exit code can be any number from the set {0, 1, ..., 255}.
If the number 7 is replaced by -7, then the command echo $? would print 249 (on most unix-based operating systems). The number \(-7\) is received as \(2^8-7=249\).
The programmers have control over which exit code is sent back to the terminal. Although the programs have the ability to send back any of the integers from the interval [0,255], there are some conventions. The exit codes should be signs on whether the programs were successful. The code 0 means that the execution was successful. Non-zero codes are used as indications of errors.
However, in our first programs, we will use exit codes as the primary communication with the operating system. Later, we will learn how to write programs in assembly language that interact with the user in more advanced ways.
1.3. The simplest program in ARM-64 assembly language
Warning: Do not attempt to execute codes from this section on intel-based CPUs. The codes are written in ARM-64 assembly language, which works only on ARM processors. Raspberry Pi 4 has an ARM processor. Your other computers may not.
Create a file called simplest.s and place the following code in the file:
.text
.global main
main:
mov x0, 7
ret
Then, call the compiler with the command
gcc simplest.s -o simplestAsmIf everything went well, then you now have the binary called
simplestAsm. The binary can be executed with the command
./simplestAsmThe execution should not produce any visible result. The exit code can be read with the command
echo $?
1.4. Special role of the register X0
When the program ends, the content of the register X0 becomes the exit code. More precisely, the last 8 bits of the register X0 are the exit code. The operating system will tell you the exit code, if you ask nicely. You need to type the command echo $? in the terminal to get the exit code of the last program.
2. Program that adds numbers 75 and 20
We will now show how the instruction ADD can be used. We will place the numbers 75 and 20 into the registers X1 and X2. Then we will add the contents of X1 and X2. We can put the sum in the register X3. However, we won't see the result, unless we put it into the register X0 just before exiting the program.
.text .global main main: mov x1, 75 mov x2, 20 add x3, x1, x2 mov x0, x3 ret
In order to execute the program from above, we store the source code in the file program02.s. Then, we run the compiler using the command
gcc program02.s -o p02We execute the program using the command
./p02The execution does not print anything. However, it returns the exit code. We read the exit code with the command
echo $?