在上一篇文章中,初步搭建了一个输入Common Lisp代码,输出汇编代码的编译器的骨架,实现了二元整数的加法运算。在这个基础上,要想实现减法、乘法,以及除法就是手到擒来的事情了。只需依葫芦画瓢,补充更多的分支状况便可。html
我本身模仿着x64的调用约定,规定四则运算的结果始终放在EAX
这个寄存器中。在稍后给出的代码中,对于减法和除法运算,都是把运算符的左操做数放到EAX
寄存器中,再从EAX
中减去或者除掉右操做数。git
在摸索除法的汇编代码怎么生成时,遇到了个费解的问题,最后才知道,原来须要把EAX
寄存器的符号扩展到高位的EDX
寄存器中去。对于as
这个汇编器来讲,须要用到CLTD
指令。github
最后,jjcc2
和stringify
两个函数被修改成以下的样子segmentfault
(defun jjcc2 (expr) "支持两个数的四则运算的编译器" (cond ((eq (first expr) '+) `((movl ,(second expr) %eax) (movl ,(third expr) %ebx) (addl %ebx %eax))) ((eq (first expr) '-) `((movl ,(second expr) %eax) (movl ,(third expr) %ebx) (subl %ebx %eax))) ((eq (first expr) '*) ;; 将两个数字相乘的结果放到第二个操做数所在的寄存器中 ;; 由于约定了用EAX寄存器做为存放最终结果给continuation用的寄存器,因此第二个操做数应当为EAX `((movl ,(second expr) %eax) (movl ,(third expr) %ebx) (imull %ebx %eax))) ((eq (first expr) '/) `((movl ,(second expr) %eax) (cltd) (movl ,(third expr) %ebx) (idivl %ebx))))) (defun stringify (asm) "根据jjcc2产生的S表达式生成汇编代码字符串" (format t " .section __TEXT,__text,regular,pure_instructions~%") (format t " .globl _main~%") (format t "_main:~%") (dolist (ins asm) (cond ((= (length ins) 3) (format t " ~A ~A, ~A~%" (first ins) (if (numberp (second ins)) (format nil "$~A" (second ins)) (second ins)) (if (numberp (third ins)) (format nil "$~A" (third ins)) (third ins)))) ((= (length ins) 2) (format t " ~A ~A~%" (first ins) (if (numberp (second ins)) (format nil "$~A" (second ins)) (second ins)))) ((= (length ins) 1) (format t " ~A~%" (first ins))))) (format t " movl %eax, %edi~%") (format t " movl $0x2000001, %eax~%") (format t " syscall~%"))
全文完。oracle
阅读原文函数