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 PortalCOMP3137Defining Data Masm Ass…
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 28 of 73
    Topic Study Note Illustration
    28

    Defining Data in MASM Assembler

    In MASM (Microsoft Macro Assembler) , data is defined in the data segment of a program. The data segment is where you declare and initialize variables, constants,...

    Defining Data in MASM Assembler

    In MASM (Microsoft Macro Assembler), data is defined in the data segment of a program. The data segment is where you declare and initialize variables, constants, and other data types that your program will use during execution. This section is separate from the code segment, which contains the instructions that the CPU will execute.

    In MASM, you use assembler directives to define and initialize data. These directives help the assembler understand how to reserve space in memory for different types of data (such as integers, characters, strings, etc.).

    Key Directives for Defining Data in MASM

    1. .data

      • The .data directive defines the beginning of the data segment. This is where you will declare all your variables, constants, and strings that are used by your program.

      Example:

      .data
      message DB 'Hello, World!', 0  ; Declare a null-terminated string
      num1 DW 10                     ; Declare a word-sized variable with the value 10
      num2 DD 20                     ; Declare a double word-sized variable with the value 20
      
    2. .data?

      • The .data? directive is used to define uninitialized data. These variables will reserve memory space but won't initialize them with any values. This is useful for variables that you will set later in the code.

      Example:

      .data?
      buffer DB 256  ; Declare an uninitialized buffer of 256 bytes
      

    Common Data Types in MASM

    1. DB (Define Byte):

      • DB reserves space for one byte of data. You can also initialize the byte with a value, such as an integer or character.

      Syntax:

      DB value1, value2, ...
      

      Example:

      message DB 'Hello', 0  ; Reserve memory for a string and null-terminate it
      
    2. DW (Define Word):

      • DW reserves space for two bytes (a word). It is commonly used to store 16-bit values.

      Syntax:

      DW value1, value2, ...
      

      Example:

      num1 DW 100   ; Reserve space for a 16-bit number and initialize it with 100
      
    3. DD (Define Double Word):

      • DD reserves space for four bytes (a double word). It is used to store 32-bit values.

      Syntax:

      DD value1, value2, ...
      

      Example:

      num2 DD 2000   ; Reserve space for a 32-bit number and initialize it with 2000
      
    4. DQ (Define Quad Word):

      • DQ reserves space for eight bytes (a quad word). It is used to store 64-bit values (supported on 64-bit systems).

      Syntax:

      DQ value1, value2, ...
      

      Example:

      num3 DQ 5000000000   ; Reserve space for a 64-bit number and initialize it
      
    5. DT (Define Ten Bytes):

      • DT reserves space for ten bytes. This is often used for larger data structures or when you need to store floating-point numbers in some formats.

      Syntax:

      DT value1, value2, ...
      

      Example:

      bigData DT 12345678901234567890   ; Reserve space for ten bytes (larger numbers)
      
    6. RESB, RESW, RESD (Reserve Byte, Word, Double Word):

      • These directives are used when you want to reserve space but not initialize the data. The space is reserved, but no specific values are assigned to it.

      Syntax:

      RESB n     ; Reserve n bytes
      RESW n     ; Reserve n words (n*2 bytes)
      RESD n     ; Reserve n double words (n*4 bytes)
      

      Example:

      buffer RESB 256   ; Reserve space for 256 bytes (uninitialized buffer)
      

    Working with Strings in MASM

    In MASM, strings are usually represented as arrays of characters. Since each character takes 1 byte, you can use the DB directive to define strings.

    Example of a string:

    message DB 'Hello, World!', 0  ; Declare a null-terminated string
    
    • The 0 at the end is a null terminator (character 0), indicating the end of the string. This is a common practice in many programming languages and is needed for functions that handle strings, like printing them to the screen.

    Example of defining multiple strings:

    greeting DB 'Hello', 0
    farewell DB 'Goodbye', 0
    

    Arrays in MASM

    In MASM, arrays are simply sequences of data items, such as bytes, words, or double words, defined using the appropriate data type directive. Arrays are particularly useful when you need to store multiple values of the same type.

    Example of an array of bytes:

    numbers DB 10, 20, 30, 40, 50  ; Declare an array of 5 bytes
    

    Example of an array of words:

    scores DW 100, 90, 85, 95  ; Declare an array of 4 words
    

    Example of an array of double words:

    data DD 1000, 2000, 3000  ; Declare an array of 3 double words (32-bit)
    

    Constants in MASM

    You can also define constants (values that do not change during the program execution) in MASM using the EQU directive. Constants are often used to make your code more readable and easier to maintain.

    Syntax:

    name EQU value
    

    Example:

    PI      EQU 3.14159   ; Define a constant for Pi
    MAXVAL  EQU 100       ; Define a constant for maximum value
    

    You can then use the constant PI and MAXVAL throughout your program as if they were regular values.


    Example Program Using Data in MASM

    Here's a simple example of how data is defined and used in a MASM program:

    .model small       ; Define small memory model
    .stack 100h        ; Define stack size
    
    .data             ; Data segment begins
    message DB 'Hello, MASM!', 0  ; Declare a string with a null terminator
    num1 DW 25                  ; Declare a word (16-bit) variable and initialize it
    num2 DD 1000                ; Declare a double word (32-bit) variable and initialize it
    
    .code             ; Code segment begins
    start:            ; Label for the start of the program
        MOV AX, @data   ; Load address of data segment
        MOV DS, AX      ; Move address into DS register
    
        ; Code to print the string message
        MOV AH, 09h       ; DOS function: display string
        LEA DX, message   ; Load the address of 'message' into DX
        INT 21h           ; Call interrupt to print message
    
        ; Code to perform a simple calculation (num1 + num2)
        MOV AX, num1      ; Load num1 into AX register
        ADD AX, num2      ; Add num2 to AX
        MOV BX, AX        ; Move the result to BX register
        
        ; Program termination
        MOV AH, 4Ch       ; DOS function: exit program
        INT 21h           ; Call interrupt to terminate program
    end start          ; End of the program
    

    Explanation of Example:

    1. .model small: Specifies the memory model (small model, which is typically used for simple programs).
    2. .stack 100h: Specifies the stack size (256 bytes in hexadecimal).
    3. .data: Defines the data section, where the string message and variables num1 and num2 are declared.
    4. .code: Defines the code section, which contains the instructions that will be executed. The program prints the string and performs a simple addition of num1 and num2.
    5. INT 21h: This is a DOS interrupt used for various system services, like printing a string or terminating a program.

    Conclusion

    In MASM assembler, defining data is crucial for storing variables, constants, and strings used by your program. The .data directive defines the data segment, where you can use various directives like DB, DW, DD, and EQU to define different types of data. Understanding how to define and manage data in MASM is a fundamental part of assembly language programming, as it helps structure your program and enables efficient memory management.

    ← Previous topic 27
    MIPS
    Next topic 29 →
    Elements of Assembly Language

    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