-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.asm
More file actions
297 lines (297 loc) · 8.23 KB
/
stack.asm
File metadata and controls
297 lines (297 loc) · 8.23 KB
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
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; Stack Library Implementation in Assembly Language with C Interface
; Copyright (C) 2025 J. McIntosh
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License along
; with this program; if not, write to the Free Software Foundation, Inc.,
; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
%ifndef STACK_ASM
%define STACK_ASM 1
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
extern bzero
extern calloc
extern free
extern memmove64
;
ALIGN_SIZE_8 EQU 8
ALIGN_WITH_8 EQU (ALIGN_SIZE_8 - 1)
ALIGN_MASK_8 EQU ~(ALIGN_WITH_8)
;
ALIGN_SIZE_16 EQU 16
ALIGN_WITH_16 EQU (ALIGN_SIZE_16 - 1)
ALIGN_MASK_16 EQU ~(ALIGN_WITH_16)
;
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
%macro ALIGN_STACK_AND_CALL 2-4
mov %1, rsp ; backup stack pointer (rsp)
and rsp, QWORD ALIGN_MASK_16 ; align stack pointer (rsp) to
; 16-byte boundary
call %2 %3 %4 ; call C function
mov rsp, %1 ; restore stack pointer (rsp)
%endmacro
;
; Example: Call LIBC function
; ALIGN_STACK_AND_CALL r15, calloc, wrt, ..plt
;
; Example: Call C callback function with address in register (rcx)
; ALIGH_STACK_AND_CALL r12, rcx
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
%include "stack.inc"
;
section .text
;
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; C definition:
;
; int stack_empty (t_stack *stack);
;
; param:
;
; rdi = stack
;
; return:
;
; rax = 0 (false) | 1 (true)
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
global stack_empty:function
stack_empty:
; if (stack->head == stack->buffer) return 1
mov eax, 1
mov rcx, QWORD [rdi + stack.head]
cmp rcx, QWORD [rdi + stack.buffer]
je .return
; return 0
xor eax, eax
.return:
ret
;
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; C definition:
;
; int stack_full (t_stack *stack);
;
; param:
;
; rdi = stack
;
; return:
;
; rax = 0 (FALSE) | 1 (TRUE)
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
global stack_full:function
stack_full:
; if (stack->head == stack->end) return 1
mov eax, 1
mov rcx, QWORD [rdi + stack.head]
cmp rcx, QWORD [rdi + stack.end]
je .return
; return 0
xor eax, eax
.return:
ret
;
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; C definition:
;
; int stack_init (t_stack *stack, size_t count, size_t const size);
;
; param:
;
; rdi = stack
; rsi = count
; rdx = size
;
; return:
;
; rax = 0 (success) | -1 (failure)
;
; stack:
;
; QWORD [rbp - 8] = rdi (stack)
; QWORD [rbp - 16] = buf_size
; QWORD [rbp - 24] = rbx (callee saved)
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
global stack_init:function
stack_init:
; prologue
push rbp
mov rbp, rsp
sub rsp, 32
mov QWORD [rbp - 8], rdi
mov QWORD [rbp - 24], rbx
; stack->o_size = size
mov QWORD [rdi + stack.o_size], rdx
; stack->s_size = (size + ALIGN_SIZE_8 - 1) & ALIGN_MASK_8
mov rax, rdx
add rax, QWORD ALIGN_SIZE_8
dec rax
and rax, QWORD ALIGN_MASK_8
test rax, rax ; test for 0 and adjust up to 8
jnz .not_zero
mov rax, QWORD ALIGN_SIZE_8
.not_zero:
mov QWORD [rdi + stack.s_size], rax
; size_t buf_size = stack->s_size * count
mul rsi
mov QWORD [rbp - 16], rax
; if ((stack->buffer = calloc(1, buf_size)) == NULL) return -1
mov rdi, 1
mov rsi, rax
ALIGN_STACK_AND_CALL rbx, calloc, wrt, ..plt
mov rdi, QWORD [rbp - 8]
mov QWORD [rdi + stack.buffer], rax
mov QWORD [rdi + stack.head], rax
test rax, rax
jnz .not_null
mov eax, -1
jmp .epilogue
.not_null:
; stack->end = stack->buffer + buf_size
mov rax, QWORD [rdi + stack.buffer]
mov rcx, QWORD [rbp - 16]
add rax, rcx
mov QWORD [rdi + stack.end], rax
; return 0
xor eax, eax
.epilogue:
mov rbx, QWORD [rbp - 24]
mov rsp, rbp
pop rbp
ret
;
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; C definition:
;
; int stack_pop (t_stack *stack, void *buffer)
;
; param:
;
; rdi = stack
; rsi = buffer
;
; return:
;
; rax = 0 (success) | -1 (failure)
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
global stack_pop:function
stack_pop:
; if (stack->head == stack->buffer) return -1
mov eax, -1
mov rcx, QWORD [rdi + stack.head]
cmp rcx, QWORD [rdi + stack.buffer]
je .return
; stack->head -= stack->s_size
mov rax, QWORD [rdi + stack.s_size]
sub rcx, rax
mov QWORD [rdi + stack.head], rcx
; (void) memmove64 (buffer, stack->head, stack->o_size)
mov rdx, QWORD [rdi + stack.o_size]
mov rdi, rsi
mov rsi, rcx
call memmove64 wrt ..plt
; return 0
xor eax, eax
.return:
ret
;
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; C definition:
;
; int stack_push (t_stack *stack, void *buffer)
;
; param:
;
; rdi = stack
; rsi = buffer
;
; return:
;
; rax = 0 (success) | -1 (failure)
;
; stack:
;
; QWORD [rbp - 8] = rdi (stack)
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
global stack_push:function
stack_push:
; prologue
push rbp
mov rbp, rsp
sub rsp, 16
; QWORD [rbp - 8] = rdi (stack)
mov QWORD [rbp - 8], rdi
; if (stack->head == stack->end) return -1
mov eax, -1
mov rcx, QWORD [rdi + stack.head]
cmp rcx, QWORD [rdi + stack.end]
je .epilogue
; (void) memmove64(stack->head, buffer, stack->o_size)
mov rdx, QWORD [rdi + stack.o_size]
mov rdi, QWORD [rdi + stack.head]
call memmove64 wrt ..plt
; stack->head += stack->s_size;
mov rdi, QWORD [rbp - 8]
mov rax, QWORD [rdi + stack.head]
add rax, QWORD [rdi + stack.s_size]
mov QWORD [rdi + stack.head], rax
; return 0
xor eax, eax
.epilogue:
mov rsp, rbp
pop rbp
ret
;
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; C definition:
;
; void stack_term (t_stack *stack)
;
; param:
;
; rdi = stack
;
; stack:
;
; QWORD [rbp - 8] = rdi (stack}
; QWORD [rbp - 16] = rbx (callee saved)
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
global stack_term:function
stack_term:
; prologue
push rbp
mov rbp, rsp
sub rsp, 32
mov QWORD [rbp - 8], rdi
mov QWORD [rbp - 16], rbx
; free item stack memory
mov rdi, QWORD [rdi + stack.buffer]
ALIGN_STACK_AND_CALL rbx, free, wrt, ..plt
; zero out stack structure
mov rdi, QWORD [rbp - 8]
mov rsi, QWORD stackSize
ALIGN_STACK_AND_CALL rbx, bzero, wrt, ..plt
; epilogue
mov rbx, QWORD [rbp - 16]
mov rsp, rbp
pop rbp
ret
%endif