NCL 502: Passing Values

In the previous lesson, we saw how CALL and RET move execution into a subroutine and back.

But execution isn't the only thing that needs to cross a call.

Suppose we've written a subroutine that draws a horizontal line on the Display.

It needs four values:

  • the column;
  • the row;
  • the length;
  • the character to draw.

The caller has those values.

The subroutine needs to get them somehow.

Agreeing on Memory

One simple solution is to reserve some memory.

We could decide that:

#line_x    $100
#line_y    $101
#line_len  $102
#line_char $103

Before calling the subroutine, we fill those locations:

MOVE #line_x 4
MOVE #line_y 3
MOVE #line_len 12
S.MOVE #line_char "-"
CALL :draw_line

Then :draw_line can read them.

This works because both pieces of code agree on what those locations mean.

We can call :draw_line again with different values:

MOVE #line_x 2
MOVE #line_y 7
MOVE #line_len 20
S.MOVE #line_char "="
CALL :draw_line

The same subroutine now does the same work with different data.

For a simple program, this may be perfectly adequate.

But there's a problem hiding in those four locations.

Calls Can Go Deeper

In NCL 501, we saw that subroutines can call other subroutines.

Suppose :draw_line is halfway through its work and needs to call another subroutine.

That subroutine needs some temporary inputs too.

If it uses:

$100
$101
$102
$103

then the values belonging to :draw_line are overwritten.

When the deeper call returns, :draw_line may still need them.

We could reserve a different group of locations for every subroutine.

That works, but the more subroutines we write, the more memory we have to divide up and keep track of.

Nested calls have a useful shape:

The most recent call finishes first.

We already have a place designed for temporary values with exactly that behavior.

Arguments on the Value Stack

Instead of placing the values at fixed addresses, the caller can push them:

PUSH #line_x
PUSH #line_y
PUSH #line_len
PUSH #line_char
CALL :draw_line

A value supplied to a subroutine is called an argument.

Here, :draw_line receives four arguments.

Inside the subroutine, we pop them:

:draw_line
POP #line_char
POP #line_len
POP #line_y
POP #line_x

The order is reversed because the value stack is LIFO.

The caller and subroutine see the same values in opposite orders:

Push order Pop order
1. line_x 1. line_char
2. line_y 2. line_len
3. line_len 3. line_y
4. line_char 4. line_x

We've seen this behavior plenty of times already.

What's new is what we're using it for.

The value stack now carries temporary data from the caller into the subroutine.

CALL doesn't pass the arguments

The arguments are already on the value stack before CALL executes.

CALL only transfers execution and remembers where RET should return.

The caller and subroutine are using the value stack to exchange data.

Using the Arguments

Once the arguments have been popped, they're ordinary values.

For example, :draw_line might use them like this:

:draw_line
POP #line_char
POP #line_len
POP #line_y
POP #line_x

D.CUR #line_x #line_y

:next_char
D.TXT #line_char
DEC #line_len
BGT :next_char #line_len 0

RET

Now the caller can draw as many lines as it likes:

MOVE #line_x 4
MOVE #line_y 3
MOVE #line_len 12
S.MOVE #line_char "-"

PUSH #line_x
PUSH #line_y
PUSH #line_len
PUSH #line_char
CALL :draw_line

and later:

MOVE #line_x 2
MOVE #line_y 7
MOVE #line_len 20
S.MOVE #line_char "="

PUSH #line_x
PUSH #line_y
PUSH #line_len
PUSH #line_char
CALL :draw_line

The data belongs to each call only as long as it is needed.

The subroutine doesn't need four permanently reserved input locations.

Sending a Value Back

Sometimes the caller needs information back from the subroutine.

Suppose we write a subroutine that calculates the average of three integers.

The caller pushes the values:

PUSH #a
PUSH #b
PUSH #c
CALL :average

The subroutine retrieves them:

:average
POP #c
POP #b
POP #a

Then it calculates the result:

ADD #result #a #b
ADD #result #result #c
DIV #result #result 3

Before returning, it pushes that result:

PUSH #result
RET

The caller can retrieve it after the call:

PUSH #a
PUSH #b
PUSH #c
CALL :average
POP #average

The same value stack has carried data in both directions.

The arguments were placed there before the call.

The result was placed there before the return.

RET doesn't return the value

RET returns execution to the caller.

The result is already waiting on the value stack when RET executes.

Once execution returns, the caller can POP it.

Keeping the Order Straight

When you write both sides of a subroutine, the important thing is that they agree.

If the caller pushes:

PUSH #a
PUSH #b
PUSH #c
CALL :work

then :work receives:

:work
POP #c
POP #b
POP #a

If it produces a result:

PUSH #result
RET

the caller retrieves it:

POP #result

There is no new stack rule to learn here.

Values still come off the value stack in the opposite order from which they went on.

We're simply arranging our calls around that rule.

Try It

Write a subroutine named :repeat.

It should receive:

Takes Type
text String
count Integer

and write text to the Display count times.

For example, your program should be able to use the same subroutine to produce:

========
Hello Hello Hello 
*****

using three separate calls to :repeat.

Choose the text and count before each call, push them onto the value stack, then retrieve them inside the subroutine.

Don't give each call its own set of input locations.

Which value gets popped first?

If the caller pushes the text first:

PUSH #text
PUSH #count
CALL :repeat

then #count is on top of the stack.

Still stuck?

Start the subroutine with:

:repeat
POP #count
POP #text
The subroutine only needs one loop

Once the arguments have been popped, the subroutine doesn't care which call they came from.

Display the text, decrease the count, and repeat while there is still work to do.

Still stuck?

The loop can have this shape:

:again
D.TXT #text
DEC #count
BGT :again #count 0
Show me one solution

One solution is:

#text  $000
#count $001

S.MOVE #text "="
MOVE #count 8
PUSH #text
PUSH #count
CALL :repeat
D.TXT "\r\n"

S.MOVE #text "Hello "
MOVE #count 3
PUSH #text
PUSH #count
CALL :repeat
D.TXT "\r\n"

S.MOVE #text "*"
MOVE #count 5
PUSH #text
PUSH #count
CALL :repeat

D.BLT
HALT

:repeat
POP #count
POP #text

:again
D.TXT #text
DEC #count
BGT :again #count 0

RET

Each call supplies different values.

:repeat doesn't need to know where those values originally came from. It only retrieves the two arguments waiting for it on the value stack.


We've seen how a subroutine we write can receive arguments from its caller and place a result back on the value stack.

So far, we've controlled both sides of every call. We knew what to push because we wrote the subroutine that would pop it.

In the next lesson, NCL 503: Subroutine Interfaces, we'll look at how to use a subroutine when someone else wrote it.