<aside> 💡
This course is made for machines running on Linux. It is best practice to use a VM or any machine with the OS (Operating System) Linux on it. The following text is to explain why, if you don’t care just skip ahead.
There are 2 major x86 assemblers, NASM (Netwide Assembler) and MASM (Microsoft Macro Asssembler), their main function being to translate human-readable assembly language source code into machine code that can be directly executed by a computer's CPU.
In order to invoke the OS to access kernel level tools (I/O, file management, process control,…), we use system calls. But different OS mean different system calls. The ones you’ll encounter on internet and in your courses will most of the time be from Linux. Hence our decision to use NASM over MASM, and so a machine with Linux.
</aside>
How to install a Virtual Machine with Ubuntu on it
How to edit a file in a terminal
There are 2 major sections in an Assembly program : the _start and the .data.
The _start section speaks for itself : its where the code starts.
The .data section will be used to initialize constants before the _start section runs.
However, in order for the _start to be visible to the linker, the program that combines your assembled code into an executable file, it has to be set global (more on that in the next section).
The skeleton of any Assembly code should then be :
.global _start
.section data
; where we initialize constants
_start:
; where our program's main logic will reside
The example up-above works fine only if we’re using the GNU assembler. This backbone is quite versatile depending on which assembler you are using. The core difference is that GNU uses an AT&T syntax, whereas NASM uses an Intel syntax (more on that later). For instance, when using GNU, in-line comments are made using a semi-colon; , but with NASM, we use double-slashes //.
Another issue applies and it’s the use of C standard libraries. We’ll see later why we need them but in order to use them we need a special syntax close to C.
When using NASM, the key difference from our previous examples is that we're using main instead of _start and ending with ret instead of the exit syscall. This is because we're linking with the C standard library, which provides its own startup code that calls our main function and handles the program exit.
The backbone for a program assembled using NASM and laid out for a C standard library usage should then look like this :
global main
.section data
// where we initialize constants
section .text
main:
// where our program's main logic will reside
Since we can use standard libraries more easily with NASM, we’ll use it for the rest of this course.