eq implementation in a different way

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

eq implementation in a different way

ajksdf
The following code is what I come up when implementing 'eq' in the StackTest.asm. I doubt that if it is possible coz I cannot get the right answer when I passed it to CPUEmulator. Am I missing sth? Thank you very much in advance.

```
@SP
            A=M-1
            A=A-1
            D=M  //x value

            A=A+1  // M = y value
            D=D-M  //x value - y value, save to D register
           
            @SP
            A=M-1
            A=A-1 // go to the value x address location to prepare for the different condition

            @SET_TRUE
            D; JEQ

            M=0  //if not true, set M=0

            (SET_TRUE)
                M=-1  //if true, set M=-1

            @SP
            M=M-1
```
Reply | Threaded
Open this post in threaded view
|

Re: eq implementation in a different way

WBahn
Administrator
Let's say that when the D;JEQ instruction is executed the jump is not taken.

What are all of the things that happen, in order, from that point until the code reaches the end.
Reply | Threaded
Open this post in threaded view
|

Re: eq implementation in a different way

ajksdf
Ah, I seem to get it.

My code does the following things.
1. execute D; JEQ
1a) if D == 0, goto (SET_TRUE). This makes the M=-1
1b) if D!== 0, execute M=0 FIRST. Then when it will set M=-1
2. A register is set to where SP located
3. Decrement SP value by 1


If it is really the case, I should 1)separate the true and false case and 2)set a non-conditional jump at each case just like below

```
           //...more code on top
           @SP
            A=M-1
            A=A-1

            @SET_TRUE
            D; JEQ

            @SET_FALSE
            0; JMP

            (SET_TRUE)
            M=-1
            @END
            0;JMP            

            (SET_FALSE)
            M=0

            (END)
            @SP
            M=M-1
```
Reply | Threaded
Open this post in threaded view
|

Re: eq implementation in a different way

WBahn
Administrator
You got it.

We humans tend to look at your original code and see what we want to see because we are good at inferring what was meant even if it conflicts with what is actually there. It's a skill that makes communication between people quite efficient and it usually works very well. But computers are intrinsically stupid and they do exactly what we tell them to because they have zero ability to infer intent. So we have to make the conscious effort to override hundreds of thousands of years of evolution in order to see what they see they way they see it.

You new code will work, but it isn't as efficient as it could be. Consider the following pseudocode:

SET D = test evaluation
IF D == 0 JUMP IF_FALSE
<if true code goes here>
JUMP END (omit if only an if() construct)
(IF_FALSE)
<if false code goes here> (omit if only an if() construct)
(END) (omit if only an if() construct)

Reply | Threaded
Open this post in threaded view
|

Re: eq implementation in a different way

ajksdf
WBahn wrote
We humans tend to look at your original code and see what we want to see because we are good at inferring what was meant even if it conflicts with what is actually there
This is a good point. Now I reflected the mistakes I have made in coding. This is absolutely one of the reasons.

For your persudo code, I have got some new insights on the implementation but 2 questions arise.
1. Does 'AM=M-1' having the same effect with 'A=M-1  A=A-1';
2. If question 1 is a valid implementation. Can I have the following implementation
implementation_on_eq.rtf

Reply | Threaded
Open this post in threaded view
|

Re: eq implementation in a different way

WBahn
Administrator
ajksdf wrote
1. Does 'AM=M-1' having the same effect with 'A=M-1  A=A-1';
If you think about it, they can't have the same effect. The first one writes something to some location in RAM while the proposed alternative does not.

Try both pieces of code with some made up data.

A = 837
RAM[837] = 4903

executing

AM=M-1

results in

A = 4902
RAM[837] = 4902

on the other hand, executing

A=M-1  

A = 4901
RAM[837] = 4902

and following that with

A=A-1

yields

A = 4900
RAM[837] = 4902

You need to be willing to kill a few trees and work your code by hand to see if it does what you want it to. While this is especially true when you are first learning this stuff, it remains true, just not as strongly, no matter how long you do this stuff. Working at the assembly language level is so far below how humans think that it takes effort to force ourselves to do it and we are prone to mistakes if we try to do it in our heads. We get a lot better at it with practice, but there will still be times that we simply need to force ourselves to play computer and do it step by step.
Reply | Threaded
Open this post in threaded view
|

Re: eq implementation in a different way

ajksdf
Yeah you are right. 'AM=M-1' won't have the same effect with 'A=M-1  A=A-1'. I misinterpret myself.

What I wanna do is actually get the y value first.

```
//assume the SP points at 258
//eq implementation
```
@SP
AM=M-1 //M is 257.  Then A = 257
D=M    // since A is changed, then the M represent RAM[257], which is y value

@SP
AM=M-1 // A and M is 256
D=D-M  //x - y === 0?

//...the comparison logic down below

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

Re: eq implementation in a different way

ajksdf
The previous reply has been modified a few time.  because I wanna ask few things. This one is now the latest. Thank you very much.