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›Procedures
    Computer Organization and Assembly LanguageTopic 22 of 35

    Procedures

    8 minread
    1,332words
    Intermediatelevel

    In computer organization and assembly language, a procedure (also known as a function or subroutine) is a block of code that performs a specific task and can be reused multiple times within a program. Procedures are used to modularize code, making it easier to maintain, understand, and debug.

    1. What is a Procedure?

    A procedure is a set of instructions that can be executed when called by the program. When a procedure is called, the program jumps to the procedure, executes the instructions, and then returns to the point where it was called. This makes the code more organized and modular, reducing redundancy and improving efficiency.

    2. Why Use Procedures?

    • Reusability: Procedures allow you to reuse code, so you don't have to write the same instructions multiple times in different places.
    • Modularity: Programs are easier to manage when they are divided into smaller, logical units.
    • Readability and Maintainability: By breaking down large tasks into smaller procedures, programs become easier to read, understand, and modify.

    3. Structure of a Procedure

    A procedure typically has two main parts:

    • Procedure Declaration/Definition: The part where the procedure is written or defined. It includes the instructions that make up the procedure.
    • Procedure Call: The part where the procedure is invoked or called to perform its task.

    4. Creating a Procedure in Assembly Language

    In assembly language, creating a procedure typically involves defining a block of code with a label (a symbolic name) and using a call instruction to invoke it. Procedures are often used to perform repetitive tasks or to organize code logically.

    Basic Example of a Procedure in Assembly Language:

    1. Defining a Procedure: A simple procedure might add two numbers and return the result.

      ADD_NUMBERS:      ; Procedure label
          ADD AX, BX    ; Add contents of AX and BX
          RET           ; Return from procedure
      

      In this example:

      • ADD_NUMBERS: is the label that marks the start of the procedure.
      • ADD AX, BX performs the addition.
      • RET is the return instruction, which causes the program to jump back to the point where the procedure was called.
    2. Calling a Procedure: To call the ADD_NUMBERS procedure, we use the CALL instruction.

      MOV AX, 5      ; Load 5 into AX
      MOV BX, 10     ; Load 10 into BX
      CALL ADD_NUMBERS ; Call the procedure
      
      • The CALL instruction pushes the return address onto the stack and jumps to the procedure.
      • After the RET instruction in the procedure, the program returns to the next instruction after the CALL.

    5. Procedure Arguments (Parameters)

    Procedures can take inputs, called parameters or arguments, which are passed to them. The most common methods of passing parameters are:

    • Using Registers: The values are placed into registers before calling the procedure. This is faster but limited by the number of available registers.
    • Using the Stack: The arguments are pushed onto the stack before calling the procedure. The procedure then pops the arguments from the stack.

    Example of Passing Parameters Using Registers:

    ; Procedure to add two numbers
    ADD_NUMBERS:
        ADD AX, BX     ; Add BX to AX (AX holds the result)
        RET            ; Return from the procedure
    
    ; Main program
    MOV AX, 5         ; First number in AX
    MOV BX, 10        ; Second number in BX
    CALL ADD_NUMBERS  ; Call procedure, result in AX
    

    In this case, the values in AX and BX are passed as arguments to the ADD_NUMBERS procedure. The result of the addition is stored back in AX.

    Example of Passing Parameters Using the Stack:

    ; Procedure to add two numbers
    ADD_NUMBERS:
        POP BX         ; Pop the first parameter into BX
        POP AX         ; Pop the second parameter into AX
        ADD AX, BX     ; Add AX and BX
        PUSH AX        ; Push the result back onto the stack
        RET            ; Return from the procedure
    
    ; Main program
    PUSH 10           ; Push first parameter onto the stack
    PUSH 5            ; Push second parameter onto the stack
    CALL ADD_NUMBERS  ; Call procedure to add the numbers
    POP AX            ; Pop the result into AX
    

    In this case, we are pushing the parameters onto the stack before calling the procedure. The procedure pops the values off the stack, performs the addition, and then pushes the result back onto the stack.

    6. Local and Global Variables in Procedures

    • Local Variables: Variables that are used only within a procedure and are typically stored on the stack. These variables are created when the procedure is called and destroyed when the procedure returns.

    • Global Variables: Variables that are accessible by all procedures in the program. These are usually stored in memory and retain their values throughout the program’s execution.

    7. The Stack and Procedure Execution

    When a procedure is called, the stack plays a crucial role in managing the procedure’s execution:

    1. Saving the Return Address: When the CALL instruction is executed, the address of the next instruction is pushed onto the stack. This is where the program will return after the procedure finishes.
    2. Pushing Parameters: If parameters are passed via the stack, they are pushed onto the stack before the procedure is called.
    3. Saving Registers: If the procedure modifies registers (such as AX, BX, etc.), it’s common to push the register values onto the stack at the beginning of the procedure and pop them back at the end to restore the state.
    4. Return Address: When the procedure finishes, the RET instruction pops the return address from the stack and jumps back to that address in the main program.

    8. Recursive Procedures

    A recursive procedure is a procedure that calls itself. This is useful for tasks that can be broken down into smaller, similar tasks, such as calculating factorials or solving problems using divide and conquer methods.

    Example of a Recursive Procedure (Factorial):

    ; Procedure to calculate factorial
    FACTORIAL:
        CMP AX, 1         ; Compare AX with 1
        JLE DONE          ; If AX <= 1, done
        PUSH AX           ; Save AX to the stack
        DEC AX            ; Decrement AX
        CALL FACTORIAL    ; Recursive call
        POP BX            ; Restore original AX value
        MUL BX            ; Multiply result by the original AX
    DONE:
        RET               ; Return from procedure
    
    ; Main program
    MOV AX, 5            ; Load 5 into AX
    CALL FACTORIAL       ; Call factorial procedure
    

    In this example, the FACTORIAL procedure calls itself until it reaches the base case (when AX <= 1). The result of the multiplication is returned through the stack.

    9. Return Value from a Procedure

    To return a value from a procedure, you can use the AX register (for 16-bit values) or other registers, depending on the architecture. The value is usually stored in a register when the procedure finishes, and the caller retrieves it after the procedure call.

    10. Example of Using Procedures in a Larger Program

    Here’s an example of how you might structure a program that uses procedures:

    ; Program to calculate the sum of two numbers using a procedure
    
    SUM_PROCEDURE:
        ADD AX, BX         ; Add BX to AX
        RET                ; Return from procedure
    
    START:
        MOV AX, 5          ; Load 5 into AX
        MOV BX, 10         ; Load 10 into BX
        CALL SUM_PROCEDURE ; Call the procedure to sum the numbers
        ; At this point, AX contains the sum (15)
        ; Continue with the program...
    

    11. Procedure Optimization Tips:

    • Minimize Stack Usage: Be mindful of how many values you push and pop to/from the stack. Excessive use of the stack can slow down your program.
    • Use Registers Efficiently: Use CPU registers efficiently to pass parameters and store intermediate results instead of always using memory or the stack.
    • Tail Recursion: In recursive procedures, consider optimizing for tail recursion, where the recursive call is the last operation. This can help the compiler optimize the recursion into a loop, saving stack space.

    Conclusion

    Procedures in assembly language are a powerful way to organize and reuse code, making programs more modular, readable, and maintainable. By understanding how to define, call, and manage procedures, you can create efficient programs that perform complex tasks with simplicity and clarity. Procedures allow you to break down tasks, pass parameters, and manage execution flow effectively.

    Previous topic 21
    Control
    Next topic 23
    Array Allocation and Access

    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 time8 min
      Word count1,332
      Code examples0
      DifficultyIntermediate