A 2–stack machine for multiplying natural numbers Contents Timothy Murphy

advertisement
A 2–stack machine for multiplying natural
numbers
Timothy Murphy
<tim@maths.tcd.ie>
School of Mathematics, Trinity College Dublin
April 22, 2003
Contents
1 Strategy
2
2 Reading m onto stack 1
2
3 The main loop
2
4 Starting the cycle
2
5 The main cycle
3
6 First part of the cycle
3
7 Second part of the cycle
3
8 The whole program
3
W
9 Appendix: Literate programming
e define a 2-stack machine which implements the
function
[m][n] 7→ [mn],
where [m] = 1m 0. Our construction is expressed as a program in the tiny language S, which we use for defining stack
machines.
1
4
1
Strategy
We start by reading in the first number m and storing it (as 01m ) on the
main stack (stack 0).
Then we enter the ‘main loop’, in which we read in the second number
n bit-by-bit. As we read in each 1, we run through the main stack, writing
out m 1’s, at the same time storing these 1’s on the auxiliary stack (stack 1).
Then when the main stack is exhausted, we ‘rewind’ m from the auxiliary
stack to the main stack, and return to the main loop.
1 h Multiplication.S 1 i ≡
h Read first number onto main stack 2 i
Loop : h Read in bit of second number; if it is 0, write it out and halt 3 i
h Pop 1’s from main stack, write them and push them onto auxiliary
stack 4 i
h Rewind auxiliary stack onto main stack, and jumpto Loop 5 i
2
Reading first number onto main stack
We start by pushing 0 onto stack 0, to mark the bottom of the stack. Then
we read successive bits, pushing them onto stack 0 as long as they are 1’s.
When we meet a 0 we push it onto stack 1, to mark the bottom of that stack,
and move on to the main loop.
At the end of this phase stack 0 holds 01m and stack 1 holds 0.
2 h Read first number onto main stack 2 i ≡
put0 ; push0 ;
read ; push0 ; jump − 2;
pop0 ; push1 ;
This code is used in chunk 1.
3
Starting the cycle
We read in a bit from the second number.
If it is 0 then we are done; we write out 0 and halt.
If it is 1 then we enter the main cycle.
3 h Read in bit of second number; if it is 0, write it out and halt 3 i ≡
read ; jump3 ; write ; halt ;
This code is used in chunk 1.
4
The main cycle
We go through the m 1’s on stack 0, writing out a 1 for each 1, and also
pushing a 1 onto stack 1.
2
When we meet a 0 (at the bottom of stack 0) we push it onto stack 1 to
mark the bottom of that stack.
4 h Pop 1’s from main stack, write them and push them onto auxiliary
stack 4 i ≡
pop0 ; jump4 ;
push0 ; put1 ; jump4 ;
push1 ; write ; jump − 7;
This code is used in chunk 1.
5
Rewinding
Next we ‘rewind’ stack 1 onto stack 0.
5 h Rewind auxiliary stack onto main stack, and jumpto Loop 5 i ≡
pop1 ; jump4 ;
push1 ; put1 ; jumpto Loop ;
push0 ; jump − 6;
This code is used in chunk 1.
6
The whole program
7
Appendix: Literate programming
This little program was written in cweb, Donald Knuth’s implementation of
his concept of ‘literate programming’.
In brief, documentation and program are combined in a single ‘web’ file.
This can then be processed in two ways: by ctangle to produce the program,
or by cweave to produce the documentation.
This document is based on the web file Multiplication.w. The actual
program (in the language S) is produced by
% ctangle Multiplication.w
On the other hand, this document was produced by
% cweave Multiplication.w
producing the LATEX file TuringMachine .tex which can be processed in the
usual way
% latex Multiplication
% xdvi Multiplication
% dvips Multiplication
3
Index
halt : 3.
jump : 2, 4, 5.
jumpto : 5.
jump3 : 3.
jump4 : 4, 5.
Loop : 1, 5.
pop0 : 2, 4.
pop1 : 5.
push0 : 2, 4, 5.
push1 : 2, 4, 5.
put0 : 2.
put1 : 4, 5.
read : 2, 3.
tex : 7.
TuringMachine : 7.
write : 3, 4.
4
List of Refinements
h Multiplication.S 1 i
h Pop 1’s from main stack, write them and push them onto auxiliary stack 4 i
Used in chunk 1.
h Read first number onto main stack 2 i Used in chunk 1.
h Read in bit of second number; if it is 0, write it out and halt 3 i Used in
chunk 1.
h Rewind auxiliary stack onto main stack, and jumpto Loop 5 i Used in
chunk 1.
5
Download