ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3137Declaration Initializa…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3137

    Computer Organization and Assembly Language

    Progress0 / 73 done
    Topics73
    1Introduction to Computer Organization2Assembly Language3Comparison of Low-Level and High-Level Languages
    4General Purpose and Special Purpose Registers5Introduction and Usage of RAM6Processor7Registers8System Bus9Instruction Execution Cycle10Assembly and Machine Language11Assembler12Linker and Link Libraries13Programmer's View of a Computer System14RISC and CISC Architecture15Physical Address Calculation16Basic Memory Organization17CPU Organization18Top Level View of Computer Function and Interconnection19Assembler Instruction Cycle20Execute Cycle21Interrupts22Interrupt Cycle23Memory Connection24Input/Output Connection25CPU Connection26MASM27MIPS28Defining Data in MASM Assembler29Elements of Assembly Language30Integer Constants31Integer Expressions32Real Number Constants33Character Constants34String Constants35Reserved Words36Identifiers37Directives38Instructions39The NOP (No Operation) Instruction40Adding and Subtracting Integer41INC and DEC Instructions42NEG Instruction43How to Move Integer Number in Register44Adding and Subtracting Numbers in Registers45Declaration and Initialization of Variables46Moving Data from Variable to Register47Data Definition Statement48BYTE and SBYTE Data49WORD and SWORD Data50Defining DWORD and SDWORD Data51Knowledge about Different Data Types52Operations, Array & Loops53Division and Multiplication in Assembly54Jumps Based on Specific Flags55Jumps Based on Equality56Simple Jump Statements57Jumps Based on Specific Condition58Code Examples59Practice on MASM60Procedures61File Operations Procedures62Labels in Procedures63Stack64Runtime Stack65Conditional Control Flow Directives66Compound Expressions67Data Representation & Conversion68Architecture69Data Path70Control Unit71Critical Path72General Principles of Pipelining73Pipelined Y86 Implementations
    7 min read
    Topic 45 of 73
    Topic Study Note Illustration
    45

    Declaration and Initialization of Variables

    In assembly language, variables are typically declared in a data segment or data section . Unlike high-level programming languages, where variables are explicitly declared with types, assembly...

    Declaration and Initialization of Variables in Assembly Language

    In assembly language, variables are typically declared in a data segment or data section. Unlike high-level programming languages, where variables are explicitly declared with types, assembly language works directly with memory addresses and data manipulation.

    Variables in assembly language are just labels that refer to memory locations where data is stored. The data can be integers, characters, strings, or other types. Initialization refers to assigning a value to a variable, which is done at the time of declaration or through a separate instruction later in the code.

    1. Declaration of Variables

    In assembly language, variables are usually declared in the data segment or section of the program. The data section is where you define constants, strings, and variables. In many assemblers (like MASM, TASM, or NASM), the syntax for declaring variables is straightforward.

    Basic Syntax for Variable Declaration:

    variable_name db  value   ; Declare a byte variable and initialize it with 'value'
    variable_name dw  value   ; Declare a word (2 bytes) variable and initialize it
    variable_name dd  value   ; Declare a double word (4 bytes) variable
    variable_name dq  value   ; Declare a quad word (8 bytes) variable
    
    • db: Stands for "define byte". This is used to declare a single byte (8 bits) variable.
    • dw: Stands for "define word". This is used to declare a 2-byte (16 bits) variable.
    • dd: Stands for "define double word". This is used to declare a 4-byte (32 bits) variable.
    • dq: Stands for "define quad word". This is used to declare an 8-byte (64 bits) variable.

    2. Variable Initialization

    When you declare a variable, you can optionally initialize it with a value. The value can be a literal constant or an expression.

    Example 1: Declare and Initialize a Byte Variable

    section .data
        var1 db 5   ; Declare a byte variable and initialize it with 5
    
    • In this example, var1 is a byte variable initialized with the value 5.

    Example 2: Declare and Initialize a Word Variable

    section .data
        var2 dw 10   ; Declare a word variable and initialize it with 10
    
    • Here, var2 is a word (2 bytes) variable initialized with the value 10.

    Example 3: Declare and Initialize a Double Word Variable

    section .data
        var3 dd 1000   ; Declare a double word variable and initialize it with 1000
    
    • In this example, var3 is a double word (4 bytes) variable initialized with the value 1000.

    3. Different Data Types and Their Sizes

    In assembly language, you have the flexibility to define variables of different sizes to match the data types you need to work with.

    Data Type Size (in bytes) Assembler Syntax Example
    Byte 1 byte (8 bits) db (define byte) db 5 (stores value 5)
    Word 2 bytes (16 bits) dw (define word) dw 10 (stores value 10)
    Double Word 4 bytes (32 bits) dd (define double word) dd 1000 (stores value 1000)
    Quad Word 8 bytes (64 bits) dq (define quad word) dq 123456789 (stores value 123456789)

    Example of Different Variables:

    section .data
        byte_var db 100      ; 1-byte variable with value 100
        word_var dw 500      ; 2-byte variable with value 500
        dword_var dd 1000    ; 4-byte variable with value 1000
        qword_var dq 1000000000 ; 8-byte variable with value 1000000000
    

    4. Initializing Arrays or Multiple Values

    You can initialize multiple values for arrays or sequences of data. This is useful when you need to work with a collection of similar data types, such as an array of integers, characters, or strings.

    Example 1: Initializing an Array of Bytes

    section .data
        byte_array db 1, 2, 3, 4, 5   ; Array of 5 bytes initialized with values 1 to 5
    
    • byte_array is an array of 5 bytes, each initialized with values from 1 to 5.

    Example 2: Initializing an Array of Words

    section .data
        word_array dw 100, 200, 300   ; Array of 3 words (2 bytes each)
    
    • word_array is an array of 3 words, each initialized with values 100, 200, and 300.

    Example 3: Initializing an Array of Doubles (32-bit Integers)

    section .data
        dword_array dd 1000, 2000, 3000   ; Array of 3 double words (4 bytes each)
    
    • dword_array is an array of 3 double words, initialized with 1000, 2000, and 3000.

    5. Using Reserved Space (Uninitialized Variables)

    If you want to reserve space for variables but not initialize them immediately, you can use the resb, resw, resd, and resq directives (reserved space).

    • resb: Reserve byte(s)
    • resw: Reserve word(s) (2 bytes)
    • resd: Reserve double word(s) (4 bytes)
    • resq: Reserve quad word(s) (8 bytes)

    This is useful when you want to allocate memory but fill it in later.

    Example of Reserved Space for Variables:

    section .bss
        var1 resb 1   ; Reserve 1 byte of space for var1
        var2 resw 1   ; Reserve 1 word (2 bytes) of space for var2
        var3 resd 1   ; Reserve 1 double word (4 bytes) of space for var3
    
    • In the .bss section (Block Started by Symbol), the variables var1, var2, and var3 are reserved but not initialized. They will be filled with values at runtime, either by the program or through further instructions.

    6. Using Constants for Initialization

    If you want to initialize variables using constants or symbols, you can define constants in the data section and use them for initialization.

    Example: Using Constants to Initialize Variables

    section .data
        constant_value equ 10   ; Define a constant with value 10
        my_var db constant_value ; Initialize a byte variable with the constant value
    
    • Here, constant_value is a constant that is used to initialize the my_var variable.

    7. String Initialization

    Strings can be initialized in assembly as well, where each character is represented by its ASCII value. Strings are usually stored as a sequence of bytes (using db), with a special null character (0) marking the end of the string.

    Example: Initializing a String

    section .data
        message db 'Hello, World!', 0   ; A null-terminated string
    
    • The string "Hello, World!" is stored as a sequence of ASCII characters, and the 0 marks the end of the string.

    8. Initialization with Equates

    You can use the equ directive to define constants for initialization, making your program more readable and maintainable.

    Example: Using equ to Define Constants

    section .data
        MAX_VALUE equ 100
        MIN_VALUE equ -100
    
        var1 db MAX_VALUE   ; Initialize with the constant MAX_VALUE
        var2 db MIN_VALUE   ; Initialize with the constant MIN_VALUE
    
    • The constants MAX_VALUE and MIN_VALUE are defined using the equ directive and then used for initializing the variables var1 and var2.

    9. Conclusion

    In assembly language:

    • Variables are declared in the data section using directives like db, dw, dd, or dq, which allocate space for variables of specific sizes (byte, word, double word, etc.).
    • Initialization assigns a value to these variables, either through direct constants, expressions, or memory references.
    • Reserved space allows for allocating memory without initializing it immediately, typically using the .bss section.
    • Constants can be used to initialize variables, making the program more readable and flexible.

    Understanding how to declare and initialize variables is crucial for effective assembly programming, especially when working with memory manipulation, data processing, and low-level system programming tasks.

    ← Previous topic 44
    Adding and Subtracting Numbers in Registers
    Next topic 46 →
    Moving Data from Variable to Register

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.2kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Computer Organization and Assembly Language