translating eq to asm

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

translating eq to asm

amber
This post was updated on .
I an trying to translate eq to asm so that the number of asm lines will be minimal (without reference gt or lt) but I can't seem to write it in less than 11 lines:

[Source code removed by admin.]

is there a shorter way if writing it?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: translating eq to asm

cadet1620
Administrator
Your code as posted is 10 instructions (labels don't count), but it is computing "not equal".

Put an M=-1 after the MD=D-M so that the return value is -1 if x-y==0, and change the M=-1 before (END) to M=0 so that the x-y!=0 case returns 0. This makes it 11 instructions.

My best is also 11 instructions, coded a bit differently.

The shortest code that you can write for an eq, gt, or lt command is an assembly language call to a common routine that your vm writes in the bootstrap:

    @$RIP$nnn
    D=A
    @$$EQ   // or $$GT or $$LT
    0;JMP
    ($RIP$nnn)

This is only 4 instructions per vm statement. The $$EQ function does all the work. It's a bit more than 11 instructions because it needs to save the RIP in D before the compare and jump to the RIP afterwards.

This same Assembly language subroutine is really helpful with call and return code in project 8; that code is a lot longer than the compare code!

[Please edit your post to remove the (mostly) functional code. We want students to develop their own solutions.]

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

Re: translating eq to asm

amber

Thanks for the quick reply!
According to you're answer true is stored as -1 and false as 0. I thought it was the other way around...
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: translating eq to asm

cadet1620
Administrator
amber wrote
According to you're answer true is stored as -1 and false as 0. I thought it was the other way around...
You are not the first person to get this backwards.

Chapter 7 says, "The VM represents true and false as -1 (minus one, 0xFFFF) and 0 (zero, 0x0000), respectively."

I don't like to use the "respectively" sentence structure in documentation for just this reason; it causes confusion because the value assignments are not next to value names.  I would have written this as,

"The VM represents true as -1 (minus one, 0xFFFF), and false as 0 (zero, 0x0000)."

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

Re: translating eq to asm

amber
thanks for the explanation!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: translating eq to asm

linse
In reply to this post by cadet1620
Thanks for the clarification on this.

In the bottom right-hand corner of slide 14 of the lecture pdf, it states: "actually true and false are stored as 0 and -1, respectively."

Unless I'm being immeasurably dense, this (slide 14) contradicts chapter 7 as you've cited it, and the behaviour of the VM Emulator.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: translating eq to asm

cadet1620
Administrator
No, you are not being dense...

There is definitely a problem with the slides. Chapter 7 slides 11 and 14 and Chapter 9 slide 20 all misstate the values for true and false.

Book Chapter 7 (7.2.2) and Chapter 11 (11.2.1 § Constants) define true=-1 and false=0.

You can also see that the supplied JackCompiler matches the book by compiling this program and running it in the VM Emulator.
// File: Main.jack
class Main {
    function void main() {
        var bool x;
        let x = true;
        do Output.printString("true = ");
        do Output.printInt(x);
        do Output.println();
        let x = false;
        do Output.printString("false = ");
        do Output.printInt(x);
        return;
    }
}
It outputs "true = -1 false = 0".

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

Re: translating eq to asm

rgravina
In reply to this post by cadet1620
I had a fair bit of trouble with eq also, but this post helped thanks :)

Basically, I found using a function worked for me. Here's what I did:

At the site of the eq instruction:

1. Store arg2 - arg1 in @R13. This can be used for an eq, lt or gt comparison (later)
2. Create a label ($$RIP:n) at the end of the instruction, where n is some globally incrementing value (to ensure label is unique). Load this into @R14.
3. @SP should be pointing to the address after the top value of the stack (should already be like this)

Then, at the top of the program, create a label ($$EQ). Jump to here after doing the above. This will look at @R13 (is it zero, negative or positive?) and then set @SP - 1 to either -1 or 0. Then, you jump back to the address stored in @R14.

It might be possible to use D and A more to avoid the accesses to @R13 and @R14, but I found A was being constantly overwritten (whenever you want to access RAM or jump) and so was D for various calculations.

After implementing eq, gt and lt should be very similar.

Hope this helps and is not giving too much away! I think it would be nice if the book had a small hint, like "hint: to implement eq, gt and lt you will need to jump and have some way of knowing where to return to". At this point in the book we haven't been introduced to assembly functions, so it might be useful to introduce them (perhaps in Chapter 4? If I missed it, excuse me!). The assembly language only allows comparisons as part of a jump, so it might be possible to infer this, but myself (and I think others) were wondering if it was possible to do with instructions only.

Loading...