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 PortalCOMP3137Masm
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 26 of 73
    Topic Study Note Illustration
    26

    MASM

    MASM (Microsoft Macro Assembler) is an assembler for x86-based microprocessors developed by Microsoft. It is used to write programs in assembly language , which is a low-level...

    MASM (Microsoft Macro Assembler)

    MASM (Microsoft Macro Assembler) is an assembler for x86-based microprocessors developed by Microsoft. It is used to write programs in assembly language, which is a low-level programming language that is closely related to machine code. MASM is commonly used in Windows environments to create software that directly interacts with the hardware, such as system-level programs, device drivers, and performance-critical applications.

    MASM provides a powerful set of features for writing and managing assembly language programs, offering both macro processing and high-level constructs for ease of development, while still allowing programmers to write programs close to the hardware level.

    Key Features of MASM

    1. Macro Processing: MASM supports macros, which are reusable code fragments that can be defined and expanded during assembly. Macros make it easier to write assembly code, as you can define a set of instructions once and then use them multiple times throughout the program.

    2. Support for High-Level Constructs: MASM allows programmers to use high-level constructs, such as if-else conditions and loops, that simplify the process of writing assembly code. These constructs are then converted to the equivalent low-level assembly instructions during the assembly process.

    3. Windows Integration: MASM is designed to work seamlessly within the Windows environment. It can be used to write programs that interact with the Windows Operating System and supports the creation of Windows applications, DLLs (Dynamic-Link Libraries), and other system-level software.

    4. Linking and Debugging: MASM integrates well with linkers and debuggers, allowing programmers to combine their assembly code with other languages (such as C or C++) and debug their programs efficiently.

    5. Optimized for x86: MASM is optimized for x86 architecture, meaning it generates machine code for Intel processors. MASM also supports 32-bit and 64-bit versions of assembly language, allowing for a wide range of applications from legacy systems to modern high-performance systems.

    6. Assembler Directives: MASM includes several assembler directives that guide the assembly process, including specifying data types, defining variables, and managing memory. For example:

      • .data: Used to define initialized data or variables.
      • .code: Marks the start of the code section.
      • .stack: Defines the stack size.
    7. Built-in Libraries: MASM provides several libraries that offer predefined routines for common tasks such as memory management, string handling, and mathematical computations.


    MASM Syntax

    MASM follows a specific syntax to define various components of an assembly language program. Here’s an overview of some of the key syntax elements:

    1. Comments: Comments in MASM are written using a semicolon (;). Anything following a semicolon on a line is ignored by the assembler.

      ; This is a comment
      MOV AX, 5 ; Load value 5 into AX register
      
    2. Labels: Labels are used to mark specific locations in the code that can be referenced by jump instructions or other parts of the program. A label is typically a name followed by a colon.

      start:   ; Label definition
          MOV AX, 10  ; Move value 10 into AX register
      
    3. Instructions: Instructions are the core elements of MASM code and represent the operations that the CPU will perform. MASM supports a wide range of x86 instructions for tasks like data movement, arithmetic operations, control flow, and more.

      MOV AX, BX  ; Move the value in BX into AX
      ADD AX, 1   ; Add 1 to the value in AX
      
    4. Data Section: The data section is where you define variables and constants. You can define data as byte, word, dword (double word), etc.

      .data
      message DB 'Hello, World!', 0   ; Define a null-terminated string
      num1 DW 10                      ; Define a word variable with value 10
      
    5. Code Section: The code section contains the executable instructions of the program.

      .code
      start:
          MOV AX, 1      ; Load 1 into AX
          MOV BX, 2      ; Load 2 into BX
          ADD AX, BX     ; AX = AX + BX (1 + 2)
          RET            ; Return from the program
      
    6. Procedures: MASM allows the use of procedures (or functions) to organize code into reusable blocks. A procedure is defined with the PROC directive and ends with the ENDP directive.

      sum PROC
          MOV AX, 1    ; Start procedure by loading value
          ADD AX, 2    ; Perform addition
          RET          ; Return from procedure
      sum ENDP
      

    Creating a Simple Program with MASM

    Here is an example of a simple MASM program that adds two numbers:

    .model small     ; Define memory model
    .stack 100h      ; Define stack size
    
    .data           ; Define data segment
        num1 DB 10   ; Define a byte with value 10
        num2 DB 20   ; Define a byte with value 20
        result DB 0  ; Define a byte to store the result
    
    .code           ; Define code segment
    start:          ; Label for the start of the program
        MOV AL, num1   ; Load num1 into AL register
        ADD AL, num2   ; Add num2 to AL
        MOV result, AL ; Store result in the result variable
        
        MOV AH, 4Ch    ; Exit program (DOS interrupt)
        INT 21h        ; DOS interrupt for terminating program
    end start        ; End of the program
    

    Explanation:

    • The .model directive sets the memory model for the program.
    • The .data section defines variables (num1, num2, result) in memory.
    • The .code section contains the actual instructions that the CPU will execute. The program loads the values of num1 and num2 into registers, performs an addition, and stores the result in result.
    • The INT 21h instruction is a DOS interrupt used to terminate the program.

    MASM Assembler Directives

    MASM includes various assembler directives to control the assembly process:

    • .model: Specifies the memory model to use (e.g., small, medium, large).
    • .data: Defines the data segment where variables and constants are stored.
    • .code: Marks the start of the code section.
    • .stack: Defines the size of the stack.
    • .end: Marks the end of the program or procedure.
    • PROC/ENDP: Defines a procedure.
    • EQU: Defines a constant value.
    • DB (Define Byte), DW (Define Word), DD (Define Double Word): Used to define data in memory.

    Example:

    CONST_VAL EQU 100    ; Define a constant value 100
    

    Compiling and Running a MASM Program

    To compile and run a MASM program, you need to follow a series of steps. Here is a typical process:

    1. Write the MASM code: Write the assembly code in a text editor and save it with a .asm extension (e.g., program.asm).

    2. Assemble the code: Use MASM to convert the assembly code into object code. This is done by running the assembler (ml.exe or masm.exe).

      ml program.asm
      
    3. Link the object file: After assembling, link the object file to create an executable file (e.g., .exe).

      link program.obj
      
    4. Run the program: After linking, you can run the resulting executable file from the command prompt or an IDE (Integrated Development Environment).


    Conclusion

    MASM is a powerful tool for writing assembly language programs targeting the x86 architecture. It combines the efficiency and control of assembly language with some high-level features, such as macro processing and structured programming constructs. It is especially useful for low-level system programming, optimizing performance, and interacting directly with hardware.

    ← Previous topic 25
    CPU Connection
    Next topic 27 →
    MIPS

    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