Which "jump" do I use?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Which "jump" do I use?

nathan613
I'm doing mult.asm and am getting destination error or sometimes expression error when I try to terminate a loop with jmp. I am confused why sometimes I see in the video instruction: @END, then D; JEQ and other times fpr @END, D; JGT.  In the video - for instance, why was @n, D=D-M followed by @END  D;JEQ and not the other? The particular condition for which  I was trying to terminate the loop was the "i" counter exceeding my limit variable - R1, i.e. one of the factors summing up the other factor R0.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Which "jump" do I use?

ivant
It all depends on what you want to accomplish. JEQ jumps if the out of the ALU is 0 and continues with the next instruction if it's non-zero. Similarly, JGT jumps if out is positive. All the jumps are in fig. 4.5.

If the CPU needs to do the jump, the address to which it jumps to should be stored beforehand in the A register. That's why you see things like

    @END
    D;JEQ

These two instructions tell the CPU to jump to the instruction at label END if the value in the D register is 0. Whereas:

    @END
    D;JGT

would make the CPU go to the END label, if D is greater than 0.

Hint for mult.asm: perhaps there's a better way to do the loop? Like in "reverse"?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Which "jump" do I use?

nathan613
Thanks - I re-read the page and I got more insight, but I am still getting "destination expected". Here is the last half of my program with two jump interludes. I'm not sure which jump the destination error refers to.
(LOOP)
@LOOP
@i
D=M //counter
M=M+1  //increment counter
@R1
D=D-M
(END)
@END
D;JEQ  
R2= R2+R0 //i.e. R2 increases by same addend in R0
@LOOP
0;JMP
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Which "jump" do I use?

cadet1620
Administrator
nathan613 wrote
Thanks - I re-read the page and I got more insight, but I am still getting "destination expected".
"Destination expected" has nothing to do with jumps.
That message means that a command has something on the left side of the "=" that is not understood. It is expecting a destination register: A, D or M, or a combination of those.

In you code sample, the error is
R2= R2+R0
To add the variable R0 to the variable R2, you need several instructions, just like you needed several instructions to do i=i+1 for the counter.

Hint: start with
@R0
D=M

--Mark
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Which "jump" do I use?

nathan613
Thanks, Mark. I reframed the R2 incrementing better, and syntactically,
the program passes. Next - overcoming the logic error(s). I'm getting
comparison failure at "line 3", and I need a hint about what to do. Here
is the entire program:
//Set up the variables
@R2  //Sum
M=0
@7
D=A
@R0
M=D  //R0 = 7 - one factor of 42
@6
D=A
@R1
M=D //R1 = 6, the counter limit factor
@i //the  counter
M=0

(LOOP)
@LOOP
@i
D=M //counter
M=D+1  //increment counter
@R1
D=M-D
(END)
@END
D;JEQ  
@R0
D=M
@R2
M=M+D

@LOOP
0;JMP

> -------- Original Message --------
> Subject: Re: Which "jump" do I use?
> From: "cadet1620 [via Nand2Tetris Questions and Answers Forum]"
> <[hidden email]>
> Date: Thu, September 29, 2016 3:03 pm
> To: nathan613 <[hidden email]>
>
>
> nathan613 wrote
> > Thanks - I re-read the page and I got more insight, but I am still getting
> > "destination expected".
>
> "Destination expected" has nothing to do with jumps.
> That message means that a command has something on the left side of the "="
> that is not understood. It is expecting a destination register: A, D or M,
> or a combination of those.
>
> In you code sample, the error is
>
> To add the variable R0 to the variable R2, you need several instructions,
> just like you needed several instructions to do i=i+1 for the counter.
>
> Hint: start with
>
>
> --Mark
>
>
>
>
> _______________________________________________
> If you reply to this email, your message will be added to the discussion below:
> http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/Which-jump-do-I-use-tp4030316p4030319.html
>
> To unsubscribe from Which "jump" do I use?, visit
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Which "jump" do I use?

cadet1620
Administrator
Your program must not set R0 and R1. They are set by the test script before it runs your program.

Mult.tst runs your program 6 times with various arguments. You can see these arguments and expected product by looking at the Mult.cmp file.

Other than setting R0 and R1, your program looks OK.

Once you have your program working, please edit your post to remove the code.

--Mark
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Which "jump" do I use?

nathan613
Okay _ I removed all specific values for R0 and R1, and ran mult.hack.
It went for some time with no comparison
errors, until finally it looped back and forth indefinitely between D;
JEQ and @END just above. I believe that is what the instructors said
should be the final outcome. Should I assume success?

> -------- Original Message --------
> Subject: RE: Which "jump" do I use?
> From: "cadet1620 [via Nand2Tetris Questions and Answers Forum]"
> <[hidden email]>
> Date: Thu, September 29, 2016 10:28 pm
> To: nathan613 <[hidden email]>
>
>
> Your program must not set R0 and R1. They are set by the test script before
> it runs your program.
>
> Mult.tst runs your program 6 times with various arguments. You can see these
> arguments and expected product by looking at the Mult.cmp file.
>
> Other than setting R0 and R1, your program looks OK.
>
> Once you have your program working, please edit your post to remove the
> code.
>
> --Mark
>
>
>
> _______________________________________________
> If you reply to this email, your message will be added to the discussion below:
> http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/Which-jump-do-I-use-tp4030316p4030321.html
>
> To unsubscribe from Which "jump" do I use?, visit
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Which "jump" do I use?

cadet1620
Administrator
nathan613 wrote
Okay _ I removed all specific values for R0 and R1, and ran mult.hack.
It went for some time with no comparison
errors, until finally it looped back and forth indefinitely between D;
JEQ and @END just above. I believe that is what the instructors said
should be the final outcome. Should I assume success?
You should see the message "End of script - Comparison ended successfully" in the status bar at the bottom the CPU Emulator's window.

Set the Animate dropdown to No Animation and the speed slider to Fast. Then it will only take a few seconds to run the test.

--Mark
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Which "jump" do I use?

nathan613
I did what you said: no animation, and slider to fast, The program said
"Running..." for over two minutes, so I just force stopped the process.
I remember during animation, it ended by bouncing back and forth
endlessly at  @END then D; JEQ. Can you diagnose what I should do?

> -------- Original Message --------
> Subject: RE: Which "jump" do I use?
> From: "cadet1620 [via Nand2Tetris Questions and Answers Forum]"
> <[hidden email]>
> Date: Fri, September 30, 2016 8:34 am
> To: nathan613 <[hidden email]>
>
>
> nathan613 wrote
> > Okay _ I removed all specific values for R0 and R1, and ran mult.hack.
> > It went for some time with no comparison
> > errors, until finally it looped back and forth indefinitely between D;
> > JEQ and @END just above. I believe that is what the instructors said
> > should be the final outcome. Should I assume success?
>
> You should see the message "End of script - Comparison ended successfully"
> in the status bar at the bottom the CPU Emulator's window.
>
> Set the Animate dropdown to No Animation and the speed slider to Fast. Then
> it will only take a few seconds to run the test.
>
> --Mark
>
>
>
> _______________________________________________
> If you reply to this email, your message will be added to the discussion below:
> http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/Which-jump-do-I-use-tp4030316p4030323.html
>
> To unsubscribe from Which "jump" do I use?, visit
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Which "jump" do I use?

cadet1620
Administrator
nathan613 wrote
I did what you said: no animation, and slider to fast, The program said
"Running..." for over two minutes, so I just force stopped the process.
That behavior sounds like you loaded the .ASM file directly instead of the test script.
Load the script (File > Load Script or button with paper scroll) and make sure that you see the test scrip in the main window.

--Mark
Loading...