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 PortalCOMP3137Operations Array Loops
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 52 of 73
    Topic Study Note Illustration
    52

    Operations, Array & Loops

    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...

    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 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