Silicon to Scripting: C to assembly
…Bla bla bla, just like how we were able to encode a number as binary, we can encode a lot of other stuff as binary. By assigning a “type” to some data, we know how to intrepet it.
Now that we understand how functions work, we can move on to C.
That also means we will be running our program on an OS, which has a few implications.
OSes manage running (and stopping) multiple programs at once, so they need to know how to run the program.
In C, instead of starting at the first line, the program starts at the main
function.
The most basic C program is
void main() {
}
Since C is a higher level than the assembler can understand, we need to compile it. For Ubuntu, you need to install a C compiler if you don’t already have one.
sudo apt update && sudo apt upgrade
sudo apt install build-essential
To compile the program, run
gcc main.c -o main
./main
This program does nothing, so it’s hard to see that it is actually running. To get some observibility, we can return an integer from the main function called a return code. This signals if the program finished successfully (0) or crashed (anything except 0) to the OS .
int main() {
return 42;
}
In linux, to see the return code of the last command, run
echo $?
If you still saw 0
, you need to recompile your program.
Just because you changed main.c doesnt mean that the compiler will automatically compile it for you.