# translating eq to asm

12 messages
Open this post in threaded view
|

## translating eq to asm

 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?
Open this post in threaded view
|

## Re: translating eq to asm

 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
Open this post in threaded view
|

## Re: translating eq to asm

 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...
Open this post in threaded view
|

## Re: translating eq to asm

 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
Open this post in threaded view
|

## Re: translating eq to asm

 thanks for the explanation!
Open this post in threaded view
|

## Re: translating eq to asm

 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.
Open this post in threaded view
|

## Re: translating eq to asm

 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
Open this post in threaded view
|

## Re: translating eq to asm

Open this post in threaded view
|

## Re: translating eq to asm

Open this post in threaded view
|

## Re: translating eq to asm

 I'm still not sure I understand the best way to implement the eq, gt, lt operations. I think I need a jump to code that sets the value to true, but if the jump condition is not met, I need to jump past that block of code, so am ending up with the following: @TRUE M;JEQ M=0 // JUMP PAST TRUE BLOCK @END 0;JMP (TRUE) M=-1 (END) Is this correct? What is the comment that things can be done "inline" mean? Also, to keep the TRUE and END labels unique, I need to append a number at the end, correct? I understand that, but was wondering if there's a reason for using \$ and : symbols as in some of the comments above. Finally, for the "bootstrap" method, does that mean having a single @TRUE and @END label to jump to that is appended to the end of every assembly code translation? However, that would still require a jump back to the main code, correct? Thanks!