ASSEMBLY LANGUAGE
Get coding on the Raspberry Pi 64-bit
John Schwartzman demos using assembly language code for the 64-bit Raspberry Pi to call Linux kernel services and the C run-time library.
Hello.c is the obligatory first program in C.
OUR EXPERT
John Schwartzman is a long-time engineering consultant to business and government. He also teaches computer science at a local college.
Learning assembly language won’t make you a faster programmer (slower, more likely). However, it just might make you a better programmer. By learning just what a processor can and can’t do, you’re on the way to a deeper understanding of computer science.
Assembly language is a low-level language. It’s specific to a particular processor. You use it to program a specific processor at the hardware level. Compilers understand assembly language, because that’s what they use to create the instructions in high-level languages. Take a C++ compiler: it strings together lots of assembly language instructions to do its work.
Every kind of program ultimately executes machine language on the computer. Assembly language is simply machine language with mnemonics. Mnemonics are names given to machine language instructions, also known as op codes, so that we don’t have to remember hundreds of numeric values. It enables us to write a program using identifiers such as ADD, SUB and MOV.
Raspberry Pis can run in 32-bit mode (AArch32) or 64-bit mode (AArch64) depending on the OS. In this tutorial we’ll look at AArch64 and install a small 64-bit desktop machine on the Pi. The Pi is a Reduced Instruction Set Computer (RISC) that performs smaller and faster operations than a Complex Instruction Set Computer (CISC) like the Intel x86_64.
Making use of the kernel
We’re using Linux and so the assembly language we write will use Linux kernel services. Even when we use the C library (glibc), the library methods we call will, in many cases, be thin wrappers around the Linux kernel services. It’s become obligatory to introduce every new programming language with a program that prints “Hello, world!” to the console, so we’ll start there.
The screenshot (above) shows the program, hello.c and the screenshot (facing page) shows hello.asm, its AArch64 assembly language equivalent. At the command line in your hello working directory, type make release . Make invokes the gcc assembler to create the object file (hello.obj) from the hello.asm file and the LD GNU linker to create the executable (hello) from the object file. It also uses the GCC C compiler to build and link hello.c into the executable file a.out (assembler output).
The code
Get it from linuxformat. com/archives and on the DVD
The command make clean will remove all of the build artifacts from the working directory. The screenshot (page 90) shows the makefile for hello. Each project in this series has a similar makefile and all rely on the bash shell script named maketest.sh in the parent directory. There is also a makefile in the parent directory that will make or clean all of the child projects at once. Execute a.out and hello and satisfy yourself that they do produce the same output. $ make release // Build the program as -o hello.obj hello.asm # assemble ld hello.obj -o hello # link gcc hello.c # compile and link C version (a.out) $ ./a.out // Run the C executable $$$ Hello, world! $ ./hello // Run the ASM executable Hello, world!