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›Operations, Array & Loops
    Computer Organization and Assembly LanguageTopic 52 of 73

    Operations, Array & Loops

    7 minread
    1,155words
    Intermediatelevel

    Operations, Arrays, and Loops in Assembly Language

    In assembly language programming, operations, arrays, and loops are fundamental concepts that allow you to manipulate data, store large datasets, and repeat certain tasks efficiently. Below is a detailed explanation of these concepts in assembly language, with examples and usage.


    1. Operations in Assembly Language

    Assembly language provides a variety of arithmetic, logical, bitwise, and comparison operations to manipulate data. These operations allow you to perform mathematical calculations, control program flow, and manipulate individual bits or groups of bits.

    Basic Arithmetic Operations

    Assembly languages support basic arithmetic operations like addition, subtraction, multiplication, and division.

    • Addition (ADD):

      • Adds two values and stores the result in a register or memory.
      • Syntax: ADD destination, source
      • Example:
        MOV AX, 5      ; AX = 5
        ADD AX, 3      ; AX = AX + 3 (AX = 8)
        
    • Subtraction (SUB):

      • Subtracts the second operand from the first operand.
      • Syntax: SUB destination, source
      • Example:
        MOV AX, 10     ; AX = 10
        SUB AX, 3      ; AX = AX - 3 (AX = 7)
        
    • Multiplication (MUL):

      • Multiplies two values. In x86 assembly, MUL works on the accumulator register (AX for 16-bit and EAX for 32-bit).
      • Syntax: MUL operand
      • Example:
        MOV AX, 5      ; AX = 5
        MOV BX, 3      ; BX = 3
        MUL BX         ; AX = AX * BX (AX = 15)
        
    • Division (DIV):

      • Divides the accumulator by the operand. The result is stored in the accumulator, and the remainder is stored in a separate register.
      • Syntax: DIV operand
      • Example:
        MOV AX, 10     ; AX = 10 (numerator)
        MOV BX, 3      ; BX = 3 (denominator)
        DIV BX         ; AX = AX / BX (quotient), DX = remainder
        

    Logical Operations

    Logical operations are used for boolean algebra (AND, OR, XOR, NOT).

    • AND (AND):

      • Performs a bitwise AND operation between two operands.
      • Syntax: AND destination, source
      • Example:
        MOV AX, 5      ; AX = 0101 in binary
        MOV BX, 3      ; BX = 0011 in binary
        AND AX, BX     ; AX = AX AND BX (AX = 0001)
        
    • OR (OR):

      • Performs a bitwise OR operation.
      • Syntax: OR destination, source
      • Example:
        MOV AX, 5      ; AX = 0101 in binary
        MOV BX, 3      ; BX = 0011 in binary
        OR AX, BX      ; AX = AX OR BX (AX = 0111)
        
    • XOR (XOR):

      • Performs a bitwise exclusive OR operation.
      • Syntax: XOR destination, source
      • Example:
        MOV AX, 5      ; AX = 0101 in binary
        MOV BX, 3      ; BX = 0011 in binary
        XOR AX, BX     ; AX = AX XOR BX (AX = 0110)
        
    • NOT (NOT):

      • Performs a bitwise negation (inverts the bits).
      • Syntax: NOT operand
      • Example:
        MOV AX, 5      ; AX = 0101 in binary
        NOT AX         ; AX = NOT AX (AX = 1010)
        

    Comparison Operations

    Comparison operations set the condition flags (CF, ZF, SF, etc.) and are used to compare values.

    • Compare (CMP):

      • Compares two values by subtracting the second from the first, but the result is not stored.
      • Syntax: CMP operand1, operand2
      • Example:
        MOV AX, 5      ; AX = 5
        CMP AX, 5      ; Compare AX with 5 (result in flags)
        
    • Jump (Conditional) (JE, JNE, JG, JL):

      • Based on the result of the comparison, the program can jump to another part of the code.
      • Example:
        CMP AX, BX     ; Compare AX and BX
        JE equal       ; Jump to "equal" label if AX == BX
        

    2. Arrays in Assembly Language

    Arrays in assembly are a way to store multiple values in a contiguous block of memory. Since assembly does not have high-level constructs like arrays in C, you have to handle the memory addresses manually. An array is just a series of consecutive memory locations.

    Defining an Array

    To define an array in assembly, you use data directives like DB, DW, DD, or DQ (depending on whether the elements are bytes, words, double words, or quad words).

    • Byte Array (DB): A series of bytes.

      section .data
          myArray DB 1, 2, 3, 4, 5  ; Array of 5 bytes
      
    • Word Array (DW): A series of words (16-bit values).

      section .data
          myWordArray DW 1000, 2000, 3000, 4000  ; Array of 4 words (16-bit integers)
      

    Accessing Array Elements

    To access elements in an array, you need to calculate the address of the element by using the base address of the array and an index.

    Example of accessing an array of bytes:

    section .data
        myArray DB 1, 2, 3, 4, 5  ; Array of bytes
    
    section .text
        MOV SI, 2             ; SI = index 2 (third element)
        LEA AX, [myArray]     ; Load the base address of myArray into AX
        ADD AX, SI            ; Add index to the base address
        MOV BL, [AX]          ; BL = myArray[2] (value = 3)
    

    Array with Loop (Iterating over the Array)

    In order to loop through an array, you need to use looping techniques such as LOOP or JMP combined with condition checking.

    section .data
        myArray DB 1, 2, 3, 4, 5  ; Array of 5 bytes
    
    section .text
        MOV CX, 5              ; Set loop counter to 5 (for 5 elements)
        MOV SI, 0              ; Start from the first element of the array
    
    loop_start:
        LEA AX, [myArray + SI] ; Load address of myArray[SI] into AX
        MOV BL, [AX]           ; Load value at myArray[SI] into BL
        ; Process the value in BL here
        INC SI                 ; Move to the next element
        LOOP loop_start        ; Decrease CX and loop if CX != 0
    

    3. Loops in Assembly Language

    Loops are a critical component of programming that allow repetitive execution of instructions. Assembly provides several ways to implement loops, including using LOOP, JMP, CALL, and conditional jumps.

    Using the LOOP Instruction

    The LOOP instruction is used for simple counter-controlled loops. It decrements the CX register and jumps to the target label if CX is non-zero.

    • Syntax:
      LOOP label
      

    Example: Simple Loop Using LOOP

    section .data
        ; Data section
    
    section .text
        MOV CX, 5      ; Set loop counter to 5
    loop_start:
        ; Do something here (e.g., print or process)
        ; For example, add 2 to a register
        ADD AX, 2
        LOOP loop_start ; Decrement CX and loop until CX = 0
    

    In this example:

    • The loop will run 5 times because the CX register is set to 5. Each time the loop runs, CX is decremented, and the program jumps back to the loop_start label.

    Using JMP for Infinite or Conditional Loops

    A JMP instruction can be used to create loops by unconditionally jumping back to the start of a block of code. You can also combine it with conditionals to create more flexible loops.

    Example of an infinite loop:

    start_loop:
        ; Do some operations
        JMP start_loop  ; Jump back to start_loop, creating an infinite loop
    

    Conclusion

    • Operations: Assembly provides arithmetic operations (ADD,
    Previous topic 51
    Knowledge about Different Data Types
    Next topic 53
    Division and Multiplication in Assembly

    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,155
      Code examples0
      DifficultyIntermediate