-
Notifications
You must be signed in to change notification settings - Fork 0
/
clasm.lisp
60 lines (48 loc) · 1.29 KB
/
clasm.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
(defun asm (&rest rest)
(mapcar (lambda (str) (progn (princ str)
(terpri)))
rest)
t)
(defun join-strings-with-comma (s1 s2)
(concatenate 'string s1 "," s2))
(defun operands-from-list (l)
(reduce #'join-strings-with-comma
(mapcar #'princ-to-string l)))
(defmacro definstruction (name)
`(defun ,name (&rest rest)
(let ((operands (operands-from-list rest)))
(concatenate 'string
" "
,(string-downcase (symbol-name name))
" "
operands))))
(defmacro defregister (name)
`(defvar ,name (concatenate 'string
"$"
,(string-downcase (symbol-name name)))))
(defun label (name)
(concatenate 'string name ":"))
; Sample x86 instructions and registers
(definstruction add)
(definstruction xor)
(definstruction cmp)
(definstruction jge)
(definstruction inc)
(definstruction jmp)
(defregister eax)
(defregister ebx)
(defregister ecx)
(defregister edx)
; Sample program
(defun sample-program ()
(asm
(label "start")
(xor eax eax)
(xor ebx ebx)
(add ebx 100)
(label "loop")
(cmp eax ebx)
(jge "end")
(inc eax)
(jmp "loop")
(label "end")))