-
Notifications
You must be signed in to change notification settings - Fork 0
/
count.jas
154 lines (145 loc) · 3.35 KB
/
count.jas
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
////
// Assembly Program for the Mic1 Microarchitecture Simulator
//
// File
// count.jas
//
// Time-stamp: <Wednesday 06 April 2005, 19:03:35 - by jermiin>
//
// Author
// Jonas Jermiin Ravn Moll <jonas@jermiin.dk>
//
// Description
// Counts integer values from 0 to a variable value (end)
//
// Usage
// To compile: java ijvmasm count.jas
// This will create count.ijvm
//
// To run in the simulator
// java mic1sim mic1ijvm.mic1 count.ijvm
// Click the Run button
////
.constant
end 9
i 1
OBJREF 0x40
.end-constant
.main
.var
a
.end-var
start: BIPUSH 0x0
DUP
ISTORE a
LDC_W OBJREF
SWAP
INVOKEVIRTUAL print
next: ILOAD a
LDC_W i
IADD
DUP
ISTORE a
LDC_W end
IF_ICMPEQ done // exit if we've reached 'end'
LDC_W OBJREF
ILOAD a
INVOKEVIRTUAL print
GOTO next
done: POP
HALT
.end-main
//////////////////////
// The method below converts a number into a string of
// characters and prints them. All of the characters
// are pushed onto the stack, least significant
// digit first, then popped off and printed.
//////////////////////
.method print( total )
.var
place
index
.end-var
print: BIPUSH 0x9 // there are 8 nibbles in each integer--setting
// this as nine pushes 10 characters onto the
// stack, thus a total of ten printed digits,
// but setting this less does not remove the
// two leading zeros, just removes significant
// digits
ISTORE index
BIPUSH 0x1 // comparison bit
ISTORE place
print1: BIPUSH 0x0
ILOAD index // index = index - 1
BIPUSH 0x1
ISUB
DUP
IFEQ pall // if index = 0 goto pall
ISTORE index
ILOAD total // else
ILOAD place //
IAND // if 1st bit of current nibble is zero (total & place)
IFEQ print2 // goto print2
BIPUSH 0x1 // else set first bit of character
IADD
print2: ILOAD place // place = place << 1
DUP
IADD
ISTORE place
ILOAD total
ILOAD place
IAND // if 2nd bit of current nibble is zero (total & place)
IFEQ print3 // goto print3
BIPUSH 0x2 // else set second bit of character
IADD
print3: ILOAD place // place = place << 1
DUP
IADD
ISTORE place
ILOAD total
ILOAD place
IAND // if 3rd bit of current nibble is zero (total & place)
IFEQ print4 // goto print4
BIPUSH 0x4 // else set second bit of character
IADD
print4: ILOAD place // place = place << 1
DUP
IADD
ISTORE place
ILOAD total
ILOAD place
IAND // if 4th bit of current nibble is zero (total & place)
IFEQ print5 // goto print5
BIPUSH 0x8 // else set second bit of character
IADD
print5: ILOAD place // place = place << 1
DUP
IADD
ISTORE place
GOTO print1
pall: POP // Pop off leading 0's
POP
BIPUSH 0x9
ISTORE index
pall1: ILOAD index // index = index - 1
BIPUSH 0x1
ISUB
DUP
IFEQ return // if index = 0 return
ISTORE index
DUP
BIPUSH 0xa // else if character < 0xa goto pall1
ISUB
IFLT pall2
BIPUSH 0x37 // else convert character to "A"-"F"
IADD
OUT // print character
GOTO pall1 // goto pall (prepare & print next character)
pall2: BIPUSH 0x30 // convert character to "0"-"9"
IADD
OUT // print character
GOTO pall1 // goto pall1 (prepare & print next character)
return: BIPUSH 0xa // print cr
OUT
IRETURN // no return value
.end-method