/
rot13.j
149 lines (122 loc) · 1.65 KB
/
rot13.j
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
.method fgetc
.args 1
bipush -69
invokevirtual fgetc
ireturn
.method fputc
.args 2
nop
bipush -69
iload 1
invokevirtual fputc
ireturn
.method iseof // return 0 if character is EOF
.args 2
iload 1
bipush -1
if_icmpeq yes // eof
iload 1
bipush 4
if_icmpeq yes // ^D
bipush 1
ireturn
yes:
bipush 0
ireturn
.method rot13int
.args 2
iload 1
bipush 13
isub
iflt lower
iload 1
bipush 13
isub
ireturn
lower:
iload 1
bipush 13
iadd
ireturn
.method rot13
.args 2
// check c < 'A'
iload 1
dup
bipush 65 // 'A'
isub
iflt post_uppercase
// check 'Z' < c
dup
bipush 26+65 // letters in alphabet
swap
isub
iflt post_uppercase
bipush 42 // objref
swap
bipush 65
isub
invokevirtual rot13int
bipush 65
iadd
ireturn
post_uppercase:
// check c < 'a'
iload 1
dup
bipush 97 // 'a'
isub
iflt post_lowercase
// check 'z' < c
dup
bipush 26+97 // letters in alphabet
swap
isub
iflt post_lowercase
bipush 42 // objref
swap
bipush 97
isub
invokevirtual rot13int
bipush 97
iadd
ireturn
post_lowercase:
ireturn
.method main
.args 1
.locals 3
.define char = 1
.define offset = 2
.define charcount = 3
bipush 0
istore charcount
while:
bipush 42 // fgetc objref
invokevirtual fgetc
istore char
bipush 42 // objref
iload char
invokevirtual iseof
ifeq endwhile
bipush 42 // objref
iload char
invokevirtual rot13
istore char
post_uppercase:
bipush 42 // objref
iload char
invokevirtual fputc
pop // disregard return value from fputc
iload charcount
bipush 1
iadd
istore charcount
goto while
endwhile:
bipush 42 // objref
bipush 10
invokevirtual fputc
iload charcount
ireturn
// vim:syn=bytecode: