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

    Integer Expressions

    7 minread
    1,177words
    Intermediatelevel

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

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