I follow the excellent Let Build a Compiler method by Jack Crenshaw found at http://compilers.iecc.com/crenshaw . I am testing the generated 68k build using the Easy68k http://www.easy68k.com/ 68000 / assembler / simulator editor. I got part 2 http://compilers.iecc.com/crenshaw/tutor2.txt , but the Divide procedure does not work properly for me.
... { Recognize and Translate a Divide } procedure Divide; begin Match('/'); Factor; EmitLn('MOVE (SP)+,D1'); EmitLn('DIVS D1,D0'); end; ...
If I introduce "8/2" as a test, then the compiler generates the following code:
MOVE #8,D0 MOVE D0,-(SP) MOVE #2,D0 MOVE (SP)+,D1 DIVS D1,D0
It seems to me that it actually calculates 2/8 (that is, the wrong path), since the value left in D0 is then 00020000. I can fix this by rewriting the last line as DIVS D0, D1, but this leaves the result in D1, not D0, as in other subroutines, and it seems unlikely to me that such seed work would be wrong. I searched on the Internet, but I do not see anyone else having this problem. That is, it means: 1) I did it wrong - probably 2) Jack did it wrong - unlikely 3) The Easy68k emulator does something wrong - it is unlikely However, I just donโt see what I did wrong. Please, help.
source share