-
Notifications
You must be signed in to change notification settings - Fork 3
/
Array_Extend.asm
111 lines (92 loc) · 2.67 KB
/
Array_Extend.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
;==============================================================================
;
; UASM64 Library
;
; https://github.com/mrfearless/UASM64-Library
;
;==============================================================================
.686
.MMX
.XMM
.x64
option casemap : none
IF @Platform EQ 1
option win64 : 11
ENDIF
option frame : auto
;GlobalReAlloc PROTO pMem:QWORD, dwBytes:QWORD, uFlags:DWORD
;
;IFNDEF GMEM_MOVEABLE
;GMEM_MOVEABLE EQU 0002h
;ENDIF
;
;includelib kernel32.lib
include UASM64.inc
EXTERNDEF d_e_f_a_u_l_t__n_u_l_l_$ :QWORD
;.DATA
;d_e_f_a_u_l_t__n_u_l_l_$ DQ 0 ; default null string as placeholder
; DQ 0,0,0
.CODE
UASM64_ALIGN
;------------------------------------------------------------------------------
; Array_Extend
;
; Extend (grow) an array and add new empty items.
;
; Parameters:
;
; * pArray - pointer to the array to extend.
;
; * nItemCount - the new count of items for the array to grow to.
;
; Returns:
;
; A pointer to the newly resized array, which is used with the other array
; functions, or 0 if an error occurred.
;
; Notes:
;
; This function as based on the MASM32 Library function: arrextnd
;
; See Also:
;
; Array_Truncate, Array_Resize
;
;------------------------------------------------------------------------------
Array_Extend PROC FRAME USES RBX RDI RSI pArray:QWORD, nItemCount:QWORD
LOCAL acnt:QWORD
LOCAL oldc:QWORD
LOCAL arr:QWORD
.IF pArray == 0
mov rax, 0
ret
.ENDIF
mov rax, pArray ; get the array member count
mov rax, [rax]
mov oldc, rax ; write it to old count variable
mov rdi, nItemCount
cmp rdi, oldc
jg @F
mov rax, 0 ;-1 ; *** ERROR *** indx not greater than oldc
jmp quit
@@:
mov rax, rdi
lea rax, [8+rax*8] ; set new pointer array size
Invoke Memory_Realloc, pArray, rax
;Invoke GlobalReAlloc, pArray, rax, GMEM_MOVEABLE
mov arr, rax ;, rv(GlobalReAlloc,arr,ecx,GMEM_MOVEABLE)
mov rsi, arr
mov [rsi], rdi ; store new array count in 1st array member
mov rbx, oldc ; copy the old count to EBX
add rbx, 1 ; add 1 for 1 based index
@@:
lea rax, d_e_f_a_u_l_t__n_u_l_l_$
mov [rsi+rbx*8], rax ; OFFSET d_e_f_a_u_l_t__n_u_l_l_$
add rbx, 1 ; increment index
cmp rbx, [rsi] ; test if it matches the array count yet
jle @B
mov rax, arr ; return the reallocated memory address
quit:
ret
Array_Extend ENDP
END