-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasm.hs
More file actions
executable file
·68 lines (57 loc) · 1.67 KB
/
Copy pathasm.hs
File metadata and controls
executable file
·68 lines (57 loc) · 1.67 KB
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
61
62
63
64
65
66
module Asm (compile) where
import Model
compile :: [BFOut] -> String
compile bf = let items = map compileItem bf
in prefix ++ (unlines items) ++ suffix
compileItem (Add n) = " movl $buffer, %eax\n\
\ addl pointer, %eax\n\
\ movb $" ++ (show n) ++ ", %bl\n\
\ add %bl, (%eax)\n"
compileItem SetZero = " movl $buffer, %eax\n\
\ addl pointer, %eax\n\
\ movb $0, (%eax)\n"
compileItem (Shift n) | n < 0 = " subl $" ++ (show (abs n)) ++ ", pointer\n"
| otherwise = " addl $" ++ (show n) ++ ", pointer\n"
compileItem ReadOut = " movl $3, %eax\n\
\ movl $0, %ebx\n\
\ movl $buffer, %ecx\n\
\ addl pointer, %ecx\n\
\ movl $1, %edx\n\
\ int $0x80\n"
compileItem WriteOut = " movl $4, %eax\n\
\ movl $1, %ebx\n\
\ movl $buffer, %ecx\n\
\ addl pointer, %ecx\n\
\ movl $1, %edx\n\
\ int $0x80\n"
compileItem (LoopOut bf n) = let metka = "begin" ++ (show n)
metka2 = "end" ++ (show n)
items = unlines $ map compileItem bf
code = " movl $buffer, %eax\n\
\ addl pointer, %eax\n\
\ cmpb $0, (%eax)\n\
\ je " ++ metka2 ++ "\n\n"
in metka ++ ":\n" ++ code ++ items ++ " jmp " ++ metka ++ "\n" ++ metka2 ++ ":\n"
prefix = ".data\n\
\ new_row:\n\
\ .string \"\\n\"\n\
\ pointer:\n\
\ .long 0\n\
\ .bss\n\
\ buffer:\n\
\ .space 256\n\
\ .text\n\
\ .globl main\n\
\ .type main, @function\n\
\ main:\n"
suffix = "/* new row */\n\
\ movl $4, %eax\n\
\ movl $1, %ebx\n\
\ movl $new_row, %ecx\n\
\ movl $1, %edx\n\
\ int $0x80\n\
\ /* exit */\n\
\ movl $1, %eax\n\
\ movl $0, %ebx\n\
\ int $0x80\n\
\ .size main, . - main\n"