-
Notifications
You must be signed in to change notification settings - Fork 0
/
trap.box.in
97 lines (84 loc) · 1.83 KB
/
trap.box.in
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
#
# About:
#
# Prompt for a string and then output it, surrounded by a series of
# stars. This demonstrates trap 0x1 (read from stdin) and trap
# 0x02 (strip newline).
#
# Usage:
#
# $ go.vm run ./trap.box.in
#
# Or compile, then execute:
#
# $ go.vm compile ./trap.box.in
# $ go.vm execute ./trap.box.raw
#
# Example:
#
# If the user enters "Steve" they will see the following output:
#
# *********
# * Steve *
# *********
store #1, "Please enter your name:"
print_str #1
# Reads a string from the console - sets the result in register #0
int 0x01
# The following trap removes the newline from the string in #0
int 0x02
# Call a subroutine to output the boxed result
call box
exit
#
# This function prints out the string in #0 in a box. For example if the input
# string in #0 is "Steve" the output will be:
#
# *********
# * Steve *
# *********
#
# Registers ruined:
# #0
# #1
# #10
#
:box
# string is in #0
store #10, #0
# find the length
int 0x00
# now we want to print the line of stars to box the string
inc #0
inc #0
inc #0
inc #0
:header
store #1, "*"
print_str #1
dec #0
jmpnz header
# print "* $str *"
store #1, "\n* "
print_str #1
store #1, #10
print_str #1
store #1, " *\n"
print_str #1
# now repeat the process to print stars under the string
store #0, #10
# find the length
int 0x00
# now we want to print the line of stars to box the string
inc #0
inc #0
inc #0
inc #0
:footer
store #1, "*"
print_str #1
dec #0
jmpnz footer
store #1, "\n"
print_str #1
ret