NCL 503: Subroutine Interfaces
In the previous lesson, we wrote both sides of every subroutine call.
We knew what values to push because we wrote the POPs that received them. We knew what came back because we wrote the code that produced it.
But we won't always write every subroutine we use.
Suppose a friend gives us a small library of useful number routines.
There's nothing particularly fancy about the library. It's NCL source that we paste into our program.
We didn't write it, though.
Somebody Else's Code
One of the routines calculates the greatest common divisor of two integers.
Here's the code:
:gcd
POP #gcd_b
POP #gcd_a
:gcd_loop
BEQ :gcd_done #gcd_b 0
MOD #gcd_r #gcd_a #gcd_b
MOVE #gcd_a #gcd_b
MOVE #gcd_b #gcd_r
JUMP :gcd_loop
:gcd_done
PUSH #gcd_a
RET
We want to use it.
So... what do we push?
We can figure that out.
The routine pops two values:
POP #gcd_b
POP #gcd_a
Since the value stack is LIFO, the caller must have pushed #gcd_a first and #gcd_b second.
At the end, the routine pushes one value:
PUSH #gcd_a
RET
So after the call, we can pop one result.
That gives us:
PUSH 42
PUSH 30
CALL :gcd
POP #divisor
It works.
But we had to read through somebody else's implementation just to learn how to call it.
For a small routine like this, that's merely inconvenient.
A library might contain hundreds of routines.
There has to be a better way.
Describing the Call
The author could have given us this:
GCD
| Takes | Type |
|---|---|
lhs |
Integer |
rhs |
Integer |
| Returns | Type |
|---|---|
divisor |
Integer |
That's much shorter than the implementation.
More importantly, it tells us everything we needed to discover.
GCD takes two integers, in this order:
| Order | Argument |
|---|---|
| 1 | lhs |
| 2 | rhs |
So we push them in that order:
PUSH 42
PUSH 30
CALL :gcd
It returns one integer:
| Order | Return |
|---|---|
| 1 | divisor |
So we pop it:
POP #divisor
We don't need to know which memory locations the routine uses internally.
We don't need to know that it uses MOD.
We don't even need to know how the greatest common divisor is calculated.
This description is the subroutine's interface.
An interface describes what a caller needs to know to use a subroutine.
Using an Interface
The same library contains another routine:
LCM
| Takes | Type |
|---|---|
lhs |
Integer |
rhs |
Integer |
| Returns | Type |
|---|---|
multiple |
Integer |
The implementation is still available to us.
Library implementation
:lcm
...
But we don't need to open it.
If we want the least common multiple of #a and #b, the interface already tells us how to make the call:
PUSH #a
PUSH #b
CALL :lcm
POP #multiple
The implementation could be ten instructions long.
It could be a hundred.
As long as the interface remains the same, the caller doesn't need to care.
We've Been Doing This Already
This isn't an entirely new idea.
Consider an NCL verb such as:
POW <dst> <base> <exponent>
That signature tells us how to use POW.
We know what each operand means and where the result will go:
POW #result 3 4
We don't need to know how the processor calculates the power.
The implementation is on the other side of a boundary we don't need to cross.
A subroutine interface gives us a similar boundary in software.
We can know how to use a piece of code without knowing how that code works.
Taking the Source Away
Let's go a little further.
The library also provides this:
DIVMOD
| Takes | Type |
|---|---|
dividend |
Integer |
divisor |
Integer |
| Returns | Type |
|---|---|
quotient |
Integer |
remainder |
Integer |
This time, we're not going to look at the implementation at all.
Suppose:
#items $000
#box_size $001
#boxes $002
#leftover $003
MOVE #items 137
MOVE #box_size 12
We want to know how many complete boxes we can fill and how many items remain.
The interface says that DIVMOD takes the dividend first and divisor second:
PUSH #items
PUSH #box_size
CALL :divmod
It returns the quotient first and remainder second:
POP #boxes
POP #leftover
That's enough.
After the call:
boxes = 11
leftover = 5
We haven't seen a single instruction inside :divmod.
We don't need to.
Order is part of the interface
It wouldn't be enough to say that DIVMOD takes two integers and returns two integers.
These values have different meanings.
| Order | Takes | Returns |
|---|---|---|
| 1 | dividend |
quotient |
| 2 | divisor |
remainder |
Their order is part of the information the caller needs.
Knowing Even Less
Here's one more routine from the library:
ISPRIME
| Takes | Type |
|---|---|
value |
Integer |
| Returns | Type | Description |
|---|---|---|
result |
Integer | 1 if value is prime; 0 otherwise. |
That's all we're going to say about it.
If we want to test #candidate:
PUSH #candidate
CALL :isprime
POP #result
We don't know whether :isprime tries every possible divisor.
We don't know whether it stops at the square root.
We don't know what memory it uses while it's working.
We don't even need to know how we would write it ourselves.
We know what it expects.
We know what it produces.
That's enough to use it.
Standing on other people's code
Real software development works like this constantly.
There are libraries for mathematics, graphics, networking, compression, databases, file formats, user interfaces, audio, games, and just about anything else programmers have needed often enough to package up.
You usually don't study every implementation before using one. You learn its interface: what it provides, what you give it, what it gives back, and any rules you need to follow.
This idea is much older than modern package managers. C programmers have been writing:
#include <stdio.h>
for decades.
Yes, even printing text is something you can get from a library.
Larger programs may build on dozens or hundreds of libraries, which themselves build on other libraries. Some languages make this especially visible through long collections of headers or imports near the top of a source file.
This enormous body of reusable software is one of the reasons we can build increasingly complicated programs without starting from scratch every time.
We stand on the shoulders of giants.
Some of those giants know how to print things.
Changing the Implementation
There's another advantage to separating an interface from its implementation.
Suppose the author of our number library decides that :gcd is terrible.
They throw away the implementation we saw earlier and write a completely different one.
But it still has this interface:
GCD
| Takes | Type |
|---|---|
lhs |
Integer |
rhs |
Integer |
| Returns | Type |
|---|---|
divisor |
Integer |
Our code:
PUSH #a
PUSH #b
CALL :gcd
POP #divisor
doesn't need to change.
The implementation changed.
The interface didn't.
Code that depends on an interface doesn't need to depend on the implementation behind it.
Try It
You've been given a number library.
For this exercise, you don't get its source.
You get its interfaces.
GCD
| Takes | Type |
|---|---|
lhs |
Integer |
rhs |
Integer |
| Returns | Type |
|---|---|
divisor |
Integer |
LCM
| Takes | Type |
|---|---|
lhs |
Integer |
rhs |
Integer |
| Returns | Type |
|---|---|
multiple |
Integer |
DIVMOD
| Takes | Type |
|---|---|
dividend |
Integer |
divisor |
Integer |
| Returns | Type |
|---|---|
quotient |
Integer |
remainder |
Integer |
ISPRIME
| Takes | Type |
|---|---|
value |
Integer |
| Returns | Type | Description |
|---|---|---|
result |
Integer | 1 if value is prime; 0 otherwise. |
You have a fraction:
#numerator $000
#denominator $001
MOVE #numerator 42
MOVE #denominator 30
Use GCD to reduce the fraction to its simplest form.
You don't need to implement :gcd.
Assume the library already provides it.
What does GCD need?
Its interface says:
| Order | Takes |
|---|---|
| 1 | lhs |
| 2 | rhs |
You already have two suitable values.
Still stuck?
Push the numerator and denominator, then make the call:
PUSH #numerator
PUSH #denominator
CALL :gcd
What comes back?
GCD returns one value: the greatest common divisor.
Retrieve it from the value stack.
Still stuck?
Give the result somewhere to go:
#divisor $002
POP #divisor
Reducing the fraction
Once you know the greatest common divisor, the library has done its part.
Divide both parts of the fraction by it.
Still stuck?
DIV #numerator #numerator #divisor
DIV #denominator #denominator #divisor
Show me one solution
#numerator $000
#denominator $001
#divisor $002
MOVE #numerator 42
MOVE #denominator 30
PUSH #numerator
PUSH #denominator
CALL :gcd
POP #divisor
DIV #numerator #numerator #divisor
DIV #denominator #denominator #divisor
The result is:
numerator = 7
denominator = 5
We used an algorithm somebody else wrote without needing to know how that algorithm was implemented.
We've seen how an interface lets us use a subroutine without knowing what happens inside it.
We know what values it expects. We know what values it returns. The implementation can remain somebody else's problem.
But arguments and results aren't the only memory a subroutine may use while it's working.
In the next lesson, NCL 504: Scratch and Preserved State, we'll look at what a subroutine is allowed to overwrite.