-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreadwriteyield.asm
More file actions
108 lines (90 loc) · 3.13 KB
/
readwriteyield.asm
File metadata and controls
108 lines (90 loc) · 3.13 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
####################################################
#This program prints 20 keystrokes to the console
#The keyboard device should have id 0. The console
#device should have id 1.
# Add each iteration this program also yields
# the CPU to other processes.
###################################################
#Initialize the variables
SET r1 0 #counter
SET r2 1 #increment amount
SET r3 20 #limit
#Main Loop
:loop
ADD r1 r2 r1
#Reserve the keyboard device
SET r0 0 #device #0 (keyboard)
PUSH r0 #push argument on stack
SET r4 3 #OPEN sys call id
PUSH r4 #push sys call id on stack
TRAP #open the device
#Check for failure
POP r4 #get return code from the system call
SET r0 0 #Succes code
BNE r0 r4 exit #exit program on error
#Reserve the console device
SET r0 1 #device #1 (console output)
PUSH r0 #push argument on stack
SET r4 3 #OPEN sys call id
PUSH r4 #push sys call id on stack
TRAP #open the device
#Check for failure
POP r4 #get return code from the system call
SET r0 0 #Succes code
BNE r0 r4 exit #exit program on error
#Read a keystroke from the keyboard
SET r0 0 #device #0 (keyboard)
PUSH r0 #push device number
PUSH r0 #push address (arg not used by this device so any val will do)
SET r0 5 #READ system call
PUSH r0 #push system call id
TRAP #system call to read the value
#Check for failure
POP r4 #get return code from the system call
SET r0 0 #Succes code
BNE r0 r4 exit #exit program on error
#save the keystroke
POP r4 #save the value in r4
#Write the value to the console
SET r0 1 #device #1 (console output)
PUSH r0 #push device number
PUSH r0 #push address (arg not used by this device so any val will do)
PUSH r4 #push value to send to device
SET r0 6 #WRITE system call
PUSH r0 #push system call id
TRAP #system call to write the value
#Check for failure
POP r4 #get return code from the system call
SET r0 0 #Succes code
BNE r0 r4 exit #exit program on error
#close the keyboard device
SET r4 0 #keyboard device id
PUSH r4 #push device number 0 (keyboard)
SET r4 4 #CLOSE sys call id
PUSH r4 #push the sys call id onto the stack
TRAP #close the device
#Check for failure
POP r4 #get return code from the system call
SET r0 0 #Success code
BNE r0 r4 exit #exit program on error
#close the console device
SET r4 1 #keyboard device id
PUSH r4 #push device number 1 (console output)
SET r4 4 #CLOSE sys call id
PUSH r4 #push the sys call id onto the stack
TRAP #close the device
#Check for failure
POP r4 #get return code from the system call
SET r0 0 #Success code
BNE r0 r4 exit #exit program on error
#yield the CPU to other processes
SET r4 8 #YIELD sys call id
PUSH r4 #push the sys call id onto the stack
TRAP #make the system call
#loop test
BNE r1 r3 loop
#exit syscall
:exit
SET r4 0 #EXIT system call id
PUSH r4 #push sys call id on stack
TRAP #exit the program