Computer Systems Experiments 14 | From Assembly To C

Series: Computer Systems Experiments

Computer Systems Experiments 14 | From Assembly To C

Before we start the C coding, let’s review the last experiment by an exercise. Suppose we want to create a blinking digit 1 on the display, can you design a circuit? Of course, we can reuse the code blink.bin from this repo.

This circuit can be designed by,

Similar to experiment 12, we use the following code to transmit the code to the Raspberry Pi.

$ rpi-install.py blink.bin

Seems like you are abused by the ARM assembly language a lot in our previous experiments, right? How about trying a new language called the C language. The C is a more structured language compared with the assembly language with more features and more human-readable. That’s why we are going to use it for the following experiments.

We have known that the machine can understand only the machine code (with 0s and 1s) but it can also be aware that the assembly language can be directly replaced to machine code by a series of corresponding rules (I didn’t use the word “translate” here because it is nothing like translating, just replacing the mnemonics with 0s and 1s).

However, how can we make our machine understand the C language? The answer is the compiler. For C, the commonest used compiler is the GNU Compiler Collection (aka. GCC).

In the blinking Display repo, you can find two files, one is named Makefile and the other is named blink.c . The Makefile file is like a bash script file setup.sh in the experiment 10 and it includes the information on how to compile the C language script blink.c.

We are not going to tell how it works in this experiment, instead, we are going to directly use the file from the repo and then make it work for our Pi. We are going to look into these files in the next experiment.

Before we compile the file, let’s first use the following command to clean the directory,

$ make clean

to clean the files ending with .o , .elf , and .bin . The rm command is use in the process and it is equivalent to run,

$ rm -f *.o *.elf *.bin

Then we can compile the file by,

$ make

Or,

$ make all

Then we can find that this is equivalent to,

$ arm-none-eabi-gcc -Og -Wall -std=c99 -ffreestanding -c blink.c -o blink.o
$ arm-none-eabi-ld -nostdlib -e main blink.o -o blink.elf
$ arm-none-eabi-objcopy blink.elf -O binary blink.bin

We are going to analyze what’s happening in the next experiment.

So finally, let’s run,

$ make install

to install the .bin script on our Pi. You can find out that this is equivalent to,

$ rpi-install.py blink.bin