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 PortalCOMP3137Practice Masm
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 59 of 73
    Topic Study Note Illustration
    59

    Practice on MASM

    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...

    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 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