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 PortalCOMP3137Integer 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 31 of 73
    Topic Study Note Illustration
    31

    Integer Expressions

    An integer expression in assembly language refers to a combination of integer constants , registers , and operators that produce an integer result. These expressions can involve...

    Integer Expressions in Assembly Language

    An integer expression in assembly language refers to a combination of integer constants, registers, and operators that produce an integer result. These expressions can involve arithmetic operations such as addition, subtraction, multiplication, division, and more. Understanding how integer expressions work is crucial for manipulating data, performing calculations, and managing control flow in assembly language programming.

    Components of Integer Expressions

    Integer expressions in assembly language typically consist of:

    1. Constants: Fixed values that are used in the expression (e.g., 5, 0x1F4).
    2. Registers: CPU registers that store data (e.g., AX, BX, CX).
    3. Memory operands: Values stored at specific memory addresses.
    4. Operators: Arithmetic or logical operators that define the operations to be performed (e.g., +, -, *, /, AND, OR).

    Types of Operators in Integer Expressions

    Assembly language supports a variety of arithmetic operators, which allow you to perform calculations. These operators can be applied to constants, registers, and memory values.

    1. Addition (+)

    • Adds two values together.
    • Example:
      MOV AX, 5    ; AX = 5
      ADD AX, 10   ; AX = AX + 10, so AX = 15
      

    2. Subtraction (-)

    • Subtracts one value from another.
    • Example:
      MOV AX, 20   ; AX = 20
      SUB AX, 5    ; AX = AX - 5, so AX = 15
      

    3. Multiplication (*)

    • Multiplies two values together.
    • Example (using MUL instruction):
      MOV AX, 5    ; AX = 5
      MOV BX, 4    ; BX = 4
      MUL BX       ; AX = AX * BX, so AX = 5 * 4 = 20
      

    4. Division (/)

    • Divides one value by another.
    • Example (using DIV instruction):
      MOV AX, 20   ; AX = 20
      MOV BX, 4    ; BX = 4
      DIV BX       ; AX = AX / BX, so AX = 20 / 4 = 5 (quotient)
                   ; DX = remainder (if any)
      

    5. Bitwise AND (AND)

    • Performs a bitwise AND operation between two operands.
    • Example:
      MOV AX, 0xF0  ; AX = 0xF0 (11110000 in binary)
      MOV BX, 0xCC  ; BX = 0xCC (11001100 in binary)
      AND AX, BX    ; AX = AX AND BX, result is 0xC0 (11000000 in binary)
      

    6. Bitwise OR (OR)

    • Performs a bitwise OR operation between two operands.
    • Example:
      MOV AX, 0xF0  ; AX = 0xF0
      MOV BX, 0xCC  ; BX = 0xCC
      OR AX, BX     ; AX = AX OR BX, result is 0xFC (11111100 in binary)
      

    7. Bitwise XOR (XOR)

    • Performs a bitwise XOR operation between two operands.
    • Example:
      MOV AX, 0xF0  ; AX = 0xF0
      MOV BX, 0xCC  ; BX = 0xCC
      XOR AX, BX    ; AX = AX XOR BX, result is 0x3C (00111100 in binary)
      

    8. Negation (NOT)

    • Inverts all the bits of a value.
    • Example:
      MOV AX, 0xF0  ; AX = 0xF0
      NOT AX         ; AX = ~AX, result is 0x0F (00001111 in binary)
      

    9. Shift Operations (SHL, SHR)

    • SHL (Shift Left): Shifts the bits of a value to the left, essentially multiplying by 2.
    • SHR (Shift Right): Shifts the bits of a value to the right, essentially dividing by 2.
    • Example:
      MOV AX, 4    ; AX = 4 (00000100 in binary)
      SHL AX, 1    ; AX = AX << 1, result is 8 (00001000 in binary)
      
      MOV BX, 8    ; BX = 8 (00001000 in binary)
      SHR BX, 1    ; BX = BX >> 1, result is 4 (00000100 in binary)
      

    Parentheses and Precedence

    Just like in higher-level programming languages, parentheses can be used in assembly language expressions to group operations and control the order of execution. The operations inside parentheses are evaluated first.

    • Example:
      MOV AX, 10     ; AX = 10
      MOV BX, 5      ; BX = 5
      MOV CX, 2      ; CX = 2
      ADD AX, BX     ; AX = 10 + 5 = 15
      MUL CX         ; AX = AX * CX = 15 * 2 = 30
      

    Integer Expressions and the Stack

    Integer expressions in assembly can also involve the stack, especially when using subroutines (functions) that take parameters or return values. In these cases, values are often pushed to or popped from the stack.

    • Example: Passing two integers to a subroutine using the stack:
      PUSH AX        ; Save AX to the stack
      MOV AX, 10     ; Load AX with 10
      PUSH BX        ; Save BX to the stack
      MOV BX, 20     ; Load BX with 20
      CALL Add       ; Call the 'Add' subroutine to add values
      POP BX         ; Restore BX from the stack
      POP AX         ; Restore AX from the stack
      
      ; Add subroutine (adds values in AX and BX, returns result in AX)
      Add:
          ADD AX, BX ; AX = AX + BX
          RET         ; Return from subroutine
      

    Example of Complex Integer Expression

    You can combine multiple operators to create more complex expressions in assembly. Here’s an example that involves both arithmetic and logical operations.

    • Example:
      MOV AX, 10      ; AX = 10
      MOV BX, 5       ; BX = 5
      MOV CX, 2       ; CX = 2
      ADD AX, BX      ; AX = AX + BX, so AX = 10 + 5 = 15
      MUL CX          ; AX = AX * CX, so AX = 15 * 2 = 30
      AND AX, 0xF0    ; AX = AX AND 0xF0, result is 0x20 (binary 00100000)
      

    In this example:

    1. The value 10 is added to 5, producing 15.
    2. The result 15 is then multiplied by 2, yielding 30.
    3. A bitwise AND operation is then performed on 30 with 0xF0 (240 in decimal), resulting in 0x20.

    Integer Expressions and Flags

    In assembly, the flags register plays an important role in handling the results of integer expressions. For example, after an arithmetic operation, the Zero Flag (ZF), Carry Flag (CF), Sign Flag (SF), and Overflow Flag (OF) are often used to check the result.

    • Example (using CMP and JZ instructions):
      MOV AX, 10    ; AX = 10
      MOV BX, 10    ; BX = 10
      CMP AX, BX    ; Compare AX with BX
      JZ Equal      ; Jump to 'Equal' label if AX == BX (Zero Flag is set)
      
      ; Equal:
      MOV CX, 1     ; If AX == BX, set CX to 1
      

    In this example:

    • CMP subtracts BX from AX, setting the Zero Flag (ZF) if they are equal.
    • JZ (Jump if Zero) uses the Zero Flag to decide whether to jump to the Equal label.

    Conclusion

    Integer expressions in assembly language are the backbone of performing calculations and logical operations. By combining constants, registers, and memory operands with operators, you can create complex expressions that perform a wide variety of tasks. Understanding how to use arithmetic operators, bitwise operations, and control flow in conjunction with the flags and stack allows you to write efficient and powerful assembly programs.

    These expressions are essential for tasks such as:

    • Arithmetic calculations.
    • Comparing values.
    • Implementing algorithms like sorting or searching.
    ← Previous topic 30
    Integer Constants
    Next topic 32 →
    Real Number Constants

    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