Wednesday, 12 March 2025

Programming using Assembler

In assembly language, a block structure refers to the way instructions, data, and control flow constructs are grouped together logically within a program. While assembly language does not have high-level constructs like loops and functions in the same way as languages like C or Python, programmers use labels, directives, and indentation to create structured and readable code.


Key Elements of Block Structure in Assembly

  1. Data Section
    Defines variables, constants, and memory storage.


    section .data msg db "Hello, World!", 0
  2. Code Section
    Contains the actual instructions executed by the CPU.


    section .text global _start _start: mov eax, 1 ; System call number (sys_exit) xor ebx, ebx ; Exit code 0 int 0x80 ; Invoke system call
  3. Procedures (Subroutines/Functions)
    Blocks of reusable code that can be called from different places.


    my_function: push ebp mov ebp, esp ; Function body mov esp, ebp pop ebp ret
  4. Loops and Conditional Blocks

    • Loops can be implemented using jmp, loop, or cmp followed by conditional jumps.

    mov ecx, 5 ; Counter for loop loop_start: dec ecx jnz loop_start ; Jump if ecx is not zero
    • Conditional blocks use comparison and jump instructions:

    cmp eax, ebx je equal_label ; Jump if equal jg greater_label ; Jump if greater jl less_label ; Jump if less
  5. Stack-Based Block Structure

    • Functions or local variables are often managed using the stack.

    push eax call my_function pop eax

Example of a Structured Assembly Program


section .data msg db "Hello, Assembly!", 0 section .bss buffer resb 16 section .text global _start _start: ; Calling function call print_msg ; Exit program mov eax, 1 xor ebx, ebx int 0x80 print_msg: ; Function to print a message mov eax, 4 mov ebx, 1 mov ecx, msg mov edx, 16 int 0x80 ret

Conclusion

While assembly lacks built-in block structures like {} in C, proper use of labels, indentation, and sections can create a clear and structured program. Using functions, loops, and stack-based organization helps in maintaining better readability and modularity in assembly code.

Variables and Constants in Assembly Language

In assembly language, variables and constants are used to store and reference data in memory. Since assembly does not have high-level variable types like in C or Python, variables are simply named memory locations with specific sizes and values.


1. Variables in Assembly Language

A variable is a memory location that holds a value and can be modified during execution.

Defining Variables

Variables are typically defined in the data section (.data) or the uninitialized section (.bss).

Syntax for Declaring Variables

variable_name data_type initial_value

Common Data Types in Assembly

Data TypeSize (Bytes)Description
db (Define Byte)1 ByteStores a single byte (8-bit)
dw (Define Word)2 BytesStores a word (16-bit)
dd (Define Double Word)4 BytesStores a double word (32-bit)
dq (Define Quad Word)8 BytesStores a quad word (64-bit)

Example of Variable Declaration

section .data num1 db 10 ; 8-bit variable storing 10 num2 dw 1000 ; 16-bit variable storing 1000 num3 dd 123456 ; 32-bit variable storing 123456 message db "Hello, World!", 0 ; String (null-terminated)

Accessing and Modifying Variables

To access a variable, you load its address or value into a register:

mov al, [num1] ; Load 8-bit value of num1 into AL register mov ax, [num2] ; Load 16-bit value of num2 into AX register mov eax, [num3] ; Load 32-bit value of num3 into EAX register

To modify a variable:


mov byte [num1], 20 ; Change value of num1 to 20 mov word [num2], 2000 ; Change value of num2 to 2000 mov dword [num3], 999999 ; Change value of num3 to 999999

2. Constants in Assembly Language

A constant is a value that does not change during execution. Unlike variables, constants are assigned at assembly time and cannot be modified.

Defining Constants

Constants are defined using the equ directive or %define (macro-style).

Using equ (Constant Definition)

PI equ 3.1415 ; Defines a constant named PI SIZE equ 10 ; SIZE is now 10

Usage:

mov eax, SIZE ; Load constant value into register

Using %define (Macro-style Constant)


%define MAX_VALUE 100

Usage:


mov ebx, MAX_VALUE ; MAX_VALUE is replaced with 100 at assembly time

3. Differences Between Variables and Constants

FeatureVariableConstant
ValueCan change during executionFixed at assembly time
StorageAllocates memoryNo memory allocated (replaced during assembly)
DefinitionDefined in .data or .bss sectionDefined using equ or %define
UsageRequires memory accessDirect substitution in instructions

4. Example Program Using Variables and Constants

section .data num1 db 10 message db "Hello, Assembly!", 0 section .bss buffer resb 16 ; Reserve 16 bytes of uninitialized memory section .text global _start %define EXIT_CODE 0 ; Constant definition _start: ; Load variable num1 into AL register mov al, [num1] ; Print message mov eax, 4 ; sys_write mov ebx, 1 ; File descriptor (stdout) mov ecx, message ; Pointer to message mov edx, 16 ; Message length int 0x80 ; Call kernel ; Exit program mov eax, 1 ; sys_exit mov ebx, EXIT_CODE ; Use constant int 0x80 ; Call kernel

Conclusion

  • Variables store values in memory and can be modified during execution.
  • Constants are fixed values replaced at assembly time.
  • Variables use directives like db, dw, dd, while constants use equ or %define.

No comments:

Post a Comment

Desktop Virtualisation

Desktop Virtualization ( DV ) Desktop Virtualization ( DV ) is a technique that creates an illusion of a desktop provided to the user. It d...