-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStep9.hs
54 lines (45 loc) · 1.12 KB
/
Step9.hs
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
{-# LANGUAGE NoMonomorphismRestriction, FlexibleContexts #-}
-- switch statements, multi-byte numbers
module Step9 where
-- import Address
import Instr -- move, moveb, inc, ..., debug
import Allocator (compile, alloc, nalloc)
import Pair
import Util
clearPair pair = do clear pair; clear (second pair)
-- increment a multi-byte number
incrPairs [] = return ()
incrPairs (x:xs) = do
incr x
ifThenElse x
pass
(incrPairs xs)
-- decrement a multi-byte number
decrPairs [] = return ()
decrPairs (x:xs) = do
ifThenElse x
pass
(decrPairs xs)
decr x
-- perform body if all pairs are zero
allZero [] body = body
allZero (x:xs) body = do
ifThenElse x
pass
(allZero xs body)
-- demo of a two-byte number: count down from 0x123 to 1
test_twobytes = do
allocPair $ \zero -> do
allocPair $ \x0 -> do
allocPair $ \x1 -> do
alloc $ \notDone -> do
withZero zero $ do
assign x1 0x01 -- hi byte
assign x0 0x23 -- low byte
-- [x0, x1] represents 0x0123
assign notDone 1
while notDone $ do
debug x1 "x1"
debug x0 "x0"
decrPairs [x0, x1]
allZero [x0, x1] (clear notDone)