-
Notifications
You must be signed in to change notification settings - Fork 15
/
monitor.asm
407 lines (348 loc) · 8.76 KB
/
monitor.asm
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
bits 16
SERIAL_PORT equ 0x3f8
TWITTER_DEVICE equ 0x100
WEATHER_DEVICE_BASE equ 0x100
AIR_QUALITY_DEVICE_BASE equ 0x200
start:
mov ax, 0x100
add ax, 0x20
mov ss, ax
mov sp, 0x1000
cld
mov ax, 0x100
mov ds, ax
mov si, welcome_msg
call print_str
jmp menu_loop
press_key:
mov si, press_any_key
call print_str
call get_users_choice
menu_loop:
call display_main_menu
call get_users_choice
cmp al, 0x31
je .cpu_details
cmp al, 0x32
je .latest_tweet
cmp al, 0x33
je .weather
cmp al, 0x34
je .air_quality
cmp al, 0x35
je .halt
mov si, illegal_choice
call print_str
jmp press_key
.cpu_details:
call print_cpu_details
jmp press_key
.latest_tweet:
call print_latest_tweet
call print_new_line
jmp press_key
.weather:
mov si, weather_str
call print_str
call print_new_line
mov si, cities_str
call print_str
call print_new_line
mov si, your_choice
call print_str
sub ax, ax
call get_users_choice
sub ax, 0x30 ; turn it from ascii to number
cmp ax, 1
jl .illegal_choice
cmp ax, 6
jg .illegal_choice
add ax, WEATHER_DEVICE_BASE ; this gives us the port number for the city
mov dx, ax
call print_weather
jmp press_key
.air_quality:
mov si, air_quality_str
call print_str
call print_new_line
mov si, cities_str
call print_str
call print_new_line
mov si, your_choice
call print_str
sub ax, ax
call get_users_choice
sub ax, 0x30 ; turn it from ascii to number
cmp ax, 1
jl .illegal_choice
cmp ax, 6
jg .illegal_choice
add ax, AIR_QUALITY_DEVICE_BASE ; this gives us the port number for the city
mov dx, ax
call print_weather
jmp press_key
.illegal_choice:
call print_new_line
mov si, illegal_choice
call print_str
jmp press_key
.halt:
hlt
data:
welcome_msg db `Welcome to Sparkler!\n`, 0
; Used by the menu system
main_menu db `\nMain menu:\n==========\n`, 0
main_menu_items db `1. CPU Info\n2. Latest CliMagic Tweet\n3. Get Weather\n4. Get Air Quality\n5. Halt VM\n`, 0
your_choice db `Your choice: \n`, 0
illegal_choice db `You entered an illegal choice!\n\n`, 0
press_any_key db `Press any key to continue...\n`, 0
; Used by our CPU ID routines
cpu_info_str db `\nHere is your CPU information:\n`, 0
cpuid_str db `Vendor ID\t: `, 0
brand_str db `Brand string\t: `, 0
cpu_type_str db `CPU type\t: `, 0
cpu_type_oem db 'Original OEM Processor', 0
cpu_type_overdrive db 'Intel Overdrive Processor', 0
cpu_type_dual db 'Dual processor', 0
cpu_type_reserved db 'Reserved', 0
cpu_family_str db `Family\t\t: `, 0
cpu_model_str db `Model\t\t: `, 0
cpu_stepping_str db `Stepping\t: `, 0
; Used by devices which fetch over the internet
fetching_wait db `\nFetching, please wait...\n`, 0
weather_str db `\nChoose the city to get weather forecast for:`, 0
air_quality_str db `\nChoose the city to get air quality report for:`, 0
; Cities
cities_str db `1. Chennai\n2. New Delhi\n3. London\n4. Chicago\n5. San Francisco\n6. New York`,0
cpuid_function dd 0x80000002
get_users_choice:
mov dx, SERIAL_PORT
in ax, dx
ret
display_main_menu:
mov si, main_menu
call print_str
mov si, main_menu_items
call print_str
mov si, your_choice
call print_str
ret
print_latest_tweet:
mov si, fetching_wait
call print_str
mov dx, TWITTER_DEVICE
.get_next_char:
in ax, dx
cmp ax, 0
je .done
call print_char
jmp .get_next_char
.done:
ret
; To be called with weather port alreay in DX
print_weather:
mov si, fetching_wait
call print_str
.get_next_char:
in ax, dx
cmp ax, 0
je .done
call print_char
jmp .get_next_char
.done:
ret
print_cpu_details:
mov si, cpu_info_str
call print_str
mov si, cpuid_str
call print_str
call print_cpuid
call print_new_line
call print_cpu_info
mov si, brand_str
call print_str
call print_cpu_brand_string
call print_new_line
ret
print_cpuid:
mov eax, 0
cpuid
push ecx
push edx
push ebx
mov cl, 3
.next_dword:
pop eax
mov bl, 4
.print_register:
call print_char
shr eax, 8
dec bl
jnz .print_register
dec cl
jnz .next_dword
ret
print_cpu_brand_string:
mov al, '"'
call print_char
.next_function:
mov eax, [cpuid_function]
cpuid
push edx
push ecx
push ebx
push eax
mov cl, 4
.next_dword:
pop eax
mov bl, 4
.print_register:
call print_char
shr eax, 8
dec bl
jnz .print_register
dec cl
jnz .next_dword
inc dword[cpuid_function]
cmp dword[cpuid_function], 0x80000004
jle .next_function
mov al, '"'
call print_char
ret
print_cpu_info:
mov eax, 1
cpuid
mov si, cpu_type_str
call print_str
mov ecx, eax ; save a copy
shr eax, 12
and eax, 0x0005
cmp al, 0
je .type_oem
cmp al, 1
je .type_overdrive
cmp al, 2
je .type_dual
cmp al, 3
je .type_reserved
.type_oem:
mov si, cpu_type_oem
jmp .print_cpu_type
.type_overdrive:
mov si, cpu_type_oem
jmp .print_cpu_type
.type_dual:
mov si, cpu_type_dual
jmp .print_cpu_type
.type_reserved:
mov si, cpu_type_reserved
jmp .print_cpu_type
.print_cpu_type:
call print_str
call print_new_line
; Family
mov si, cpu_family_str
call print_str
mov eax, ecx
shr eax, 8
and ax, 0x000f
cmp ax, 15 ; if Family == 15, Family is derived as the
je .calculate_family ; sum of Family + Extended family bits
jmp .family_done ; else
.calculate_family:
mov ebx, ecx
shr ebx, 20
and bx, 0x00ff
add ax, bx
.family_done:
call print_word_hex
; Model
mov si, cpu_model_str
call print_str
cmp al, 6 ; If family is 6 or 15, the model number
je .calculate_model ; is derived from the extended model ID bits
cmp al, 15
je .calculate_model
mov eax, ecx ; else
shr eax, 4
and ax, 0x000f
jmp .model_done
.calculate_model:
mov eax, ecx
mov ebx, ecx
shr eax, 16
and ax, 0x000f
shl eax, 4
shr ebx, 4
and bx, 0x000f
add eax, ebx
.model_done:
call print_word_hex
; Stepping
mov si, cpu_stepping_str
call print_str
mov eax, ecx
and ax, 0x000f
call print_word_hex
ret
print_new_line:
push dx
push ax
mov dx, SERIAL_PORT
mov al, `\n`
out dx, al
pop ax
pop dx
ret
print_char:
push dx
mov dx, SERIAL_PORT
out dx, al
pop dx
ret
print_str:
push dx
push ax
mov dx, SERIAL_PORT
.print_next_char:
lodsb ; load byte pointed to by SI into AL and SI++
cmp al, 0
je .printstr_done
out dx, al
jmp .print_next_char
.printstr_done:
pop ax
pop dx
ret
; Print the 16-bit value in AX as HEX
print_word_hex:
xchg al, ah ; Print the high byte first
call print_byte_hex
xchg al, ah ; Print the low byte second
call print_byte_hex
call print_new_line
ret
; Print lower 8 bits of AL as HEX
print_byte_hex:
push dx
push cx
push ax
lea bx, [.table] ; Get translation table address
; Translate each nibble to its ASCII equivalent
mov ah, al ; Make copy of byte to print
and al, 0x0f ; Isolate lower nibble in AL
mov cl, 4
shr ah, cl ; Isolate the upper nibble in AH
xlat ; Translate lower nibble to ASCII
xchg ah, al
xlat ; Translate upper nibble to ASCII
mov dx, SERIAL_PORT
mov ch, ah ; Make copy of lower nibble
out dx, al
mov al, ch
out dx, al
pop ax
pop cx
pop dx
ret
.table: db "0123456789ABCDEF", 0