Skip to content

Instantly share code, notes, and snippets.

@5cotts
Created January 4, 2022 21:34
Show Gist options
  • Save 5cotts/6b079908aef52e1ad319e3e3ca157c6f to your computer and use it in GitHub Desktop.
Save 5cotts/6b079908aef52e1ad319e3e3ca157c6f to your computer and use it in GitHub Desktop.
Hello, world! in x 86-64 Assembly Language
; Adapted from:
; https://www.youtube.com/watch?v=VQAKkuLL31g
;
; This will only run on x86_64 GNU/Linux
; Run `uname -a` in terminal to determine what you are running.
;
; To run:
; $ nasm -f elf64 -o hello.o hello.asm
; $ ld hello.o -o hello
; $ ./hello
section .data
text db "Hello, world!", 10
section .text
global _start
_start:
; sys_write(1, text, 14)
mov rax, 1 ; ID 1 in register rax corresponds to sys_write
mov rdi, 1 ; ARG1 for sys_write in register rdi corresponds to standard output (value of 1)
mov rsi, text ; ARG2 for sys_write in register rsi corresponds to the address bytes we want to write
mov rdx, 14 ; ARG3 for sys_write in register rdx corresponds to the length of the string we want to write to standard output
syscall ; Executes the above block of commands
; sys_exit(0)
mov rax, 60 ; ID 60 in register rax corresponds to sys_exit
mov rdi, 0 ; ARG1 for sys_exit in register rdi is the error code (0 is success)
syscall ; Executes the above block of commands

Interpreting text db "Hello, Wolrd!", 10

  • text This is a name assigned to the address in memory that this data is located in. Whenever we use "text" later in the code, when the code is compiled, the compiler will determine the actual location in memory of this data and replace all future instances of "text" with that memory address.

  • "db" Stands for "define bytes". It essentially means that we are going to define some raw bytes of data to insert into our code.

  • "Hello, World!", 10 Above are the bytes of data that we are defining. Each character in the string of text is a single byte. The "10" is a newline character.

Registers

Registers are part of the process that temporarily holds memory.

In the x86_64 architecture, registers holds 64 bits.

This means each register can hold the below values.

Type Min Max
Unsigned 0 18,446,744,073,709,551,616
Signed -9,223,372,036,854,775,808 9,223,372,036,854,775,808

System Call

A system call, or a syscall, is hwhen a program requests a sevice from the kernel.

All syscalls have an ID associated with them (a number).

Syscalls also take arguements, meaning a list of inputs.

Argument Registers
ID rax
1 rdi
2 rsi
3 rdx
4 r10
5 r8
6 r9

System Call List: https://syscalls64.paolostivanin.com/

Let's look at sys_write as an example.

syscall ID ARG1 ARG2 ARG3
sys_write 1 File Descriptor: 0 (Standard Input), 1 (Standard Output), 2 (Standard Error) Buffer (Location of string to write) Count: Length of String

Sections

.data - Where all data is defined before compilation

.bss - Where data is allocated for future use

.text - Where the actual code goes

Labels

Used to lable a part of code.

Upon compilation, the compiler will calculate the location in which the label will sit in memory.

Any time the name of the label is used afterwards. the name is replaced by the location in memory by compiler.

Start Label

The "_start" label is essential for all programs.

When your program is compiled and later executed, it is executed first at the location of "_start"

If the linker cannot find "_start", it will throw an error.

Global

The word "global" is used when you want the linker to be able to know the address of some label.

The object file generated will contain a link to every label declared "global."

In this case, we have the declare "_start" as global since it is required for the code to be properly linked.

Source: https://www.youtube.com/watch?v=BWRR3Hecjao

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment