Our First Program (PI5)
Our first program will initially do nothing. It will simply run an infinite loop. This serves as a basic building block for all further experiments. I will explain how to create, compile, and run such a program. First, let's check if everything works.
Creating the Source Code
Open a text editor to write our first Assembly program:
//
// The first program
// 01.07.2024 www.satyria.de
//
.section .init // Ensure the linker places this at the beginning of the kernel image
.globl _start // Generates a global label
_start: // The label _start (entry address)
b main // Branch to "main"
.section .text // Instruction to the linker that code is coming
main: // Create the label "main"
mov sp, #0x80000 // Create a stack of 512KB (524288 bytes)
MainLoop: // Infinite loop
b MainLoop
Save the file in the standard directory under Windows: C:\msys64\home\xxx (where xxx is usually your username). On Linux, you can save the file in your home directory. Name the file "first.S". The ".S" extension indicates the file is Assembly source code.
Compiling the Program
Assuming you have set up the programming environment as described in the "Setting Up the Programming Environment" chapter, to compile the program, open "msys2" on Windows (found in the Windows menu). On Linux, open the terminal. Navigate to the home directory if you are not already there. You can do this with the command "cd ~". Use "ls" to display the directory contents. Now, three commands are needed to compile the program:
aarch64-none-elf-as first.S -o first.o
aarch64-none-elf-ld --no-undefined first.o -o first.elf
aarch64-none-elf-objcopy first.elf -O binary kernel_2712.img
aarch64-none-elf-as: Creates an object file (first.o) with the "-o" option. This is the first step, as the assembler might not have all the information yet.
aarch64-none-elf-ld: The linker generates the actual machine code, combines information not declared in the code, and produces the file "first.elf".
aarch64-none-elf-objcopy: Since the Raspberry Pi cannot understand the ELF file in its original state, this command converts the file into a binary format expected by the Raspberry Pi. The result is the file "kernel_2712.img".
Compiling the Program with Make
Previously, I described in "Working with Make and Linker Script" how to write a Makefile. With this Makefile, we can also compile this program if the files "Makefile" and "linker.ld" are present in the directory:
make
You can download the source code as a ZIP file from the following link: https://www.satyria.de/arm/sources/assem/first.zip
| < Back (Working with Make and Linker Script) | < Home > | Next (Making the LED Blink (PI5)) > |