-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.asm
More file actions
154 lines (129 loc) · 2.29 KB
/
Copy pathInput.asm
File metadata and controls
154 lines (129 loc) · 2.29 KB
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
# Input.asm
# This file implements the Input functions.
.globl Input
.data
inputFlag: .word 4294901760
inputAddr: .word 4294901764
bufferSize: .word 16384
.text
# Constructor for the Input class
#
# Args:
# None
#
# Return:
# $v0 The address of the new instance
#
# Usage:
# jal Input
# move $t1, $v0
#
####################################
# Object Structure
# 0-4 The tick function
# 4-8 The pop function
# 8-12 The reset function
# 12-16 The address of the buffer
# 16-20 The current pointer into the buffer
# 20-21 The hasChars flag
####################################
Input:
move $t0, $a0 # start of new Input()
move $t1, $a1
move $t2, $a2
# alloc Buffer
li $v0, 9
lw $a0, bufferSize
syscall
move $t3, $v0
# alloc Input
li $v0, 9 # sbrk code
li $a0, 24 # number of bytes needed
syscall
# load methods
la $t0, tick
la $t1, pop
la $t2, reset
li $t4, 0
# Setup object
sw $t0, 0($v0)
sw $t1, 4($v0)
sw $t2, 8($v0)
sw $t3, 12($v0)
sw $t3, 16($v0)
sb $t4, 20($v0)
jr $ra
# Adds input to buffer if there is any.
#
# Args:
# $a0 The address of the Input class
#
# Return:
# none
#
# Usage:
# code to put the address of Input into $a0
# lw $t9, 0($a0)
# jalr $t9
#
tick:
# $t0 input flag
lw $t0, inputFlag # start of Input.tick()
lw $t0, ($t0)
beqz $t0, tickReturn
# $t0 input value
# $t1 cur pointer
lw $t0, inputAddr
lw $t0, ($t0)
lw $t1, 16($a0)
sw $t0, ($t1)
add $t1, $t1, 4
sw $t1, 16($a0)
li $t2, 1
sb $t2, 20($a0)
tickReturn:
jr $ra
# Pops the last buffered char and returns it
#
# Args:
# $a0 The address of the Input class
#
# Return:
# $v0 The value poped
#
# Usage:
# code to put the address of Input into $a0
# lw $t9, 4($a0)
# jalr $t9
#
pop:
# $t0 cur pointer
# $t1 base pointer
lw $t0, 16($a0) # start of Input.pop()
lw $v0, -4($t0)
sub $t0, $t0, 4
sw $t0, 16($a0)
lw $t1, 12($a0)
sub $t2, $t0, $t1
bnez $t2, popReturn
li $t3, 0
sb $t3, 20($a0)
popReturn:
jr $ra
# Resets the buffer
#
# Args:
# $a0 The address of the Input class
#
# Return:
# none
#
# Usage:
# code to put the address of Input into $a0
# lw $t9, 8($a0)
# jalr $t9
#
reset:
lw $t0, 12($a0)
sw $t0, 16($a0)
jr $ra