-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab6.asm
195 lines (146 loc) · 3.82 KB
/
Lab6.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
.486 ; create 32 bit code
.model flat, stdcall ; 32 bit memory model
option casemap :none ; case sensitive
__UNICODE__ equ 1
E_RANGE equ 1
E_OPNOTSUPP equ 2
E_SYNTAX equ 3
include \masm32\include\windows.inc
include \masm32\include\masm32.inc
include \masm32\include\kernel32.inc
include \masm32\include\shell32.inc
include \masm32\include\msvcrt.inc
include \masm32\macros\macros.asm
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\shell32.lib
includelib \masm32\lib\msvcrt.lib
apply_operation PROTO :REAL8, :REAL8
print_help PROTO
print_error PROTO
; ariphmetic operations
addition PROTO :REAL8, :REAL8
subtraction PROTO :REAL8, :REAL8
multiply PROTO :REAL8, :REAL8
division PROTO :REAL8, :REAL8
.data
errno dd 0
operation dd 0
result REAL8 ?
.code
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
main proc
LOCAL args: LPWSTR
LOCAL nArgs: DWORD
LOCAL ArgList: DWORD
LOCAL x: REAL8
LOCAL y: REAL8
invoke GetCommandLineW
mov args, EAX
invoke CommandLineToArgvW, args, addr nArgs
mov ArgList, EAX
.IF ArgList == 0
print "Error: can't get command line arguments."
jmp close
.ENDIF
cmp nArgs, 4
jne command_line_error
mov ESI, ArgList
add ESI, 4
invoke crt_swscanf, dword ptr [ESI], cfm$("%8lf"), ADDR x
cmp EAX, 1
jne command_line_error
add ESI, 4
invoke crt_swscanf, dword ptr [ESI], cfm$("%c"), ADDR operation
cmp EAX, 1
jne command_line_error
add ESI, 4
invoke crt_swscanf, dword ptr [ESI], cfm$("%8lf"), ADDR y
cmp EAX, 1
jne command_line_error
invoke apply_operation, x, y
.IF errno != 0
invoke print_error
.ELSEIF
printf("%lf %c %lf = %lf", x, operation, y, result)
.ENDIF
jmp close
command_line_error:
mov errno, E_SYNTAX
invoke print_error
close:
; free memory
invoke LocalFree, ArgList
invoke ExitProcess,0
ret
main endp
print_help proc
print "AVS Labs, 2013"
print chr$(13,10,13,10)
print "SYNTAX: avs-labs [first number] [operation] [second number]"
print chr$(13,10)
print " first number : first number"
print chr$(13,10)
print " operation : operation that be applied to expression"
print chr$(13,10)
print " second number: another number."
print chr$(13,10,13,10)
print " Output is to STDOUT. It can redirected to a file."
print chr$(13,10)
ret
print_help endp
print_error proc
.IF errno == E_RANGE
print "Error: out of range."
.ELSEIF errno == E_OPNOTSUPP
print "Error: operation not supported."
.ELSEIF errno == E_SYNTAX
invoke print_help
.ENDIF
ret
print_error endp
apply_operation proc x:REAL8, y:REAL8
.IF operation == "+"
invoke addition, x, y
.ELSEIF operation == "-"
invoke subtraction, x, y
.ELSEIF operation == "*"
invoke multiply, x, y
.ELSEIF operation == "/"
invoke division, x, y
.ELSEIF
mov errno, E_OPNOTSUPP
.ENDIF
ret
apply_operation endp
addition proc x:REAL8,y:REAL8
fld x
fld y
fadd
fstp result
ret
addition endp
subtraction proc x:REAL8,y:REAL8
fld x
fld y
fsub
fstp result
ret
subtraction endp
multiply proc x:REAL8,y:REAL8
fld x
fld y
fmul
fstp result
ret
multiply endp
division proc x:REAL8,y:REAL8
fld x
fld y
fdiv
fstp result
ret
division endp
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start