-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW.ASM
171 lines (146 loc) · 3.95 KB
/
HW.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
format PE console
entry start
include 'win32a.inc'
;--------------------------------------------------------------------------
section '.data' data readable writable
strVecSize db 'Input size of vector: ', 0
strIncorSize db 'Incorrect size of vector = %d', 10, 0
strVecElemI db '[%d] = ', 0
strScanInt db '%d', 0
strSumValue db 'Summa = %d', 10, 0
strOut db 'Arrray: ', 10, 0
strVecElemOut db '[%d] = %d', 10, 0
vec_size dd 0
sum dd 0
i dd ?
tmp dd ?
tmp2 dd ?
tmpStack dd ?
vec rd 100
newvec rd 100
;--------------------------------------------------------------------------
section '.code' code readable executable
start:
; 1) vector input
call VectorInput
call CopyVector
push strOut
call [printf]
call PrintVector
finish:
call [getch]
push 0
call [ExitProcess]
;--------------------------------------------------------------------------
VectorInput:
push strVecSize
call [printf]
add esp, 4
push vec_size
push strScanInt
call [scanf]
add esp, 8
mov eax, [vec_size]
cmp eax, 0
jle FinishError
cmp eax, 100
jg FinishError
jmp getVector
;If vec_size <= 0 || vec_size > 100
FinishError:
push [vec_size]
push strIncorSize
call [printf]
call [getch]
push 0
call [ExitProcess]
getVector:
xor ecx, ecx ; ecx = 0
mov ebx, vec ; ebx = &vec
getVecLoop:
mov [tmp], ebx
cmp ecx, [vec_size]
jge endInputVector ; to end of loop
; input element
mov [i], ecx
push ecx
push strVecElemI
call [printf]
add esp, 8
push ebx
push strScanInt
call [scanf]
add esp, 8
mov ecx, [i]
inc ecx
mov ebx, [tmp]
add ebx, 4
jmp getVecLoop
endInputVector:
ret
;--------------------------------------------------------------------------
CopyVector:
xor ecx, ecx ; ecx = 0
mov ebx, vec
xor edx, edx
xor eax, eax
VectorCopyLoop:
cmp ecx, [vec_size]
je endCopyVector ; to end of loop
cmp dword [ebx], 0
jle CopyElement
cmp edx, 0
je ChangeFlag
jmp CopyElement
ChangeFlag:
mov edx, 1
inc ecx
add ebx, 4
jmp VectorCopyLoop
CopyElement:
mov [tmp], ecx
mov ecx, dword[ebx]
mov [newvec + eax*4], ecx
mov ecx, [tmp]
inc ecx
inc eax
add ebx, 4
jmp VectorCopyLoop
endCopyVector:
neg edx
add [vec_size], edx
ret
;----------------------------------------------------------------------------
PrintVector:
xor ecx, ecx
mov ebx, newvec
VectorLoop:
cmp ecx, [vec_size]
je endPrintVector
mov [tmp], ecx
push dword [ebx]
push ecx
push strVecElemOut
call [printf]
add esp, 12
mov ecx, [tmp]
inc ecx
add ebx, 4
jmp VectorLoop
endPrintVector:
ret
section '.idata' import data readable
library kernel, 'kernel32.dll',\
msvcrt, 'msvcrt.dll',\
user32,'USER32.DLL'
include 'api\user32.inc'
include 'api\kernel32.inc'
import kernel,\
ExitProcess, 'ExitProcess',\
HeapCreate,'HeapCreate',\
HeapAlloc,'HeapAlloc'
include 'api\kernel32.inc'
import msvcrt,\
printf, 'printf',\
scanf, 'scanf',\
getch, '_getch'