All,
I posted a newsgroup question here a few weeks back, asking some
questions that related to my 10 year quest (so far) to understand
pointers.
Someone suggested I write a simple emulator. Part of his post is
below. I would have emailed him directly but a valid email wasn't
included.
I'm sure its quite simple for him, but I don't get it! I understand
his suggestion conceptually, but don't have a clue where to start. How
will these 'programs' run? How will the instructions be executed? What
do I do with the instructions? Just print them on the screen? Jeez,
maybe if I was a hardware engineer I might have a clue.
Doug.
"If you want to understand pointers, write a computer. Or rather,
write an
emulator for the non-existent computer of your choice.
Start off simple. A 1-bit computer with 2 1-bit "bytes" of memory, a
1-bit
instruction register (it holds the address in memory of the next
instruction to be executed), and one 1-bit register. Decide on an
instruction set for it (it'll have just two different instructions),
and
then write it. /Then/ write programs for it. (These are easy: 00, 01,
10,
11. All done.)
This can be done in around 300 lines of C code. Less if you're a terse
coder, but mine is 300 lines (excluding a few printfs and comments),
and it
even includes a rudimentary disassembler.
Then write a 2-bit computer. That'll have four 2-bit "bytes" of
memory, a
2-bit instruction register, and perhaps you can be daring and give it
two
2-bit registers. The instruction set can have a massive FOUR
instructions.
My version takes about 350 lines. And the 3-bit version is about 400
lines.
It's not difficult, believe me. And by the time you get up to 8 bits,
you
/will/ understand pointers.
I posted a newsgroup question here a few weeks back, asking some
questions that related to my 10 year quest (so far) to understand
pointers.
Someone suggested I write a simple emulator. Part of his post is
below. I would have emailed him directly but a valid email wasn't
included.
I'm sure its quite simple for him, but I don't get it! I understand
his suggestion conceptually, but don't have a clue where to start. How
will these 'programs' run? How will the instructions be executed? What
do I do with the instructions? Just print them on the screen? Jeez,
maybe if I was a hardware engineer I might have a clue.
Doug.
"If you want to understand pointers, write a computer. Or rather,
write an
emulator for the non-existent computer of your choice.
Start off simple. A 1-bit computer with 2 1-bit "bytes" of memory, a
1-bit
instruction register (it holds the address in memory of the next
instruction to be executed), and one 1-bit register. Decide on an
instruction set for it (it'll have just two different instructions),
and
then write it. /Then/ write programs for it. (These are easy: 00, 01,
10,
11. All done.)
This can be done in around 300 lines of C code. Less if you're a terse
coder, but mine is 300 lines (excluding a few printfs and comments),
and it
even includes a rudimentary disassembler.
Then write a 2-bit computer. That'll have four 2-bit "bytes" of
memory, a
2-bit instruction register, and perhaps you can be daring and give it
two
2-bit registers. The instruction set can have a massive FOUR
instructions.
My version takes about 350 lines. And the 3-bit version is about 400
lines.
It's not difficult, believe me. And by the time you get up to 8 bits,
you
/will/ understand pointers.
Comment