To compile using nasm
:
nasm -felf64 <fn>.asm && ld <fn>.o -o <fn>
A hello world program, which:
- Writes out the message.
- Exits with status code 0.
The syscall table for x64: https://chromium.googlesource.com/chromiumos/docs/+/master/constants/syscalls.md
A small program which prints out whether the number of CLI arguments is even or odd:
- Pop argc into register.
- Bitwise AND the value with 1.
- Compare the result with 0, and jump to the corresponding label.
- Print the relevant message and exit.
Loops can be achieved through a conditional and some jumps.
Here’s FizzBuzz, compiled with:
nasm -felf64 fizzbuzz.asm && gcc fizzbuzz.o -o fizzbuzz
In main
:
- The loop variable is initialised to 1.
- Evalulate the condition, and jump to
exit
if the loop variable becomes greater than 100.
- Call the
fizzbuzz
function (The body of the loop).
- Increment the loop variable.
- Jump back to the condition.