Assembly Basic Syntax

Assembly language is a low-level programming language that’s closely related to a computer’s architecture. It’s essential for understanding the underlying mechanics of a computer and is often used in system programming and embedded systems development. In this article, we’ll explore the basic syntax of assembly language, focusing on how to create variables and the different types of variables used.

The Assembly Language Environment

Assembly language provides a direct representation of a computer’s architecture and operates at a level just above machine code. It uses mnemonics, which are symbolic representations of the processor’s instructions, making it more human-readable than pure machine code.

Creating Variables in Assembly

In assembly language, variables are known as memory locations or data storage. To create a variable, you typically use the RESB, RESW, or RESD directives, depending on the size of the variable you want to allocate.

  • RESB: Reserves a specified number of bytes.
  • RESW: Reserves a specified number of words (16 bits).
  • RESD: Reserves a specified number of doublewords (32 bits).

For example, to create a variable named myVar that occupies 4 bytes of memory, you would use:

myVar RESB 4

This allocates 4 bytes of memory for myVar.

Variable Types in Assembly

Assembly language primarily deals with three types of variables:

  1. Byte (BYTE): A byte is 8 bits and can represent values from 0 to 255. In assembly, you can declare byte variables using the RESB directive.
  2. Word (16-bit, INT, or INTEGER): A word consists of 16 bits and can represent values from -32,768 to 32,767 (signed) or 0 to 65,535 (unsigned). You declare word variables using the RESW directive.
  3. Doubleword (32-bit, DWORD, or LONG): A doubleword consists of 32 bits and can represent values from -2,147,483,648 to 2,147,483,647 (signed) or 0 to 4,294,967,295 (unsigned). You declare doubleword variables using the RESD directive.

Working with Variables

Once you’ve created variables, you can perform various operations on them. This includes loading values into variables, performing arithmetic operations, and using variables in conditional jumps and loops.

Here’s a simple example in x86 assembly language that adds two variables and stores the result in another variable:

section .data
    num1 db 10        ; Declare a byte variable
    num2 db 20        ; Declare another byte variable
    result db 0       ; Declare a byte variable to store the result

section .text
global _start

_start:
    mov al, [num1]   ; Load num1 into AL register
    add al, [num2]   ; Add num2 to AL register
    mov [result], al ; Store the result in the 'result' variable

    ; Exit the program
    mov eax, 1       ; Service to exit
    mov ebx, 0       ; Status code
    int 80h

This simple assembly program loads the values of num1 and num2, adds them, and stores the result in the result variable.

Understanding assembly language syntax and variables is crucial for low-level programming and gaining insights into computer architecture. While the syntax can vary between different assembly languages (x86, ARM, MIPS, etc.), the fundamental concepts of creating and working with variables remain consistent.