appending lines after
[sped.git] / utils.asm
1
2 %include "macros.S"
3
4 extern memmove
5 extern free
6 extern realloc
7
8 global shiftLeft
9 global shiftRight
10
11 section .text
12
13 ; shrinks array (4byte) by shifting blocks left
14 ; args: buffer, buffer_len, shift_pos (index that gets destroyed)
15 ; return:
16 ;   eax: location of new buffer
17 ; issues: 
18 shiftLeft:
19     %define _BUFFER      16
20     %define _BUFFER_LEN  12
21     %define _SHIFT_POS   8
22     %define SHIFT_LEN    4
23     %define BLOCK_OFFSET 8 ; mem location of block to be destroyed
24     %define NEW_BUFFER   12
25
26     push ebp
27     mov ebp, esp
28
29     sub esp, 12
30
31     ; set vars
32     mov eax, DWORD [ebp+_BUFFER_LEN]
33     sub eax, [ebp+_SHIFT_POS]
34     sub eax, 1
35     mov [ebp-SHIFT_LEN], eax
36
37     str_offset [ebp+_BUFFER], [ebp+_SHIFT_POS]
38     mov [ebp-BLOCK_OFFSET], eax
39     
40     ; free string to be destoryed first
41     mov eax, DWORD [ebp-BLOCK_OFFSET]
42     mov eax, [eax]
43     push eax
44     call free
45
46     ; move the memory
47     mov eax, DWORD [ebp-SHIFT_LEN]
48     mov ecx, 4
49     mul ecx
50     push eax
51     mov eax, DWORD [ebp-BLOCK_OFFSET]
52     add eax, 4
53     push eax
54     push DWORD [ebp-BLOCK_OFFSET]
55     call memmove
56     
57     ; realloc to shrink the array
58     mov eax, DWORD [ebp+_BUFFER_LEN]
59     sub eax, 1 
60     mov ecx, 4
61     mul ecx
62     push eax
63     push DWORD [ebp+_BUFFER]
64     call realloc
65     mov [ebp-NEW_BUFFER], eax
66
67     %undef _BUFFER
68     %undef _BUFFER_LEN
69     %undef _SHIFT_POS
70
71     mov eax, [ebp-NEW_BUFFER]
72     
73     mov esp, ebp
74     pop ebp
75     ret
76
77 ; grows array by shifting blocks right
78 ; args: buffer, buffer_len, shift_pos (new uninitalized index)
79 ; return:
80 ;   eax: location of new buffer
81 shiftRight:
82     %define _BUFFER      16
83     %define _BUFFER_LEN  12
84     %define _SHIFT_POS   8
85     %define SHIFT_LEN    4
86     %define BLOCK_OFFSET 8 ; mem location of block to be destroyed
87     %define NEW_BUFFER   12
88
89     push ebp
90     mov ebp, esp
91
92     sub esp, 12
93
94     ; realloc to make memory bigger first
95     mov eax, DWORD [ebp+_BUFFER_LEN]
96     add eax, 1
97     mov ecx, 4
98     mul ecx
99     push eax
100     push DWORD [ebp+_BUFFER]
101     call realloc
102     mov [ebp-NEW_BUFFER], eax
103
104     ; set vars
105     mov eax, DWORD [ebp+_BUFFER_LEN]
106     sub eax, [ebp+_SHIFT_POS]
107     mov [ebp-SHIFT_LEN], eax
108
109     str_offset [ebp-NEW_BUFFER], [ebp+_SHIFT_POS]
110     mov [ebp-BLOCK_OFFSET], eax
111
112     ; move the memory
113     mov eax, DWORD [ebp-SHIFT_LEN]
114     mov ecx, 4
115     mul ecx
116     push eax
117     push DWORD [ebp-BLOCK_OFFSET]
118     mov eax, DWORD [ebp-BLOCK_OFFSET]
119     add eax, 4
120     push eax
121     call memmove
122
123     %undef _BUFFER
124     %undef _BUFFER_LEN
125     %undef _SHIFT_POS
126
127     mov eax, [ebp-NEW_BUFFER]
128     
129     mov esp, ebp
130     pop ebp
131     ret
132