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 PortalCOMP3137Compound Expressions
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 66 of 73
    Topic Study Note Illustration
    66

    Compound Expressions

    In MASM (Microsoft Macro Assembler), compound expressions refer to the use of multiple operands and operators combined in a single instruction or statement. These expressions often involve...

    Compound Expressions in Assembly Language (MASM)

    In MASM (Microsoft Macro Assembler), compound expressions refer to the use of multiple operands and operators combined in a single instruction or statement. These expressions often involve combinations of mathematical, logical, or comparison operations. By using compound expressions, assembly language programmers can perform complex calculations, manipulations, or comparisons efficiently.

    What are Compound Expressions?

    Compound expressions are those that consist of more than one operation or operand combined together. In assembly language, this usually involves:

    • Arithmetic operations (addition, subtraction, multiplication, etc.)
    • Logical operations (AND, OR, NOT, etc.)
    • Comparison operations (equal to, greater than, less than, etc.)

    These expressions typically combine operations to work on multiple values (registers, memory, constants) at the same time.

    Types of Compound Expressions

    1. Arithmetic Compound Expressions
    2. Logical Compound Expressions
    3. Comparison Compound Expressions

    Each of these uses a combination of operands (values or variables) and operators.


    1. Arithmetic Compound Expressions

    In arithmetic compound expressions, multiple arithmetic operations (addition, subtraction, multiplication, division) are combined.

    Basic Arithmetic Operations

    • Addition (+)
    • Subtraction (-)
    • Multiplication (*)
    • Division (/)

    These can be combined in a single statement.

    Example:

    mov ax, 10         ; Load ax with 10
    add ax, 5          ; Add 5 to ax -> ax = 10 + 5 = 15
    sub ax, 3          ; Subtract 3 from ax -> ax = 15 - 3 = 12
    

    In this example, the arithmetic operations are performed in sequence, but you can combine them in more complex expressions:

    Compound Arithmetic Example:

    mov ax, 10         ; Load ax with 10
    mov bx, 5          ; Load bx with 5
    mov cx, 2          ; Load cx with 2
    add ax, bx         ; ax = ax + bx -> ax = 10 + 5 = 15
    mul cx             ; ax = ax * cx -> ax = 15 * 2 = 30
    

    Here, the multiplication operation is combined with addition, so the result is:

    • ax = 15 * 2 = 30

    Complex Arithmetic Compound Expression Example:

    mov ax, 10         ; Load ax with 10
    mov bx, 5          ; Load bx with 5
    mov cx, 3          ; Load cx with 3
    add ax, bx         ; ax = ax + bx -> ax = 10 + 5 = 15
    mul cx             ; ax = ax * cx -> ax = 15 * 3 = 45
    sub ax, 4          ; ax = ax - 4 -> ax = 45 - 4 = 41
    

    In this case, we are performing multiple arithmetic operations on the same register, combining addition, multiplication, and subtraction.


    2. Logical Compound Expressions

    Logical compound expressions combine bitwise logical operations. These operations are often used to manipulate specific bits in a register or memory location. Common logical operations include:

    • AND: Performs a bitwise AND operation.
    • OR: Performs a bitwise OR operation.
    • XOR: Performs a bitwise XOR operation.
    • NOT: Performs a bitwise negation.

    Logical operations can be used to mask out certain bits, toggle them, or test specific conditions.

    Example of Logical AND (AND):

    mov ax, 0xF0F0    ; Load ax with 0xF0F0 (1111 0000 1111 0000 in binary)
    and ax, 0x00FF    ; Perform AND with 0x00FF (0000 0000 1111 1111 in binary)
                      ; Result: ax = 0x00F0 (0000 0000 1111 0000 in binary)
    

    The AND operation masks the upper 8 bits of the ax register, leaving only the lower byte.

    Example of Logical OR (OR):

    mov ax, 0xF0F0    ; Load ax with 0xF0F0 (1111 0000 1111 0000 in binary)
    or ax, 0x00FF     ; Perform OR with 0x00FF (0000 0000 1111 1111 in binary)
                      ; Result: ax = 0xF0FF (1111 0000 1111 1111 in binary)
    

    The OR operation combines the bits from both operands, turning on the bits in ax that were set in 0x00FF.

    Logical XOR (XOR):

    mov ax, 0xF0F0    ; Load ax with 0xF0F0 (1111 0000 1111 0000 in binary)
    xor ax, 0x00FF    ; Perform XOR with 0x00FF (0000 0000 1111 1111 in binary)
                      ; Result: ax = 0xF0FF (1111 0000 1111 1111 in binary)
    

    In XOR, bits that are the same in both operands result in 0, while bits that are different result in 1. The result here would combine both operands where the bits differ.


    3. Comparison Compound Expressions

    Comparison compound expressions are used to compare values and set condition flags that can influence the control flow of the program (such as using conditional jumps). These expressions often involve comparison instructions like CMP, TEST, and TST.

    Example:

    mov ax, 10         ; Load ax with 10
    mov bx, 5          ; Load bx with 5
    cmp ax, bx         ; Compare ax and bx
    je  EqualLabel     ; Jump if ax == bx (if the Zero Flag is set)
    jne NotEqualLabel  ; Jump if ax != bx (if the Zero Flag is cleared)
    

    Here, the CMP instruction is a comparison operation that subtracts bx from ax but does not store the result. It sets the flags based on the result of the subtraction.

    Compound Comparison Example:

    mov ax, 10         ; Load ax with 10
    mov bx, 5          ; Load bx with 5
    mov cx, 15         ; Load cx with 15
    cmp ax, bx         ; Compare ax with bx -> 10 - 5 = 5 (no flag set)
    je  EqualLabel     ; This won't jump, since ax != bx
    cmp ax, cx         ; Compare ax with cx -> 10 - 15 = -5 (sign flag set)
    jl  LessLabel      ; Jump since ax < cx (signed comparison)
    

    In this example, we have multiple comparisons. The first one (ax == bx) does not jump because ax is not equal to bx. The second comparison (ax < cx) sets the Sign Flag, and the jump is executed because ax is less than cx.


    Compound Expressions with Constants

    In assembly language, compound expressions can also combine constants directly within operations. For example, performing arithmetic or logical operations using immediate values (constants) and registers or memory addresses.

    Example with Constants (Arithmetic):

    mov ax, 10         ; Load ax with 10
    add ax, 15         ; Add constant 15 -> ax = 10 + 15 = 25
    sub ax, 5          ; Subtract constant 5 -> ax = 25 - 5 = 20
    

    In this case, the constants 15 and 5 are used directly in arithmetic operations.

    Example with Constants (Logical):

    mov ax, 0xFF00     ; Load ax with 0xFF00
    and ax, 0x0F0F     ; Perform AND with constant 0x0F0F -> ax = 0x0F00
    or ax, 0x00F0      ; Perform OR with constant 0x00F0 -> ax = 0x0FF0
    

    Here, the constants 0x0F0F and 0x00F0 are used in logical operations.


    Summary

    • Compound expressions in assembly language combine multiple operands and operators to perform more complex operations.
    • These expressions can be arithmetic (addition, subtraction, multiplication, etc.), logical (AND, OR, XOR, NOT), or comparison operations (using CMP, TEST).
    • Arithmetic expressions are often used for mathematical calculations.
    • Logical expressions are frequently used for bit manipulation.
    • Comparison expressions are used for conditional jumps and altering control flow.
    • Constants can be used within these compound expressions to make them more flexible and powerful.

    Compound expressions in assembly allow you to perform complex tasks in a single instruction sequence, making assembly programming more efficient and compact.

    ← Previous topic 65
    Conditional Control Flow Directives
    Next topic 67 →
    Data Representation & Conversion

    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