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›MASM
    Computer Organization and Assembly LanguageTopic 26 of 73

    MASM

    7 minread
    1,190words
    Intermediatelevel

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

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