Extra Register!

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Extra Register!

KeithB
Having finished chapter 6 (the assembler), I decided to try something I'd been thinking about for the previous few chapters; namely, using the two unused instruction bits to implement an extra register to reduce the amount of data swapping in and out of the D register. I decided to name it C for counting and to remap the instruction bits as:

1 rc rm c1 c2 c3 c4 c5 c6 da dc dd dm jlt jeq jgt

I chose that as it keeps relevant bits together, even though it shifts some of the existing bit locations.

The additions are rc (read from C register, "rm" is called "a" in the book) and dc (destination C register).
The CPU treats C similarly to M, so if the rc bit is set, C is read instead of A or M. All of the changes were made in the CPU and were fairly straightforward.

I updated the assembler to output in the new format and to recognise the additional mnemonics involving C in place of A or M.

I'm pleased to say, it's worked perfectly, with the following sort of assembly code possible to implement multiplication, performing just four CPU instructions in each iteration of the loop:

@R2 // Load address of R2 (output) into A
M=0 // Set R2 to 0

@R1 // Load address of R1 (2nd operand) into A
D=M // Load value of R1 into D

@R0 // Load address of R0 (1st operand) into A
C=M // Load value of R0 into C

(LOOP)
@R2 // Load address of R2 (output) into A
M=D+M // Increase value in R2 by value in D

@LOOP
C=C-1;JGT // Loop if decremented count > 0

(END)
@END
0;JMP


I'd just like to thank the authors of the book for helping me to grasp the concepts involved in computer hardware to the point that I can attempt changes like this. It really is fantastic material.
Reply | Threaded
Open this post in threaded view
|

Re: Extra Register!

cadet1620
Administrator
KeithB wrote
Having finished chapter 6 (the assembler), I decided to try something I'd been thinking about for the previous few chapters; namely, using the two unused instruction bits to implement an extra register to reduce the amount of data swapping in and out of the D register. I decided to name it C for counting and to remap the instruction bits as:

1 rc rm c1 c2 c3 c4 c5 c6 da dc dd dm jlt jeq jgt

I chose that as it keeps relevant bits together, even though it shifts some of the existing bit locations.

The additions are rc (read from C register, "rm" is called "a" in the book) and dc (destination C register). The CPU treats C similarly to M, so if the rc bit is set, C is read instead of A or M. All of the changes were made in the CPU and were fairly straightforward.

Congratulations on a job well done.

One of the things I really love about this book is how straightforward the Hack architecture is, yet it remains a usable platform that can support quite complex programs.

If you are planning to do the software chapters in the book, you might want to consider making your modified Hack computer machine language compatible with the original so that you can use your assembler to assemble the output from your VM.

I made my assembler output a listing file that showed the address, machine code and source line.

   14 24576      @24576      // SCREEN + size of screen
   15 58576      D=D-A
   16     4      @LOOP
   17 58116      D;JLT       // loop if pScreen still within screen
This was quite useful while debugging the VM since I included the VM source as comments in the generated assembly code.

(It's also incredibly cool when you get to the end of the book and compile/translate/assemble your game and OS with your tools, and run in in the CPU simulator.)

--Mark

[If your assembler works like mine, oring in instruction bits as required, it may be a bit of a pain to invert the sense of the rc and dc bits. A trick that just occurred to me is to generate the instruction with the bits in their non-inverted state and xoring with 0x6000 to invert them.]