- Introduction:
Although a computer program is
one unit, but:
- a computer program is
logically partitioned
into multiple units called
subroutines/methods
to reduce the
complexity
(Humans can't handle things that are
too large)
By dividing up a complex program into
units (subroutines) that
perform
specific (and more simpler) tasks,
we can reduce the
program complexity and
write bug-free programs
|
So, although subroutines are
in the same program,
you must view each
subroutine as
a separate and independent
execution unit
- All subroutines (must) use in their computations:
Each subroutine
will need to:
- Use
the CPU register to
perform
computations
(because that's how the CPU operates !)
|
It is important that you
understand that
the CPU registers are
resources
shared by
all subroutines:
- Each
subroutine can
access all the
CPU registers
|
- Main caveat in using
registers:
- The value in
any register
used by a subroutine
A can
be changed
after A
calls
a subroutine
B !!!
|
Example:
- main set
the register r0 to
4 and
call
subroutine subroutine1 that
changes
register r0:
- When
subroutine subroutine1
returns to
main, the
register r0 will
have changed !!!
|
This can happen with
any subroutine call.
Another example:
- subroutine1 set
the register r0 to
7 and
call
subroutine subroutine2 that
changes
register r0:
- When
subroutine subroutine2
returns to
subroutine1, the
register r0 will
have changed !!!
|
- Moral (programming rule) of this
story:
- If a sub-program has an
important value stored in
a register, then:
|
We will learn how to save value in registers
later
- But the above rule can
help use understand
that we can use registers
to store
parameters and local variables
for leaf functions
(that do not call
any function)
storing
parameters and local variables is
simple and efficient,
we will discuss this technique first.