6.4 Computer
I/O explained DATE
“So, if the
construct Init-Input-Output-Data is rather common, maybe I should
make a template for it.” says Peter. He and the teacher was in the
office during consulting hour.
“Actually,
a more common construct is Input-Function-Output. The Data block is
either usually being loaded on the Init phase, in case of the scope
of the program, or the Input phase, in case of subroutines. You do
know what a subroutine is, right?”
“I know
what a function is.”
“You can
think of subroutine as a function, but in truth, a subroutine is just
a block of code. No more, no less. What is commonly done, is you
assign values to all the variables that you need for that subroutine,
then call the subroutine, then read the variables that is assigned on
that subroutine. Therefore, the structure is Input-Function-Output.
“Furthermore,
you can have multi-level of it. Some of them can be nested quite
deep. Any question?”
“Will that
be a GOTO or CALL command?”
“GOSUB
command. There's a GOSUB-RETURN pair construct. GOSUB tells the
program to go to a certain label that is the subroutine. RETURN
returns the program execution to the original place of the call.”
“And about
GOTO?”
“Goto is
simply changing the execution of the program to another place. You
know about the cursor of character text screen and the cursor for the
DATA?”
“Yes.”
“GOTO
command changes the 'cursor' of the program execution to the defined
label.”
“Seems like
there's quite a lot of cursor going on.”
“You
wouldn't believe it. Every time GOSUB is called, there's the pointer
for the stack changed. Yes, certainly there's a lot of things going
on around.”
“So, how
come none of the computer programming book mention this? I would
think that at least some of them would have mentioned it.”
“Hmmm. Good
question. I think part of it is the notion that a good high level
language hides the complexity of the computer. The higher the
language, the more the complexity is hidden. Another reason is that
the program execution 'cursor' isn't called program cursor. It's
called 'Program Counter'.”
“Program
Counter? That doesn't seem to fit.”
“The
original program is machine language, remember? All it is, is a
number, denoting which line of the program, the CPU needs to execute
next. That's all it is, a number.
“You can
think of it as a pointer that points to the memory address to be fed
to the CPU.”
“So a
program counter is a pointer.”
“Correct.
Just like a variable is a pointer.”
“Huh? How
does that work?” Peter was confused.
“Do you
know why we specify X,Y,WORD$? They act as a placeholder for the
actual values. The actual values aren't X and Y. They're numbers!”
“Yes, I
see.”
“Numbers,
which resides in a certain place in memory. Do you know which of the
memory address contain the values? Well, you look up the entry at X,
and that tells us not the value of X, but the memory address
containing the value of X! So pointers are look up address, or
references! Just for fun, it may be fun to look up all internal
variables, such as TIME$, as well.”
No comments:
Post a Comment