-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbin2dec.psm
109 lines (101 loc) · 2.88 KB
/
bin2dec.psm
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
;This program has been tested on real
;PicoBlaze and it worked there! While
;this program uses regbanks and flags,
;I would not take the fact that it
;works on PicoBlaze to mean that the
;emulation of flags in this simulator
;is realistic when the regbank changes.
;Namely, given what I know, it is
;possible that real PicoBlaze does not
;have separate flags for each regbank.
;The "Regbanks-Flags Test" is supposed
;to test for that (to succeed if and
;only if flags work the way I think they
;do when the regbank changes, and to
;fail if they do not).
;Converting binary numbers entered using
;the switches to decimal and displaying
;them on the 7-segment displays.
;Also, converting them to Gray code and
;displaying the Gray code using LEDs.
base_decimal ;By default, the numerical
;literals in PicoBlaze
;assembly are treated as
;base 16 (hexadecimal),
;rather than base 10
;(decimal), as in almost
;all programming languages.
;I don't like that, so I
;added this preprocessor
;directive so that the
;users of my PicoBlaze
;assembler can change that
;behaviour.
constant eight_switches_input , 0
constant eight_LEDs_output , 0
constant first_two_7s_displays , 1
constant second_two_7s_displays, 2
address 0
infinite_loop:
namereg sc , first_digit
input s0 , eight_switches_input
load sa , s0
load first_digit, 0
compare sa , 200
jump c , lessThan200
sub sa , 200
load first_digit, 2
lessThan200:
compare sa, 100
jump c , lessThan100
sub sa , 100
load first_digit, 1
lessThan100:
call divide_sa_by_10
call multiply_sa_by_16
add sa , sb ;sb contains the
;remainder of the
;division of sa by
;10.
output first_digit, first_two_7s_displays
output sa ,second_two_7s_displays
;I removed the space after `,` to make
;that line fit inside 43 columns, so that
;the user doesn't have to scroll it
;horizontally in Chrome (they didn't have
;to do that in Firefox even before that).
;Now goes the code for converting from
;binary to the Gray Code...
load s1 , s0
sr0 s1
xor s1 , s0
output s1 , eight_LEDs_output
jump infinite_loop ;You can add a
;breakpoint here
;to make the
;program stop as
;soon as it
;prints out the
;result.
divide_sa_by_10:
star s0, sa
regbank b
load s1, 0
beginning_of_loop:
compare s0, 10
jump c , end_of_loop
sub s0, 10
add s1, 1
jump beginning_of_loop
end_of_loop:
star sa, s1
star sb, s0
regbank a
return
multiply_sa_by_16:
;16=2^4
sl0 sa
sl0 sa
sl0 sa
sl0 sa
return