How do we confirm/test the programs in this chapter?

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

How do we confirm/test the programs in this chapter?

burge91
I'm a little confused how we verify our program performs correctly. For example does it leave the stack in its proper condition? And are we saving the data to the correct memory location? I have for example written some ASM code for the "add" VM command, how do I test this in isolation?
Reply | Threaded
Open this post in threaded view
|

Re: How do we confirm/test the programs in this chapter?

WBahn
Administrator
To test it in isolation, you would have to write your own test script. This isn't too hard -- look at the existing scripts as a starting point.

The five provided test scripts progressively test more and more of the VM implementation. The first one only tests the implementations of push constant i and add.

The next one tests all of the arithmetic-logical commands and, in the process, confirms that they are pushing/popping the stack properly.
Reply | Threaded
Open this post in threaded view
|

Re: How do we confirm/test the programs in this chapter?

burge91
i think im going to have to reread this chapter a lot before i can do the tasks. there seems to be a lot. im not sure if there are some details left out but it doesnt seem as clear as other chapters what exactly i should be doing.
Reply | Threaded
Open this post in threaded view
|

Re: How do we confirm/test the programs in this chapter?

WBahn
Administrator
Take it one step at a time and kill lots of trees (i.e., sketch memory maps out on paper and walk through how they need to change between the start of a command and the end.

For instance, taking the first two commands that get tested, if you have

SP = 304

That means that the value currently on top of the stack is whatever is located at RAM[303] (since the Stack Pointer points to the first unused memory location on the stack).

Since RAM always has something in it, let's just make some stuff up.

RAM[0] = 304
RAM[300] = 7827
RAM[301] = -1835
RAM[302] = 10498
RAM[303] = 838
RAM[304] = -418
RAM[305] = -20398

So the top of the stack has the value 838 in it.

Now let's say that you execute the command

push constant 42

What should the memory addresses in the above list become?

What sequence of assembly language instructions are needed to make that happen?

Next consider what should happen if the command

add

is executed.

What changes need to happen to the memory? What sequence of ASM instructions will make that happen?
Reply | Threaded
Open this post in threaded view
|

Re: How do we confirm/test the programs in this chapter?

burge91
I'm mainly concerned about what I should be doing with the stack pointer. Since memory is all zero to start with, when I test my programs the stack pointer points to zero, which causes the program to fail. (I don't know if I'm just confused here or whether my question makes sense? In the past I have come here asking questions that don't make sense, could be a psychological made up roadblock).

To answer your questions. It would overwrite -418 with 42, then point to ram[305].

ASM instructions I imagine are:

@SP
A=M
@42
M=A
@SP
M=M+1

Is this correct?

My add program doesn't quite work I think because of my stack issue.
Reply | Threaded
Open this post in threaded view
|

Re: How do we confirm/test the programs in this chapter?

WBahn
Administrator
burge91 wrote
I'm mainly concerned about what I should be doing with the stack pointer. Since memory is all zero to start with, when I test my programs the stack pointer points to zero, which causes the program to fail. (I don't know if I'm just confused here or whether my question makes sense? In the past I have come here asking questions that don't make sense, could be a psychological made up roadblock).

To answer your questions. It would overwrite -418 with 42, then point to ram[305].

ASM instructions I imagine are:

@SP
A=M
@42
M=A
@SP
M=M+1

Is this correct?
Well, let's kill a (virtual) tree and find out.

At the beginning of your code segment, we don't know what's in A or D, so we will use question marks for those:

// D = ?
// A = ?
// RAM[0] = 304
// RAM[304] = -418

@SP

// A = 0

A=M

// A = 304

@42

// A = 42

M=A

// RAM[42] = 42

@SP

// A = 0

M=M+1

// RAM[0] = 305

Does this do what you want it to?

If not, do you see where the problem is?

My add program doesn't quite work I think because of my stack issue.
How are you testing things? Are you just running it, or are you using the test script?

Look at the test script:

// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/07/StackArithmetic/SimpleAdd/SimpleAdd.tst

load SimpleAdd.asm,
output-file SimpleAdd.out,
compare-to SimpleAdd.cmp,
output-list RAM[0]%D2.6.2 RAM[256]%D2.6.2;

set RAM[0] 256,  // initializes the stack pointer

repeat 60 {      // enough cycles to complete the execution
  ticktock;
}

output;          // the stack pointer and the stack base
The first thing it does (after setting up the file and output parameters) is to force the SP to point to 256.

This is because, as explained in the text, the first few test scripts perform stack initialization for you because you haven't gotten to the implementation of the bootstrap code yet.

So if you are just running your code without a test script, you are responsible for making sure that the SP gets properly initialized.
Reply | Threaded
Open this post in threaded view
|

Re: How do we confirm/test the programs in this chapter?

burge91
thanks for your response, I haven't been able to answer your questions (havent tried) but I have successfully tested the first program SimpleAdd so seem to have implemented push constant x and add correctly.

The main confusion I had is that I have to use the CPUEmulator now not the VM emulator. I couldn't work that out, even though it says it.

I will work through the rest of the chapter. I am already very confused about the segments. But we'll see what happens. I think I just need to reread it a lot as I'm not the most literate person. Thanks for your help.
Reply | Threaded
Open this post in threaded view
|

Re: How do we confirm/test the programs in this chapter?

WBahn
Administrator
The memory segments can, indeed, be confusing. In part this is because there are different types of segments and each type takes a different approach to implement it correctly.
Reply | Threaded
Open this post in threaded view
|

Re: How do we confirm/test the programs in this chapter?

burge91
In reply to this post by burge91
thanks finished this chapter now. had to reread it a lot. but it was simpler than i expect, which is often the problem with me. enjoyed it and thanks again for your help. Onto Chapter 8!

I think I will buy edition 2 of this just out of nostalgia and as a kind of collector. My dream is that they release a new book that gets us building something else. Maybe an 8 bit computer or even a 32 bit. (probably 8 would be better). This style of learning is brilliant, if you know any other books that are like this and clear and get you doing things please share.
Reply | Threaded
Open this post in threaded view
|

Re: How do we confirm/test the programs in this chapter?

WBahn
Administrator
There is a book that I bought a while back that seems to be in this vein. It has you build an interpreter for a python-like language. I started reading it and then got side tracked, but it looked pretty decent. It actually has you write the interpreter two ways (or, probably more accurately, give you the choice to doing it one of two common ways) so it's really two books in one. If I run across it I'll try to remember to post a link.

Reply | Threaded
Open this post in threaded view
|

Re: How do we confirm/test the programs in this chapter?

ashort
Reply | Threaded
Open this post in threaded view
|

Re: How do we confirm/test the programs in this chapter?

WBahn
Administrator
I'm pretty sure that's the text that I was referring to. Thanks.