IUCompilerCourse / public-student-support-code

Public helper code for p423/p523 students (Racket)
MIT License
154 stars 67 forks source link

question on `subq` argument ordering #2

Open xysun opened 3 years ago

xysun commented 3 years ago

Hello,

I'm following the Essentials of Compilation book together with this code for the exercises. I find the ordering of subq argument is giving me surprising result, I feel I must be making a silly mistake somewhere, yet I cannot figure out where.. so here I am :)

here is my example X86Program:

(define i1 (Instr 'movq (list (Imm 0) (Reg 'rax))))
(define i2 (Instr 'subq (list (Imm 10) (Reg 'rax))))
(define instrs (list i1 i2))
(define block (Block '() instrs))
(define p (X86Program '() `((start . ,block))))
(println (interp-x86-0 p))

which essentially is

movq 0 %rax
subq 10 %rax

However, both interp-pseudo-x86-0 and interp-x86-0 gives me 10 instead of -10. If I swap the arguments to subq then I do get -10 back.

From reading both the x86 instruction quick reference from the book:

subq A, B map to B - A -> B

And in code:

(subq 2 ,(lambda (s d) (- d s)))

it seems they are consistent with my understanding of the ordering of arguments. What am I doing wrong?