ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Computer Organization and Assembly Language
    COMP3137
    Progress0 / 73 topics
    Topics
    1. Introduction to Computer Organization2. Assembly Language3. Comparison of Low-Level and High-Level Languages4. Register Types (16-bit): General Purpose and Special Purpose Registers5. Introduction and Usage of RAM6. Processor7. Registers8. System Bus9. Instruction Execution Cycle10. Assembly and Machine Language11. Assembler12. Linker and Link Libraries13. Programmer's View of a Computer System14. RISC and CISC Architecture15. Physical Address Calculation16. Basic Memory Organization17. CPU Organization18. Top Level View of Computer Function and Interconnection19. Assembler Instruction Cycle20. Execute Cycle21. Interrupts22. Interrupt Cycle23. Memory Connection24. Input/Output Connection25. CPU Connection26. MASM27. MIPS28. Defining Data in MASM Assembler29. Elements of Assembly Language30. Integer Constants31. Integer Expressions32. Real Number Constants33. Character Constants34. String Constants35. Reserved Words36. Identifiers37. Directives38. Instructions39. The NOP (No Operation) Instruction40. Adding and Subtracting Integer41. INC and DEC Instructions42. NEG Instruction43. How to Move Integer Number in Register44. Adding and Subtracting Numbers in Registers45. Declaration and Initialization of Variables46. Moving Data from Variable to Register47. Data Definition Statement48. BYTE and SBYTE Data49. WORD and SWORD Data50. Defining DWORD and SDWORD Data51. Knowledge about Different Data Types52. Operations, Array & Loops53. Division and Multiplication in Assembly54. Jumps Based on Specific Flags55. Jumps Based on Equality56. Simple Jump Statements57. Jumps Based on Specific Condition58. Code Examples59. Practice on MASM60. Procedures61. File Operations Procedures62. Labels in Procedures63. Stack64. Runtime Stack65. Conditional Control Flow Directives66. Compound Expressions67. Data Representation & Conversion68. Architecture69. Data Path70. Control Unit71. Critical Path72. General Principles of Pipelining73. Pipelined Y86 Implementations
    COMP3137›Defining Data in MASM Assembler
    Computer Organization and Assembly LanguageTopic 28 of 73

    Defining Data in MASM Assembler

    7 minread
    1,186words
    Intermediatelevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time7 min
      Word count1,186
      Code examples0
      DifficultyIntermediate