Assembly Programming Language Cheatsheet

Assembly language is a low-level programming language that is closely tied to the architecture of a computer’s central processing unit (CPU). It is a symbolic representation of machine code, allowing programmers to write code that is more readable than machine code but still closely related to the hardware. Assembler is the tool used to convert assembly language code into machine code. This cheatsheet serves as a quick reference guide for those diving into the world of assembly programming.

1. Basic Structure

section .data
    ; Data segment - declare variables

section .text
    global _start
    ; Code segment - start of the program

_start:
    ; Main entry point
    ; Program instructions go here

section .bss
    ; Uninitialized data segment - reserve space for variables

2. Registers

; General-purpose registers
eax, ebx, ecx, edx

; Index registers
esi, edi

; Base pointer and stack pointer
ebp, esp

; Flags register
eflags

3. Data Movement

mov destination, source   ; Move data from source to destination

4. Arithmetic and Logic

add destination, source   ; Add source to destination
sub destination, source   ; Subtract source from destination
mul source                ; Multiply destination by source
div source                ; Divide destination by source
and destination, source   ; Bitwise AND
or destination, source    ; Bitwise OR
xor destination, source   ; Bitwise XOR

5. Control Flow

jmp label          ; Jump to label unconditionally
je label           ; Jump if equal (ZF=1)
jne label          ; Jump if not equal (ZF=0)
jl label           ; Jump if less than (SF ≠ OF)
jg label           ; Jump if greater than (ZF=0 and SF=OF)
call subroutine    ; Call a subroutine
ret                ; Return from subroutine

6. Stack Operations

push value         ; Push value onto the stack
pop destination    ; Pop value from the stack into destination

7. Procedure Declaration

section .text
global my_function   ; Declare a global function

my_function:
    ; Function code goes here
    ret

8. Input/Output

section .data
    output db 'Hello, World!', 0

section .text
    ; Output to console
    mov eax, 4            ; Syscall number for sys_write
    mov ebx, 1            ; File descriptor (1 for stdout)
    mov ecx, output       ; Pointer to the message
    mov edx, 13           ; Length of the message
    int 0x80              ; Call kernel

    ; Exit program
    mov eax, 1            ; Syscall number for sys_exit
    xor ebx, ebx          ; Exit code 0
    int 0x80              ; Call kernel

9. Macros

%macro my_macro 2
    ; Macro code with %1 and %2 as parameters
%endmacro

10. System Calls

section .text
    mov eax, 4       ; Syscall number for sys_write
    mov ebx, 1       ; File descriptor (1 for stdout)
    mov ecx, message ; Pointer to the message
    mov edx, length  ; Length of the message
    int 0x80         ; Call kernel

This cheatsheet provides a glimpse into the essential aspects of assembly language programming. As you delve deeper into the world of assembly, you’ll discover its power and efficiency in working closely with hardware, making it a valuable skill for low-level programming and optimization tasks.

Refer to the official documentation for more in-depth information and examples.

FAQ

1. What is Assembly Language, and why is it important?

Answer: Assembly Language is a low-level programming language that closely represents machine code and is specific to a computer architecture. It is important for tasks requiring fine-grained control over hardware resources, optimization, and interfacing with system-level operations. Assembler converts assembly code to machine code, making it essential for programming at the hardware level.

2. How does Assembly Language differ from high-level programming languages?

Answer: High-level languages are more abstract and portable, designed to be easily understood by humans. Assembly Language, on the other hand, is architecture-specific, closely tied to the CPU, and provides a direct representation of machine code. While high-level languages prioritize readability and portability, assembly offers precise control over hardware resources.

3. What are the key registers in Assembly, and how are they used?

Answer: Assembly uses various registers, including general-purpose registers (e.g., eax, ebx), index registers (e.g., esi, edi), and special-purpose registers (e.g., ebp, esp). These registers store data temporarily during program execution, and their use depends on the specific operation being performed, such as arithmetic, data movement, or control flow.

4. How is memory managed in Assembly Language?

Answer: Assembly Language interacts directly with memory through instructions like mov (move) and push/pop (stack operations). Memory addresses are used to access data, and programmers must manage memory carefully to avoid conflicts and ensure the correct functioning of the program. Understanding memory management is crucial for effective assembly programming.

5. Can Assembly Language be used for modern software development?

Answer: While high-level languages are commonly used for modern software development due to their abstraction and ease of use, Assembly Language is still relevant in certain scenarios. It is often employed for low-level system programming, device drivers, and optimization tasks where direct hardware control is required. While not as common for general application development, it remains a valuable skill in specific domains.