Example of C Program with Subroutine - 4.1.4 | 4. Instruction: Procedure CALL/RETURN | Computer Organisation and Architecture - Vol 2
Students

Academic Programs

AI-powered learning for grades 8-12, aligned with major curricula

Professional

Professional Courses

Industry-relevant training in Business, Technology, and Design

Games

Interactive Games

Fun games to boost memory, math, typing, and English skills

Example of C Program with Subroutine

4.1.4 - Example of C Program with Subroutine

Enroll to start learning

You’ve not yet enrolled in this course. Please enroll for free to listen to audio lessons, classroom podcasts and take practice test.

Practice

Interactive Audio Lesson

Listen to a student-teacher conversation explaining the topic in a relatable way.

Understanding Subroutines

🔒 Unlock Audio Lesson

Sign up and enroll to listen to this audio lesson

0:00
--:--
Teacher
Teacher Instructor

Today, we will discuss subroutines, which are essential for modular programming. Can anyone tell me what a subroutine is?

Student 1
Student 1

Isn't it a piece of code that can be called multiple times within a program?

Teacher
Teacher Instructor

Exactly! Subroutines allow us to write reusable code. They help in organizing our programming tasks more efficiently. Can anyone give an example of when we would want to use a subroutine?

Student 2
Student 2

If I need to calculate a value several times, I could write a function to do that!

Teacher
Teacher Instructor

Great example! Functions can encapsulate calculations or tasks we want to repeat. We can call a function instead of rewriting code.

Teacher
Teacher Instructor

Let's remember: **Functions = Reusability!** Today, we also need to focus on the concept of context. What happens to our current state when we call a subroutine?

Student 3
Student 3

Uh, we have to save it somewhere?

Teacher
Teacher Instructor

Yes! We save the program's current context, including the Program Counter, before jumping to the subroutine. This is done using a stack.

Teacher
Teacher Instructor

In summary, subroutines improve code organization and allow us to save and restore the program state. Remember, **Modular + Context = Functionality!**

Function Calls and Returns

🔒 Unlock Audio Lesson

Sign up and enroll to listen to this audio lesson

0:00
--:--
Teacher
Teacher Instructor

Now that we understand subroutines, let's discuss function calls and returns. How does a program keep track of where to return after a function call?

Student 4
Student 4

It stores the return address on the stack?

Teacher
Teacher Instructor

Exactly! When we call a function, we push the return address onto the stack. When the function completes, we pop this address back into the Program Counter.

Student 2
Student 2

So the stack is really important for managing these calls?

Teacher
Teacher Instructor

Absolutely! It's crucial for storing our program's context. Also, there are both **conditional** and **unconditional** jumps involved in this process. What’s the difference?

Student 1
Student 1

Conditional jumps only happen if a certain condition is met, while unconditional jumps happen every time?

Teacher
Teacher Instructor

Correct! Unconditional jumps occur every time we call a subroutine, while conditional jumps depend on flags like zero or equality.

Teacher
Teacher Instructor

Let’s summarize that: **Jumps + Stack = Context Management!** Always remember how vital the stack is.

Program Example and Assembly Reflection

🔒 Unlock Audio Lesson

Sign up and enroll to listen to this audio lesson

0:00
--:--
Teacher
Teacher Instructor

Let's look into a simple example: a C program that computes a square. How do we define this function?

Student 3
Student 3

We declare it with a return type, like 'int square(int value).' right?

Teacher
Teacher Instructor

That's right! This function takes an integer, calculates its square, and returns that value. Can anyone tell me what happens in assembly when we call this function?

Student 4
Student 4

The CPU saves the context, jumps to the function, and then returns the result back to the caller?

Teacher
Teacher Instructor

Exactly! The assembly might show you the register movements and how the stack is utilized to preserve the context with each call.

Student 1
Student 1

So, all of these low-level operations help with high-level language execution!

Teacher
Teacher Instructor

Indeed! Understanding both levels aids in writing efficient programs. Remember: **High-Level Logic + Low-Level Operations = Effective Programming!**

Introduction & Overview

Read summaries of the section's main ideas at different levels of detail.

Quick Overview

This section delves into the practical use of procedural calls and returns in C programming, highlighting the importance of managing program contexts effectively.

Standard

The section explores how subroutines in C can be called and return values while managing the program's context through stacks, program counters, and status words. It emphasizes the significance of unconditional and conditional jumps in these procedures, alongside an example illustrating a simple C function.

Detailed

Example of C Program with Subroutine

In this section, we discuss the implementation of subroutines in C, focusing on how a function can be called and how the program can return from the function while maintaining its state. We'll examine the role of the CPU in handling these procedure calls, specifically through the use of the stack to save the context and how the program counter is manipulated during these calls.

Key Concepts Covered:

  1. Subroutine Overview: A subroutine, or function, is a self-contained block of code within a larger program that can be called and executed. In the context of C programming, functions provide a modular way to organize code, making it reusable and easier to read.
  2. Function Call Mechanics: When a function is invoked, the current state of the program (including the Program Counter and relevant register values) is saved. This allows the program to return to the exact point it left off after the function execution is complete.
  3. Importance of Stacks: The stack is used to store the context of the program during function calls. Each time a procedure is called, the current program status, variables, and flags are pushed onto the stack, and when returning, those values are popped back to restore the previous state.
  4. Example of C Function: The section provides an example C program demonstrating how a simple function to compute a square of a number (square()) is designed. When the function is called, the input value is processed, and the result is returned to the calling function, illustrating the usage of the stack and context preservation.

By understanding these concepts, programmers can write efficient and modular code, ensuring that their programs work as intended even when using multiple procedure calls.

Youtube Videos

One Shot of Computer Organisation and Architecture for Semester exam
One Shot of Computer Organisation and Architecture for Semester exam

Audio Book

Dive deep into the subject with an immersive audiobook experience.

Understanding Procedure Calls in C

Chapter 1 of 4

🔒 Unlock Audio Chapter

Sign up and enroll to access the full audio experience

0:00
--:--

Chapter Content

So, as we are all very much familiar with C programming. So, we are going to take a very simple C program with a subroutine and see how it reflects in an assembly language. So, if int a, b; a = 5, b = r2. So, this is a function in which you are making a2 then in this case actually this b = a2.

Detailed Explanation

In this section, we introduce a simple C program that involves a procedure (or function). It starts with declaring two integers, a and b, where a is assigned the value 5. The following syntax is used to define a function that calculates the square of a and assigns it to b. The function takes a as an input parameter, computes its square (a * a), and returns that value, which is then stored in b. This showcases how functions modularize code and how they play a crucial role in any programming language, particularly in managing computations and reusability.

Examples & Analogies

Think of a function in programming like a robotic arm in a factory. You give it an instruction (like square a number), and it carries out that work independently without needing to know how the entire factory operates. Once it's done, it hands back the result (the squared number), allowing the main assembly line (the rest of your program) to continue based on that result.

Mechanics of Function Calls

Chapter 2 of 4

🔒 Unlock Audio Chapter

Sign up and enroll to access the full audio experience

0:00
--:--

Chapter Content

Now, if you look at it how the subroutine would look like. So, first we are taking a value of the memory location a into R1, then it’s a so now R1 has the value of a. So now, what you are doing? You are making a jump unconditional to a procedure whose name is square.

Detailed Explanation

When the program calls the subroutine (in this case, a function named square), it begins by accessing the value of a and loading it into a register (R1). This is the first technical step in executing a function call. An 'unconditional jump' means the program will directly move to the instructions defined in the square procedure; it does not need to check any conditions. This effectively pauses the current function and starts executing the new function, which further leads to processing and computation of the square.

Examples & Analogies

Imagine you are writing a book and you decide to take a break to find a specific reference in another book. You mark your current page (this is like saving your current context), open the other book (this is the jump to the subroutine), and look for the information you need. Once you find it and make notes, you return back to your original book and continue writing from where you left off.

Returning from a Subroutine

Chapter 3 of 4

🔒 Unlock Audio Chapter

Sign up and enroll to access the full audio experience

0:00
--:--

Chapter Content

So when I am executing the return statement what happens? So, when I am calling this one you have to store all the value of the temporary variables temporary registers as well as most importantly you have to store the value of the memory location for the jump instruction.

Detailed Explanation

When the return statement is executed in a subroutine, it signifies the end of that function. Before returning, it's essential to save all temporary data used during the execution, including the current program counter (PC) value, which indicates the next instruction's address in the main program. This saving process is crucial as it enables the program to revert to the exact point it left off before the subroutine was called, ensuring no data or instruction flow is lost.

Examples & Analogies

Similar to how you might use bookmarks to keep track of where you left off when reading multiple books. As you switch between books (or in programming, between functions), you save your progress (values of temporary variables or the current page) so that you can pick up right where you left off when you return to that book.

Understanding the Stack in Procedure Calls

Chapter 4 of 4

🔒 Unlock Audio Chapter

Sign up and enroll to access the full audio experience

0:00
--:--

Chapter Content

So in fact, you require basically a program counter, memory everything is required and in addition you require a stack which is holding all the components or the context of the programs or the subroutines when a call is made from one subroutine to another.

Detailed Explanation

The stack is a critical data structure used in managing function calls. It acts like a temporary storage area for all variables, status registers, and the program counter details necessary when a function is invoked. When one function calls another, all relevant context from the previous function is pushed onto the stack. Upon returning, this context is popped off the stack, allowing the program to continue executing smoothly where it left off. This mechanism is vital, especially when dealing with nested function calls.

Examples & Analogies

Think of the stack like a stack of plates at a buffet. Each plate represents a function call. When you take a plate off the top to use, you can put it back when you're done. If you need to handle another plate (function call), you place your current one on the stack (top of the pile) to access it later. When you’re finished with the last plate, you just take them off in reverse order and go back to the previous tasks.

Key Concepts

  • Subroutine Overview: A subroutine, or function, is a self-contained block of code within a larger program that can be called and executed. In the context of C programming, functions provide a modular way to organize code, making it reusable and easier to read.

  • Function Call Mechanics: When a function is invoked, the current state of the program (including the Program Counter and relevant register values) is saved. This allows the program to return to the exact point it left off after the function execution is complete.

  • Importance of Stacks: The stack is used to store the context of the program during function calls. Each time a procedure is called, the current program status, variables, and flags are pushed onto the stack, and when returning, those values are popped back to restore the previous state.

  • Example of C Function: The section provides an example C program demonstrating how a simple function to compute a square of a number (square()) is designed. When the function is called, the input value is processed, and the result is returned to the calling function, illustrating the usage of the stack and context preservation.

  • By understanding these concepts, programmers can write efficient and modular code, ensuring that their programs work as intended even when using multiple procedure calls.

Examples & Applications

A simple function in C can look like this:

int square(int value) {

return value * value;

}

This function takes an integer input and returns its square.

In assembly, when a function is called, the instruction flow would be like:

Store the current Program Counter on the stack.

Jump to the function's memory location.

Execute the function and return the result.

Memory Aids

Interactive tools to help you remember key concepts

🎵

Rhymes

Functions are nice, they save us time, call them with ease, they fit like a rhyme.

📖

Stories

Imagine a librarian organizing books. Each shelf represents a subroutine, and whenever a book (or function) is called, the librarian writes the location (context) down so they can return to it later.

🧠

Memory Tools

Remember SLIC: Subroutine, Load context, Instruct to jump, Call function. This will guide you through the process of handling a subroutine.

🎯

Acronyms

STACK

Save

Transition

Allocate

Call

Keep. This describes the steps taken in managing the stack during function calls.

Flash Cards

Glossary

Subroutine

A self-contained block of code that can be called and executed from other parts of a program.

Function Call

A command that directs a program to execute a specific subroutine.

Return Address

The location in memory where a program should resume after a function call.

Stack

A data structure that stores the context of the program during function calls.

Program Counter

A register that holds the address of the next instruction to be executed.

Conditional Jump

A jump instruction that occurs only if a specified condition is met.

Unconditional Jump

A jump instruction that occurs without condition, typically used in function calls.

Reference links

Supplementary resources to enhance your learning experience.