-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharr_2.asm
161 lines (133 loc) · 2.55 KB
/
arr_2.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
%define ARRAY_SIZE 5
%define ELEMENT_SIZE 4
%define EOF -1
segment .data
inputPrompt: db "Please Enter int value (ctrl-d to stop): ", 0
intFormat: db "%d", 0
intOutput: db "Current Arr: %d, Min Val: %d, Arithm Val: %d", 10, 0
numElements: db "number of elements: %d", 10, 0
sum: db "Sum: %d", 10, 0
min: db "Min: %d", 10, 0
max: db "Max: %d", 10, 0
arraySize: db 5d
segment .bss
myArray: resq 1
intInput: resd 1
segment .text
global asm_main
extern printf, scanf, calloc
asm_main:
enter 0, 0
xor r12, r12
xor r13, r13
xor r9, r9
mov rdi, myArray
mov rcx, ARRAY_SIZE
inputLoop:
inc rcx
push rcx
push rdi
;;prompt and get inputs
mov rdi, inputPrompt
call printf
mov rdi, intFormat
mov rsi, intInput
call scanf
;;check if end of reading
cmp eax, EOF
je inputDone
inc r12
;;check if array bounds exceeded
cmp r12, ARRAY_SIZE
jle skipResize
; call resizeArray
;;resize
mov rdi, 10 ;new array size
mov rsi, ELEMENT_SIZE
call calloc
;;move elements into new array
mov rdi, rax ;dest
mov rsi, myArray ;source
mov rcx, ARRAY_SIZE ;old array size
mov [myArray], rax ;replace old ptr with new
cld
rep movsd
skipResize:
;;sums values
add r13, [intInput]
;;add elements to array and loop
xor rax, rax
mov eax, [intInput]
pop rdi
;;relocate rdi to new location
;; push rcx
; mov rcx, r12 ;loop number of elements times
; mov rdi, myArray
;relocateLoop:
; add rdi, 4 ;move up to current element
; loop relocateLoop
; pop rcx
stosd
mov r9, [rdi - 4]
pop rcx
jmp inputLoop
inputDone:
;;set up array access
mov rbx, myArray
mov rcx, r12
;;set up max and min
xor r14, r14
xor r15, r15
movsx r14, DWORD [rbx]
movsx r15, DWORD [rbx]
minMaxLoop:
push rcx
push rsi
;;printing to test output
xor r9,r9
movsx r9, DWORD [rbx]
mov rdi, intFormat
mov rsi, r9
call printf
;;comparisons - min
movsx r9, DWORD [rbx]
sub r9, r14 ;sets flags
jns returnMin
movsx r14, DWORD [rbx]
returnMin: ;;-max
movsx r9, DWORD [rbx]
sub r9, r15 ;sets flags
js returnMax
movsx r15, DWORD [rbx]
returnMax:
pop rsi
pop rcx
add rbx, 4
loop minMaxLoop
jmp printStuff
;;not coding in this function yet
resizeArray:
enter 0,0
mov rax, ARRAY_SIZE
ret
printStuff:
;;print number of elements
mov rdi, numElements
mov rsi, r12
call printf
;;print sum
mov rdi, sum
mov rsi, r13
call printf
;;print min
mov rdi, min
mov rsi, r14
call printf
;;print max
mov rdi, max
mov rsi, r15
call printf
;;exit
mov rax, 0
leave
ret