Nested loop

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

Nested loop

lllllllalalala
This post was updated on .
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Nested loop

WBahn
Administrator
The CPU doesn't know anything about loops at all. It knows how to execute each of the 28 defined instructions in the Hack language and that's pretty much it.

What you need to do is figure out the housekeeping associated with executing a loop and then apply that to where you need a loop -- even if it is within another loop.

i = i_start
while (i < i_stop)
{
    // arbitrary code goes here
    i = i + 1
}

This will translate into three sections of Hack code

// Code that handles the setup and top of the loop
...
// Code that does the arbitrary task
...
// Code that handles the bottom of the loop

The arbitrary code can contain a loop.

...
Reply | Threaded
Open this post in threaded view
|

Re: Nested loop

lllllllalalala
Thank you! I tried to write this and loaded program, but although I wrote a lot, there is nothing when I click that document and tried to run as usual. Does this mean there are some mistakes in my program that this emulator cannot recognize? (Because before even if there were mistakes, the code can be displayed in ROM column, however, this time there is nothing)
Reply | Threaded
Open this post in threaded view
|

Re: Nested loop

WBahn
Administrator
You probably have an error that the assembler can't handle.

Write your code a little bit at a time and test it incrementally.
Reply | Threaded
Open this post in threaded view
|

Re: Nested loop

lllllllalalala
This post was updated on .
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Nested loop

WBahn
Administrator
This post was updated on .
How should I know? I don't even know what that long list of instructions is supposed to accomplish. In your first post you talked about b to the power of a. Is this related to that? If so, your code references x and y. Is it x to the y or y to the x? Or is this multiplying x and y? What? I'm not a mind reader.

But just looking at your first three blocks of code I don't think it is doing what you want it to, whatever that happens to be.

@R0
D=M
@x
M=D //R0 = x

The comment "R0 = x" implies that you are trying to change the value stored in R0 and set it equal to the value in the variable x. But you are doing just the opposite (which is what I suspect you actually want). You really should use the normal convention that the value on the left of the assignment operator is the target of the operation. So the comment should be "x = R0". Similarly, the comment in the block below should be "y = R1".

@R1
D=M
@y
M=D //R1 = y

Now you use the normal convention for the comment "D=insum". At the very least, you need to be consistent.

@insum
D=M //D=insum
@x
M=D //insum = x

So does "insum = x" mean you want to change the value stored in insum, or the value stored in x? Since your comments have gone both ways, it's impossible to know. Again, I'm not a mind reader.

But if you want to change the value in insum, wouldn't the address of insum need to be in the A register? If your code is correct, then you are wanting to change the value in the variable x. But if so, then what was the point of the first block of code?

If you had tested your code after writing these three block of code and checking to see if the values stored in the various memory locations were what you wanted them to be, you could have found and fixed this issue before proceeding to write a bunch of code that was pretty much guaranteed to never work.

Again -- small steps, incremental development, and frequent testing are the keys to success.

Reply | Threaded
Open this post in threaded view
|

Re: Nested loop

lllllllalalala
Oh Thank you so much. I'll check myself first