-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlib.asm
271 lines (232 loc) · 7.69 KB
/
lib.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
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
; lib.asm
;
%include "sconst.inc"
[BITS 32]
extern disp_pos
[SECTION .text]
; 导出函数
global memocpy
global print
global out_byte
global in_byte
global memoset
global strcpy
global enable_irq
global disable_irq
global enable_int
global disable_int
; ------------------------------------------------------------------------
; void* memocpy(void* es:pDest, void* ds:pSrc, int iSize);
; ------------------------------------------------------------------------
memocpy:
push ebp
mov ebp, esp
push esi
push edi
push ecx
mov edi, [ebp + 8] ; Destination
mov esi, [ebp + 12] ; Source
mov ecx, [ebp + 16] ; Counter
.1:
cmp ecx, 0 ; 判断计数器
jz .2 ; 计数器为零时跳出
mov al, [ds:esi] ; ┓
inc esi ; ┃
; ┣ 逐字节移动
mov byte [es:edi], al ; ┃
inc edi ; ┛
dec ecx ; 计数器减一
jmp .1 ; 循环
.2:
mov eax, [ebp + 8] ; 返回值
pop ecx
pop edi
pop esi
mov esp, ebp
pop ebp
ret ; 函数结束,返回
; memocpy 结束-------------------------------------------------------------
; ========================================================================
; PUBLIC void print(const char* pszInfo, const int color);
; ========================================================================
print:
push ebp
mov ebp, esp
mov esi, [ebp + 8] ; pszInfo
mov ah, [ebp + 12] ; color
mov edi, dword [disp_pos]
.1:
lodsb
test al, al
jz .2
cmp al, 0Ah ; 是回车吗?
jnz .3
push eax
mov eax, edi
mov bl, 160
div bl
and eax, 0FFh
inc eax
mov bl, 160
mul bl
mov edi, eax
pop eax
jmp .1
.3:
mov [gs:edi], ax
add edi, 2
jmp .1
.2:
mov dword [disp_pos], edi
pop ebp
ret
;----------------------------------------------------------------------------
; ========================================================================
; void out_byte(u16 port, u8 value);
; ========================================================================
out_byte:
mov edx, [esp + 4] ; port
mov al, [esp + 4 + 4] ; value
out dx, al
nop ; 一点延迟
nop
ret
; ========================================================================
; u8 in_byte(u16 port);
; ========================================================================
in_byte:
mov edx, [esp + 4] ; port
xor eax, eax
in al, dx
nop ; 一点延迟
nop
ret
; ========================================================================
; ------------------------------------------------------------------------
; void memoset(void* p_dst, char ch, int size);
; ------------------------------------------------------------------------
memoset:
push ebp
mov ebp, esp
push esi
push edi
push ecx
mov edi, [ebp + 8] ; Destination
mov edx, [ebp + 12] ; Char to be putted
mov ecx, [ebp + 16] ; Counter
.1:
cmp ecx, 0 ; 判断计数器
jz .2 ; 计数器为零时跳出
mov byte [edi], dl ; ┓
inc edi ; ┛
dec ecx ; 计数器减一
jmp .1 ; 循环
.2:
pop ecx
pop edi
pop esi
mov esp, ebp
pop ebp
ret ; 函数结束,返回
; ------------------------------------------------------------------------
; ------------------------------------------------------------------------
; char* strcpy(char* p_dst, char* p_src);
; ------------------------------------------------------------------------
strcpy:
push ebp
mov ebp, esp
mov esi, [ebp + 12] ; Source
mov edi, [ebp + 8] ; Destination
.1:
mov al, [esi] ; ┓
inc esi ; ┃
; ┣ 逐字节移动
mov byte [edi], al ; ┃
inc edi ; ┛
cmp al, 0 ; 是否遇到 '\0'
jnz .1 ; 没遇到就继续循环,遇到就结束
mov eax, [ebp + 8] ; 返回值
pop ebp
ret ; 函数结束,返回
; strcpy 结束-------------------------------------------------------------
; ========================================================================
; void disable_irq(int irq);
; ========================================================================
; Disable an interrupt request line by setting an 8259 bit.
; Equivalent code:
; if(irq < 8)
; out_byte(INT_M_CTLMASK, in_byte(INT_M_CTLMASK) | (1 << irq));
; else
; out_byte(INT_S_CTLMASK, in_byte(INT_S_CTLMASK) | (1 << irq));
disable_irq:
mov ecx, [esp + 4] ; irq
pushf
cli
mov ah, 1
rol ah, cl ; ah = (1 << (irq % 8))
cmp cl, 8
jae disable_8 ; disable irq >= 8 at the slave 8259
disable_0:
in al, INT_M_CTLMASK
test al, ah
jnz dis_already ; already disabled?
or al, ah
out INT_M_CTLMASK, al ; set bit at master 8259
popf
mov eax, 1 ; disabled by this function
ret
disable_8:
in al, INT_S_CTLMASK
test al, ah
jnz dis_already ; already disabled?
or al, ah
out INT_S_CTLMASK, al ; set bit at slave 8259
popf
mov eax, 1 ; disabled by this function
ret
dis_already:
popf
xor eax, eax ; already disabled
ret
; ========================================================================
; void enable_irq(int irq);
; ========================================================================
; Enable an interrupt request line by clearing an 8259 bit.
; Equivalent code:
; if(irq < 8)
; out_byte(INT_M_CTLMASK, in_byte(INT_M_CTLMASK) & ~(1 << irq));
; else
; out_byte(INT_S_CTLMASK, in_byte(INT_S_CTLMASK) & ~(1 << irq));
;
enable_irq:
mov ecx, [esp + 4] ; irq
pushf
cli
mov ah, ~1
rol ah, cl ; ah = ~(1 << (irq % 8))
cmp cl, 8
jae enable_8 ; enable irq >= 8 at the slave 8259
enable_0:
in al, INT_M_CTLMASK
and al, ah
out INT_M_CTLMASK, al ; clear bit at master 8259
popf
ret
enable_8:
in al, INT_S_CTLMASK
and al, ah
out INT_S_CTLMASK, al ; clear bit at slave 8259
popf
ret
; ========================================================================
; void disable_int();
; ========================================================================
disable_int:
cli
ret
; ========================================================================
; void enable_int();
; ========================================================================
enable_int:
sti
ret