-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.asm
104 lines (77 loc) · 1.76 KB
/
calc.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
%include "io.asm"
%include "memory.asm"
%include "rpn.asm"
%include "strcmp.asm"
%include "tokenize.asm"
%include "winapi.asm"
global main
section .data
STD_INPUT_HANDLE equ -10
STD_OUTPUT_HANDLE equ -11
INPUT_BUFFER_SIZE equ 1024
cursor db "calc>", 0
cursor.len equ $-cursor
test_cmd db "-- TESTING --", 10, 0
test_cmd.len equ $-test_cmd
; Commands
EXIT_COMMAND db "exit", 0
TEST_COMMAND db "?test?", 0
section .bss
empty resd 1
CONSOLE_HANDLE resq 1
input_buffer resb INPUT_BUFFER_SIZE
section .text
main:
push rbp
mov rbp, rsp
sub rsp, 0x30
call input_loop ; main loop
leave
ret
input_loop:
call zero_input_buffer
; display the input cursor
mov rcx, cursor
mov rdx, cursor.len
call write_line
; read the input
mov rcx, input_buffer
mov rdx, INPUT_BUFFER_SIZE
call read_line
; switch (input_buffer)
; case "exit"
mov rcx, input_buffer
mov rdx, EXIT_COMMAND
call strcmp
test rax, rax
je end
; case "?test?"
mov rcx, input_buffer
mov rdx, TEST_COMMAND
call strcmp
test rax, rax
je test_command
; Parse the rest
mov rcx, input_buffer
call tokenise
mov rcx, rax
call calculate_rpn
input_loop_end:
jmp input_loop
end:
mov rcx, 0x00
call ExitProcess
ret
; Prints the test command
test_command:
mov rcx, test_cmd
mov rdx, test_cmd.len-0x01
call write_line
jmp input_loop_end
zero_input_buffer:
cld
mov rdi, input_buffer
mov rcx, INPUT_BUFFER_SIZE
xor rax, rax
rep stosb
ret