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›Compound Expressions
    Computer Organization and Assembly LanguageTopic 66 of 73

    Compound Expressions

    7 minread
    1,204words
    Intermediatelevel

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

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