This is just a list of some basic ASM instructions and C functions that are useful when getting started with assembly. Instructions that require additional values in registers will have a list of required values. The list is still being worked on. For more instructions one should use a NASM manual, reference or something similar. Once you understand the basics it isn’t hard to look at the manual for new instructions. Note that the wording and terminology might be wrong here and there since I am still a beginner at this.
| extern <function> | Import an external (C) function. |
| section <section_type> | A section – it can be an executable code section (.text), a data section (.data) or a portion of assembly code that contains statically allocated variables (.bss). |
| global <symbol> | NASM specific instruction – export symbols in your code to where it points in the object code generated (in Linux that would be for example main). It basically tells the kernel where the entry point of the program is, where it starts. |
| mov <to>, <from> | Move data to a register from a register/value/variable/location. When referring to data in the .bss section, surround the variable names with square brackets (mov rax, [N]), Also when moving data inside of the .bss variable add the data type before it (mov qword [1], 1). |
| syscall | Do a system call. The required register values below are for the print system call. More can be found here. Rax = <System call number> Rdi = <Parameter for operation> Rsi = <Message address> Rdx = <Message length [bytes]> |
| xor <reg_a>, <reg_b> | Xor operation on two registers. |
| <label_name>: db <data>, <more_data> | Save data in the data section – db is the byte data type. |
| <label_name>: equ $-msg | Save the length of the message in the data section. |
| .<decl_name> | Declaration – defines a part/block of code, we can use it to jump to it – it works in tandem with the goto instruction (in NASM that is jmp, jg, jl etc.). |
| push <data> | Pushes data on the stack. Often we have to push the rbp register on the stack at the beginning – rbp contains the frame pointer. |
| call <function> | Calls an (external) function. Calls any function and then returns to where the function was called. |
| cmp <reg_a, req_b/var> | Compare two values in registers/variables. |
| jg .<declaration> | Jump to a declaration if a number was greater (a cmp instruction or similar should be used before). |
| jl .<declaration> | Jump to a declaration if a number was lower (a cmp instruction or similar should be used before). |
| jne .<declaration> | Jump to a declaration if a number was not equal (a cmp instruction or similar should be used before). |
| inc <reg/var> | Increase the value of a register/variable by one. |
| dec <reg/var> | Decrease the value of a register/variable by one. |
| jmp .<declaration> | Jump to a specified declaration. |
| pop <reg> | Pop value off a stack to a specified register. |
| ret | Return/exit from the program. A return value should be specified in the %rax register. |
| <var_name> resq <n> | Reserve n-qwords of data for later use. |
| scanf | Input: %rdi – Input format – address to the format string, %rsi – Where to save the value, %rax – Save a 0 here. Output: Variable/register specified in %rsi |
| printf | Input: %rdi – Output format – address to the format string, %rsi – What we are printing out, %rax – Set this to 0. Output: Specified value is printed out on the screen. |
| div <divisor> | Input: %rax – Dividend, Output: %rax – Quotient, %rdx – Remainder. |
| lodsb | Loads a byte from [%rsi] into %al. If the direction flag is set, decrements %rsi, else it increments. |
| stosb | Stores a byte in %al into [%rdi]. If the direction flag is set, decrements %rdi, else increments. |
| cld | Clears direction flag. |
| std | Set direction flag. |
Also useful are the data types in the .data section. Here is a list of them that I found.
- DB – Define Byte – 1 byte
- DW – Define Word – 2 bytes
- DD – Define Double Word (DWord) – 4 bytes
- DQ – Define Quad Word (QWord) – 8 byte
- DT – Define TWord – 10 bytes
- DO / DDQ – Define OWord (DQWord) – 16 bytes
- DY – Define YWord – 32 bytes
- DZ – Define ZWord – 64 bytes
And when it comes to allocating space for variables in the .bss section we have the following commands. The instruction pattern is the same for all of the following instructions: <variable_name> resx <n>, where variable_name is the variable name, resx is one of the listed instructions and n is the number of variables that we allocate space for.
- RESB – 1 byte
- RESW – 2 bytes
- RESD – 4 bytes
- RESQ – 8 bytes
- REST – 10 bytes
- RESO / RESDQ – 16 bytes
- RESY – 32 bytes
- RESZ – 64 bytes
As you can see, the pattern for these instruction and data types is very predictable. If you know the data types (byte, word, dword, …), you pretty much know all of these instructions.