Contents | 1 | 2 | 3 | 4 | 5 | 6 | 7 | Previous | Next

## 4. The Workspace

The next few examples illustrate the use of the workspace for storing data.

 Grabbing Space Top

Load the example program work1 into the simulator. You should see:

```         60
bd    ajw -3
b3    ajw 3
```

The first instruction is two bytes long; ajw -3 means "adjust the workspace pointer by -3 words". If you press step, you should see that Wptr in the Register panel has changed by -12 bytes, which is -3 words. The red marker in the Data panel should also appear at a position three words down from the top.

This gives the program 3 words of data memory which it can use, numbered 0, 1 and 2, counting upwards from the workspace pointer Wptr.

The second instruction in the program is ajw 3 which moves Wptr back to the end of the memory, effectively giving back the space which it grabbed at the beginning. Later we will see that each procedure grabs some more space for itself by moving the workspace pointer down from whatever its current position was, and that it gives that space back when it returns by readjusting the workspace pointer.

 Storing Intermediate Values Top

With many expressions, you can get away with using the three data registers Areg, Breg, Creg. However, some are too complicated. For example, suppose you want to evaluate the expression:

```         (1 + 2) * (3 + 4) + (8 - 5) * (6 + 1)
```

You would need four registers to calculate this without using any memory locations. You need to store an intermediate value, say the first half, while calculating the second. If you load up the program work2 into the simulator, you should see:

```         60
bf    ajw -1
.....
d0    stl 0
.....
70    ldl 0
b1    ajw 1
```

The first instruction reserves a single word of workspace. The next few instructions calculate (1+2) * (3+4), resulting in 21 in Areg. The stl (store local) instruction means store the value from Areg into the workspace, at position 0 relative to Wptr, ie the location that was reserved earlier. The value is deleted from Areg, and the other two data registers are moved up, as with arithmetic operations.

This saves the intermediate result while the next part of the expression is evaluated. The next few instructions calculate (8 - 5) * (6 + 1) , leaving the result, 21, in Areg.

The ldl (load local) instruction copies the value from the workspace into Aerg. The word copied is at position 0 relative to Wptr, ie the value 21 that was stored away earlier. Now the two halves of the expression are in Areg and Breg ready for the final addition. The program ends by tidying up, readjusting the workspace pointer to give the space back.

 Assignment Top

Slide 15. This example carries out a complete assignment as a sequence of load, arithmetic and store instructions.

Contents | 1 | 2 | 3 | 4 | 5 | 6 | 7 | Previous | Next