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
    DC-221
    Progress0 / 35 topics
    Topics
    1. Introduction to Computer Systems2. Information is Bits + Context3. Programs are Translated by Other Programs4. Understanding Compilation Systems5. Processors Read and Interpret Instructions6. Caches Matter7. Storage Devices Form a Hierarchy8. The Operating System Manages the Hardware9. Systems Communicate Using Networks10. Representing and Manipulating Information11. Information Storage12. Integer Representations13. Integer Arithmetic14. Floating Point15. Machine-Level Representation of Programs16. A Historical Perspective17. Program Encodings18. Data Formats19. Accessing Information20. Arithmetic and Logical Operations21. Control22. Procedures23. Array Allocation and Access24. Heterogeneous Data Structures25. Understanding Pointers26. Using the GDB Debugger27. Out-of-Bounds Memory References and Buffer Overflow28. x86-64: Extending IA-32 to 64 Bits29. Machine-Level Representations of Floating-Point Programs30. Processor Architecture31. The Y86 Instruction Set Architecture32. Logic Design and the Hardware Control Language (HCL)33. Sequential Y86 Implementations34. General Principles of Pipelining35. Pipelined Y86 Implementations
    DC-221›Control
    Computer Organization and Assembly LanguageTopic 21 of 35

    Control

    7 minread
    1,242words
    Intermediatelevel

    In computer organization, control refers to the mechanism by which the CPU (Central Processing Unit) manages the execution of instructions, coordinates different parts of the system, and ensures that tasks are carried out in the correct sequence. The control unit, a part of the CPU, plays a key role in managing the flow of data and instructions.

    1. Control Unit (CU)

    The Control Unit (CU) is responsible for directing the operations of the CPU by generating control signals. It orchestrates the execution of instructions by interpreting the machine code and signaling the various components of the computer (such as ALU, memory, and I/O devices) to perform the required tasks.

    Main Functions of the Control Unit:

    • Instruction Fetching: The CU fetches instructions from memory.
    • Instruction Decoding: It decodes the instruction to determine what action needs to be performed.
    • Control Signal Generation: It generates control signals to coordinate the execution of instructions.
    • Execution Sequencing: It manages the sequence of execution for different instructions.

    The CU can be categorized into two types:

    • Hardwired Control Unit: Uses fixed logic circuits to produce control signals. It’s fast but inflexible.
    • Microprogrammed Control Unit: Uses a set of instructions (micro-operations) stored in memory to produce control signals. It is slower but more flexible and easier to modify.

    2. Control Flow in Computers

    Control flow refers to the order in which individual instructions are executed in a program. In assembly language, control flow allows the program to make decisions, repeat actions, and jump to different sections based on conditions.

    Types of Control Flow:

    1. Sequential Execution:
      In a simple program, instructions are executed one after the other in sequence.

      • Example:
        MOV AX, 5   ; Move 5 into AX register
        MOV BX, 10  ; Move 10 into BX register
        ADD AX, BX  ; Add AX and BX, result in AX
        

      Here, the instructions are executed one after the other.

    2. Conditional Branching:
      Conditional branching allows the program to make decisions. Based on a comparison of values (using conditional flags), the program can jump to a different instruction.

      • Example:
        CMP AX, BX   ; Compare AX and BX
        JE Label     ; Jump to Label if Equal (Zero flag set)
        MOV CX, 20   ; This line will be skipped if AX = BX
        

      In this example, the program compares the values of AX and BX, and if they are equal, it jumps to the label Label to skip the instruction that moves 20 into CX.

    3. Unconditional Branching (Jump):
      An unconditional jump allows the program to jump to a specific part of the program without any conditions.

      • Example:
        JMP Label     ; Jump to Label unconditionally
        MOV CX, 30    ; This line will be skipped
        
    4. Loops (Repetition):
      Loops allow the program to repeat a set of instructions until a certain condition is met. Loops are controlled using conditional and unconditional jumps.

      • Example:
        MOV CX, 0    ; Initialize counter
        LoopStart:
        INC CX       ; Increment CX
        CMP CX, 10   ; Compare CX with 10
        JL LoopStart ; Jump back to LoopStart if CX < 10
        

      This program uses a loop to increment the value of CX from 0 to 10.

    5. Function Calls:
      Function calls allow a program to call a subroutine (function), execute it, and then return to the point where the call was made. This is managed by a call stack.

      • Example:
        CALL MyFunction    ; Call a subroutine
        MOV AX, 100        ; This line is executed after returning from MyFunction
        

      A function call typically involves saving the return address (where to continue after the function call) on the stack, executing the function, and then returning to the saved address when the function is done.

    3. Control Instructions in Assembly Language

    Control instructions in assembly language are used to manipulate the flow of execution. These instructions include jump operations, loops, and condition checks.

    Types of Control Instructions:

    • Jump Instructions (Unconditional):

      • JMP (Jump): Causes the program to jump to a specified label, causing a change in the flow of execution.
        • Example:
          JMP LoopStart   ; Unconditionally jump to LoopStart
          
    • Conditional Jump Instructions: Conditional jumps depend on the status of flags set after a comparison operation.

      • JE (Jump if Equal): Jump if the Zero flag is set (i.e., the two values compared were equal).

        • Example:
          CMP AX, BX  ; Compare AX and BX
          JE EqualLabel ; Jump if AX equals BX
          
      • JNE (Jump if Not Equal): Jump if the Zero flag is not set (i.e., the values compared were not equal).

        • Example:
          CMP AX, BX  ; Compare AX and BX
          JNE NotEqualLabel ; Jump if AX is not equal to BX
          
      • JG (Jump if Greater): Jump if the result of a comparison shows that the first value is greater.

        • Example:
          CMP AX, BX   ; Compare AX and BX
          JG GreaterLabel ; Jump if AX > BX
          
      • JL (Jump if Less): Jump if the result of a comparison shows that the first value is less.

        • Example:
          CMP AX, BX   ; Compare AX and BX
          JL LessLabel   ; Jump if AX < BX
          
    • Loop Instructions:

      • LOOP: This instruction decrements the counter and jumps to the label if the counter is not zero.
        • Example:
          MOV CX, 5  ; Set loop counter
          LoopStart:
          ; Loop body
          LOOP LoopStart  ; Decrement CX and loop back to LoopStart
          
    • CALL and RET (Function Call and Return):

      • CALL: This instruction pushes the return address onto the stack and jumps to the function.

        • Example:
          CALL MyFunction   ; Call the function
          
      • RET: The RET instruction pops the return address from the stack and jumps back to that address.

        • Example:
          RET  ; Return from the function
          

    4. Control Flags

    The control flags are special flags in the CPU that help manage the control flow based on the results of arithmetic and logical operations. These flags are set or cleared based on the outcome of instructions like compare (CMP) and test (TEST).

    • Zero Flag (ZF): Set if the result of an operation is zero. It is commonly used in conditional jumps like JE (Jump if Equal).

    • Carry Flag (CF): Set if there is a carry out or borrow in an arithmetic operation (for unsigned numbers). It is used in conditional jumps like JC (Jump if Carry).

    • Sign Flag (SF): Set if the result is negative (for signed numbers).

    • Overflow Flag (OF): Set if there is an overflow in an arithmetic operation for signed numbers (i.e., the result cannot be represented within the available bits).

    • Parity Flag (PF): Set if the number of 1s in the result is even (used for error detection).

    5. Control in Multitasking and Operating Systems

    In modern computers, control also involves managing multiple tasks or processes at the same time. The operating system's scheduler uses control instructions to switch between processes in multitasking environments. Key mechanisms include:

    • Context Switching: The process of saving the state of a running process and loading the state of the next process to be executed.

    • Interrupts: Interrupts allow the CPU to be temporarily diverted from its current task to handle a more urgent task (like input/output). Once the interrupt is handled, control returns to the interrupted task.

    Conclusion

    The control unit and control flow mechanisms are crucial in ensuring that a computer follows the correct sequence of instructions and responds to conditions during execution. Control instructions in assembly language, such as jumps, loops, and function calls, provide the flexibility to implement decision-making and repetitive tasks in programs. Additionally, control flags help in managing the outcomes of operations, and modern systems use control techniques like multitasking and interrupts to handle multiple processes efficiently.

    Previous topic 20
    Arithmetic and Logical Operations
    Next topic 22
    Procedures

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