Assembly is an extremely low-level human-readable language that has a strong relation between the code and the corresponding machine code.
Compilation and Linking
For more specifics, visit the OSX and Windows assembly pages.
You will first need a program to compile your assembly into machine code. Most common is the program nasm for Unix systems and masm for Windows systems.
After you have written your code, you will need to assemble your code, translating it from the human readable assembly language into a file object.
You will then need to link your newly created output file. This will pull all the needed libraries into a single executable.
Syntax and Keywords
||x ← y|
||x ← x & y|
||x ← x | y|
||x ← x ^ y|
||x ← x + y|
||x ← x – y|
||EAX ← EAX * x|
||EAX ← EAX ÷ x|
||x ← x + 1|
||x ← x – 1|
||When this line is reached, jump to the line labeled label|
||Invoke an operating system routine|
||A pseudo-instruction that declares bytes that will be in memory when the program runs|
A section is an assembler directive that tells the assembler what kind of code follows. Generally, you put code in a section called
.text and your constant data in a section called
32-Bit General Purpose
|EAX||Accumulator Register||calculations for operations and results data|
|EBX||Base Register||pointer to data in the DS segment|
|ECX||Count Register||counter for string and loop operations|
|EDX||Data Register||input/output pointer|
|ESI||Source Index||source pointer for string operations|
|EDI||Destination Index||destination pointer for string operations|
|ESP||Stack Pointer||stack pointer, should not be used|
|EBP||Base Pointer||pointer to data on the stack|
|CS||Code Segment||where instructions being executed are stored|
|DS, ES, FS, GS||Data Segment||data segment|
|SS||Stack Segment||where the stack for the current program is stored|
|EFLAGS||Code Segment||status, control, and system flags|
|EIP||Instruction Pointer||offset for the next instruction to be executed|
Carry Flag (
The carry flag is set to 1 (true) when the second operand of an operation is smaller than the first. For example if you
cmp 2 1, the result will be a
CF of 1 (true), since 1 - 2 goes negative, meaning that the first operand is larger than the first.
Overflow Flag (
The overflow flag will be set to 1 (true) if the operation overflows beyond the bounds of the integers and their storage. This could happen if you are dealing with numbers that go beyond the boundaries involved (e.g. adding two numbers that go above 256 with an 8-bit unsigned integer, or adding two 8-bit unsigned integers that go beyond 128).
Sign Flag (
The sign flag will be set to 1 (true) after an arithmetic operation when the result is negative.
Zero Flag (
The zero flag is set after an arithmetic operation and will have a value of 1 (true) when the result is zero, and 0 (false) when the result is not zero.
The stack is used to invoke a function or as temporary storage by pushing parameters in last in first out to be utilized by the function.
This will push the number 3 onto the stack and then write it to the EAX register.
push 3 pop eax ret
This will push three values to the stack in reverse order of what the function expects, because LIFO, and then invokes the function, which will use those values.
push [var] ; Push last parameter first push 216 ; Push the second parameter push eax ; Push first parameter last call _myFunc ; Call the function (assume C naming) add esp, 12 ; Flush the three 4 byte values on the stack by ; adding 12 to the stack pointer
Last modified: 202107112229