and with switches and lights.
support the same basic programming constructs that Java does.
we've got the arithmetic operations and we've got the bitwise operations.
statements and some math loading stuff in the registers and implementing things.
We have conditionals and loops and we'll talk about that.
that's what opcodes AMB are for and we'll talk about that in the next lecture.
and we have advanced programming constructs as well.
that's what opcodes E and F are about.
we're not taking the time to do it in lecture.
In linked structures as well.
to do an input that with no limit on its length.
perform these kinds of functions with the very few instructions that we have.
binary search trees is in some ways easier to understand than the Java program.
only use 10 to 20 or 30 instructions.
Not very many instructions are all to do really a lot.
and there's plenty of examples of this on the book side and in the book.
So, let's look at conditionals and loops to start.
and that's how we control the flow of instruction execution.
We test the registers value and change the PC depending on the value.
they change the PC to that address.
the absolute value of the number in register A.
if it's positive then set the PC to memory location 12.
next instruction is 11 and then so 12 would be skipping the next instruction.
And what's the next instruction?
so it subtracts register A from zero.
So if it was negative that'll make it positive and then puts the result into register A.
So that's a two instruction program that computes the absolute value.
And a typical loop works as follows.
we go to 15 that's branching out of the loop.
"Two is subtract, one has the value 0 0 1."
the result back in A that's decrementing register A by one.
do a branch if zero on register zero which always succeeds and sets the PC to 10.
a while loop that decrements register until zero.
of constructs that we use all the time in a higher level languages like Java.
And we'll give the example of a program that does this in just a minute.
So that's the kind of Java-ish expression of the same code.
too persnickety about the correspondence.
way, way more than we could do without being able to control the flow.
just a couple of TOY instructions to get us there.
But the other thing is as input and output.
people knew right away is just not going to work.
to have some way to get data from the outside world into the machine.
and that's actually the method that I use for a couple of years.
some I/O devices that could read and write a paper tape.
standard out that are familiar to you.
come in what's called fan folded, it will be folded.
we think of it as unbounded in length you could read as much as you want.
And it's got holes punched in it.
We encode our 16-bit words in two rows on the tape.
then 0 0 1 1 and then 0 1 1 1.
sprockets that are used to drive the tape through the tape reader.
just punch a hole corresponding to each one.
what's supposed to be one.
And That's it. Very simple device.
And how do we access this with a TOY program?
and so memory location FF is not really a memory location.
the holes corresponding to the values of the word that you said you'd store.
And to read, you'd do the same thing.
put that in as if it was came from memory.
magnetic tape which could hold way way more data and go a lot faster.
But from the point of view of the programs no change at all.
It's like standard input and standard output.
I think of the input stream and the output stream is infinite.
Within my program I don't have any bound I just write FF or just read from FF.
and we'll talk about the impact of this in the next lecture.
that illustrates the flow of control in standard output.
So what this program does is print on paper tape the Fibonacci numbers.
and the excitement of being able to actually compute things that you didn't know.
to learn things really about math.
And so lots of the early programs were of this form.
some kind that is going onto the paper tape for later processing.
And certainly, there was quite a bit of that as well.
But let's look at the example.
there's lots of things you could do with this computer.
This is just a 13 instructions to get this job done.
So let's look at what it does.
and I haven't talked about instruction seven that's a load address instruction.
the register and then clear out the upper bits of the register with zero.
So that's a quick way to get a value into a register.
and then register nine is going to get the location of whatever's in memory location 4C.
If you look down at 4C it's got the value A.
"We want to punch out 10 Fibonacci numbers."
put a of course a much larger number if we wanted.
So that's going to be a counter for our loop that's in.
our loop or that corresponds to i in the pseudo code right.
So now we test if i is zero or register nine is zero.
This is our while loop construct.
nine means register nine and then 4B is down at the halt instruction.
So this is going to be our loop.
"Just write register A standard out.
"That's just 9 A F F and we execute that.
We punch out a one.
So, it's A and B are the two previous.
and then, move B to A and C to B.
they we'll always keep track of.
And then, decrement register 9.
And now, we'll just show what happens every time through the loop.
and C is the sum of the two previous.
And then, we update the two previous.
or a thousand, or all the way up to 32,000 things that we could punch out.
And it could be a much more complicated number than the Fibonacci.
And then, finally we come to the halt instruction.
many of the computational problems that we addressed early on in Java.
there's really a lot you can do.
You'll find plenty of examples in the book.
that it is a complete specification of what happens in the computer.
it specifies exactly what TOY will do if you get that instruction.
and they're all described with examples on the book site, and in the book.
But this is a complete description of what goes on in TOY.
a full description of what you needed to know while programming.
Sometimes, the cards got really quite detailed.
and there was lots and lots of information on it.
and had to have quick reference to when they looked at what's in the memory.
do if it happened to interpret that thing as an instruction.
this is what you have to work with.
and I encourage everybody to learn more about it.
you have to be able to answer questions of this sort.
So, I give you a 16 bit number.
What is that number if it's interpreted as a TOY instruction?
Or as a two's compliment integer value for all different kinds of numbers.
We're going to add register 7 and 5, put the result in A.
Well, that's a 1, it's positive.
1 times 16 cube plus 10 times 16 squared plus 7 times 16 plus 5.
And actually, this kind of conversion is an easy programming exercise.
these kinds of conversions where you could write a TOY program to do it even.
And that's it, rest of it's ignore.
What about as a two's compliment?
that's 16 cube and subtract 1,16 cube 4,096 subract 1 and get 4,095.
8888, well, 8 is load instruction.
and 88 means memory location 88.
And as a two's complement integer value.
Well, you can do it by converting to binary and so forth, reverting back.
But actually, the idea of flipping all the bits and adding 1 works in any number system.
1 less than the base.
short answer question that you might find on a quiz or exam on this material.
or as a data value.
how do you flip all the bits in a TOY register?
and that's kind of programming trick that every programmer knows.
Everybody who programs at this level knows.
And then, you should be able to figure out what small programs do.
And that might seem amazing to you but this programming model is really simple.
and it won't take you long to figure out.
and that so, it computes 1024.
So, that's another type of question that anybody can answer after seeing this lecture.
So, how does TOY really relate to your actual computer?
conditionals, loops, and other low level constructs.
arrays, and other high level constructs.
They both have infinite input and output streams.
TOY implements just as well as your laptop does.
is that really enough to do anything useful at all?
It's such a small computer.
and actually, we'll talk next time that absolutely 256 words is enough.
all fit in the same memory.
And we'll talk more about this next time.
then TOY becomes a Turing machine.
It means you can compute anything that any computer can compute.
So, TOY is a very strong sense equivalent to your laptop.
developed in the last 50 years have all of these same characteristics.
It's really, really quite amazing phenomenon.
and we want more memory when we can afford it.
is that that's always true.
You want a faster laptop with more memory.
have been a continuous development of providing that.
But what the computer does basically, not much change.
That's what we're going to look at more next time.