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

    Practice on MASM

    7 minread
    1,200words
    Intermediatelevel

    Practice on MASM (Microsoft Macro Assembler)

    MASM (Microsoft Macro Assembler) is a powerful tool for writing and assembling low-level assembly language code for x86 and x64 architectures. Below are various practice examples that demonstrate common concepts and instructions in MASM.


    1. Simple "Hello World" Program

    Let's start with a simple MASM program that outputs "Hello World" to the console.

    MASM Code:

    ; hello.asm
    .model small
    .stack 100h
    
    .data
        helloMessage db 'Hello, World!', 0
    
    .code
    main:
        mov ax, @data       ; Initialize data segment
        mov ds, ax
    
        ; Print "Hello, World!"
        mov ah, 09h         ; DOS function to print string
        lea dx, helloMessage ; Load the address of the string
        int 21h             ; Call DOS interrupt
    
        ; Exit the program
        mov ah, 4Ch         ; DOS function to terminate program
        int 21h             ; Call DOS interrupt
    end main
    

    Explanation:

    1. .model small: Defines a small memory model where both data and code fit within a single segment.
    2. .stack 100h: Reserves 256 bytes of stack memory.
    3. .data: Declares the data segment, where we define the helloMessage string.
    4. .code: Marks the start of the code segment.
    5. mov ax, @data and mov ds, ax: Initializes the data segment.
    6. mov ah, 09h: Prepares to call DOS interrupt 21h to display a string.
    7. lea dx, helloMessage: Loads the address of the string into the DX register.
    8. int 21h: Calls DOS interrupt 21h to print the string.
    9. mov ah, 4Ch and int 21h: Terminates the program.

    To assemble and run:

    1. Write the program in a text file named hello.asm.
    2. Use MASM to assemble: ml hello.asm
    3. Link the program: link hello.obj
    4. Run the resulting executable.

    2. Simple Arithmetic Operations

    This example performs basic arithmetic operations like addition, subtraction, multiplication, and division.

    MASM Code:

    ; arithmetic.asm
    .model small
    .stack 100h
    
    .data
        num1 dw 10         ; Define first number
        num2 dw 5          ; Define second number
        result dw 0        ; Variable to store result
    
    .code
    main:
        mov ax, @data      ; Initialize data segment
        mov ds, ax
    
        ; Load numbers
        mov ax, num1       ; AX = num1 (10)
        mov bx, num2       ; BX = num2 (5)
    
        ; Addition
        add ax, bx         ; AX = AX + BX (10 + 5)
        mov result, ax     ; Store result (15)
    
        ; Subtraction
        mov ax, num1       ; Load num1 again
        sub ax, bx         ; AX = AX - BX (10 - 5)
        mov result, ax     ; Store result (5)
    
        ; Multiplication
        mov ax, num1       ; Load num1 again
        mov bx, num2       ; Load num2 again
        mul bx             ; AX = AX * BX (10 * 5)
        mov result, ax     ; Store result (50)
    
        ; Division
        mov ax, num1       ; Load num1 again
        mov bx, num2       ; Load num2 again
        div bx             ; AX = AX / BX (10 / 5), result in AX, remainder in DX
        mov result, ax     ; Store result (2)
    
        ; Exit the program
        mov ah, 4Ch        ; DOS function to terminate program
        int 21h            ; Call DOS interrupt
    end main
    

    Explanation:

    1. mov ax, num1 and mov bx, num2: Loads num1 and num2 into the registers AX and BX.
    2. add ax, bx: Adds the contents of BX to AX (10 + 5).
    3. sub ax, bx: Subtracts BX from AX (10 - 5).
    4. mul bx: Multiplies AX by BX (10 * 5), storing the result in AX.
    5. div bx: Divides AX by BX (10 / 5). The quotient is in AX, and the remainder is in DX.
    6. The result of each operation is stored in the result variable.

    3. Using Loops in MASM

    This example demonstrates how to use loops in MASM. The loop will count from 1 to 10 and print the number each time.

    MASM Code:

    ; loop.asm
    .model small
    .stack 100h
    
    .data
        msg db 'The number is: $'
        counter db 1          ; Counter starts at 1
    
    .code
    main:
        mov ax, @data         ; Initialize data segment
        mov ds, ax
    
    start_loop:
        ; Print message
        mov ah, 09h           ; DOS function to print string
        lea dx, msg           ; Load the address of the message
        int 21h               ; Call DOS interrupt
    
        ; Print counter
        mov al, counter       ; Load the current counter value
        call PrintNum         ; Call the PrintNum procedure
    
        ; Increment counter
        inc counter           ; Increment counter by 1
        cmp counter, 11       ; Check if counter is 11
        jl start_loop         ; Jump to start_loop if counter < 11
    
        ; Exit program
        mov ah, 4Ch           ; DOS function to terminate program
        int 21h               ; Call DOS interrupt
    
    ; PrintNum procedure
    PrintNum:
        ; Print the number in AL (assuming AL holds a number < 10)
        add al, '0'           ; Convert number to ASCII
        mov dl, al            ; Move number to DL register
        mov ah, 02h           ; DOS function to print a character
        int 21h               ; Call DOS interrupt
        ret                   ; Return from procedure
    end main
    

    Explanation:

    1. counter db 1: Initializes a counter variable starting at 1.
    2. lea dx, msg: Loads the address of the string into the DX register.
    3. int 21h: Uses DOS interrupt 21h to print the string.
    4. The loop is controlled using the cmp instruction (cmp counter, 11), checking if counter is less than 11. If true, it loops again.
    5. The PrintNum procedure is used to print a single number by converting it into ASCII.

    4. Conditional Jumps (If-Else Simulation)

    This example simulates an "if-else" block using conditional jumps.

    MASM Code:

    ; if_else.asm
    .model small
    .stack 100h
    
    .data
        msg_true db 'The condition is true.$'
        msg_false db 'The condition is false.$'
        condition db 1        ; Set the condition to 1 (True)
    
    .code
    main:
        mov ax, @data         ; Initialize data segment
        mov ds, ax
    
        mov al, condition     ; Load the condition value into AL
        cmp al, 1             ; Compare condition with 1
        je true_condition     ; Jump if condition is true (AL == 1)
    
        ; Else part
        mov ah, 09h           ; DOS function to print string
        lea dx, msg_false     ; Load the "false" message
        int 21h
        jmp end_program       ; Jump to end
    
    true_condition:
        ; If part
        mov ah, 09h           ; DOS function to print string
        lea dx, msg_true      ; Load the "true" message
        int 21h
    
    end_program:
        ; Exit the program
        mov ah, 4Ch           ; DOS function to terminate program
        int 21h               ; Call DOS interrupt
    end main
    

    Explanation:

    1. condition db 1: This is a variable that controls the conditional flow. You can change it to 0 for the "false" branch.
    2. cmp al, 1: Compares the value of AL (which contains the condition) with 1.
    3. je true_condition: If AL == 1, jump to the true_condition label to print the true message.
    4. If the condition is false (AL != 1), the program prints the false message.

    5. Function Call Example

    In this example, we'll define a function that adds two numbers and returns the result.

    MASM Code:

    ; function_call.asm
    .model small
    .stack 100h
    
    .data
        num1 dw 5         ; First number
        num2 dw 3         ; Second number
        result dw 0       ; Store result
    
    .code
    main:
        mov ax, @data     ; Initialize data segment
        mov ds, ax
    
        ; Call AddNumbers function
        mov ax, num1      ; Load num1 into AX
        mov bx, num2      ;
    
    Previous topic 58
    Code Examples
    Next topic 60
    Procedures

    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,200
      Code examples0
      DifficultyIntermediate