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