30 畅游 x86 分页机制(下)

参考

https://blog.51cto.com/13475106/category6.html及狄泰软件相关课程

 

 

 

 

 

 

 

 

代码如下:

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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
%include "inc.asm"
 
PageDirBase0    equ    0x200000
PageTblBase0    equ    0x201000
PageDirBase1    equ    0x700000
PageTblBase1    equ    0x701000
 
ObjectAddrX     equ    0x401000
TargetAddrY     equ    0xD01000
TargetAddrZ     equ    0xE01000
 
org 0x9000
 
jmp ENTRY_SEGMENT
 
[section .gdt]
; GDT definition
;                                       段基址,           段界限,       段属性
GDT_ENTRY         :     Descriptor        0,                0,           0
CODE32_DESC       :     Descriptor        0,        Code32SegLen - 1,    DA_C + DA_32
VIDEO_DESC        :     Descriptor     0xB8000,         0x07FFF,         DA_DRWA + DA_32
DATA32_DESC       :     Descriptor        0,        Data32SegLen - 1,    DA_DRW + DA_32
STACK32_DESC      :     Descriptor        0,         TopOfStack32,       DA_DRW + DA_32
PAGE_DIR_DESC0    :     Descriptor    PageDirBase0,       4095,          DA_DRW + DA_32
PAGE_TBL_DESC0    :     Descriptor    PageTblBase0,       1023,          DA_DRW + DA_LIMIT_4K + DA_32
PAGE_DIR_DESC1    :     Descriptor    PageDirBase1,       4095,          DA_DRW + DA_32
PAGE_TBL_DESC1    :     Descriptor    PageTblBase1,       1023,          DA_DRW + DA_LIMIT_4K + DA_32
FLAT_MODE_RW_DESC :     Descriptor        0,             0xFFFFF,        DA_DRW + DA_LIMIT_4K + DA_32
; GDT end
 
GdtLen    equ   $ - GDT_ENTRY
 
GdtPtr:
          dw   GdtLen - 1
          dd   0
           
           
; GDT Selector
 
Code32Selector      equ (0x0001 << 3) + SA_TIG + SA_RPL0
VideoSelector       equ (0x0002 << 3) + SA_TIG + SA_RPL0
Data32Selector      equ (0x0003 << 3) + SA_TIG + SA_RPL0
Stack32Selector     equ (0x0004 << 3) + SA_TIG + SA_RPL0
PageDirSelector0    equ (0x0005 << 3) + SA_TIG + SA_RPL0
PageTblSelector0    equ (0x0006 << 3) + SA_TIG + SA_RPL0
PageDirSelector1    equ (0x0007 << 3) + SA_TIG + SA_RPL0
PageTblSelector1    equ (0x0008 << 3) + SA_TIG + SA_RPL0
FlatModeRWSelector  equ (0x0009 << 3) + SA_TIG + SA_RPL0
; end of [section .gdt]
 
TopOfStack16    equ 0x7c00
 
[section .dat]
[bits 32]
DATA32_SEGMENT:
    DTOS               db  "D.T.OS!", 0
    DTOS_LEN           equ $ - DTOS
    DTOS_OFFSET        equ DTOS - $$
    HELLO_WORLD        db  "Hello World!", 0
    HELLO_WORLD_LEN    equ $ - HELLO_WORLD
    HELLO_WORLD_OFFSET equ HELLO_WORLD - $$
 
Data32SegLen equ $ - DATA32_SEGMENT
 
[section .s16]
[bits 16]
ENTRY_SEGMENT:
    mov ax, cs
    mov ds, ax
    mov es, ax
    mov ss, ax
    mov sp, TopOfStack16
     
    ; initialize GDT for 32 bits code segment
    mov esi, CODE32_SEGMENT
    mov edi, CODE32_DESC
     
    call InitDescItem
     
    mov esi, DATA32_SEGMENT
    mov edi, DATA32_DESC
     
    call InitDescItem
     
    mov esi, STACK32_SEGMENT
    mov edi, STACK32_DESC
     
    call InitDescItem
     
    ; initialize GDT pointer struct
    mov eax, 0
    mov ax, ds
    shl eax, 4
    add eax, GDT_ENTRY
    mov dword [GdtPtr + 2], eax
 
    ; 1. load GDT
    lgdt [GdtPtr]
     
    ; 2. close interrupt
    cli
     
    ; 3. open A20
    in al, 0x92
    or al, 00000010b
    out 0x92, al
     
    ; 4. enter protect mode
    mov eax, cr0
    or eax, 0x01
    mov cr0, eax
     
    ; 5. jump to 32 bits code
    jmp dword Code32Selector : 0
 
 
; esi    --> code segment label
; edi    --> descriptor label
InitDescItem:
    push eax
 
    mov eax, 0
    mov ax, cs
    shl eax, 4
    add eax, esi
    mov word [edi + 2], ax
    shr eax, 16
    mov byte [edi + 4], al
    mov byte [edi + 7], ah
     
    pop eax
     
    ret
 
     
[section .s32]
[bits 32]
CODE32_SEGMENT:
    mov ax, VideoSelector
    mov gs, ax
     
    mov ax, Stack32Selector
    mov ss, ax
     
    mov eax, TopOfStack32
    mov esp, eax
     
    mov ax, Data32Selector
    mov ds, ax
     
    mov ax, FlatModeRWSelector
    mov es, ax
     
    mov esi, DTOS_OFFSET
    mov edi, TargetAddrY
    mov ecx, DTOS_LEN
     
    call MemCpy32
     
    mov esi, HELLO_WORLD_OFFSET
    mov edi, TargetAddrZ
    mov ecx, HELLO_WORLD_LEN
     
    call MemCpy32
     
    mov eax, PageDirSelector0
    mov ebx, PageTblSelector0
    mov ecx, PageTblBase0
     
    call InitPageTable
     
    mov eax, PageDirSelector1
    mov ebx, PageTblSelector1
    mov ecx, PageTblBase1
     
    call InitPageTable
     
    mov eax, ObjectAddrX   ; 0x401000
    mov ebx, TargetAddrY   ; 0xD01000
    mov ecx, PageDirBase0
     
    call MapAddress
     
    mov eax, ObjectAddrX   ; 0x401000
    mov ebx, TargetAddrZ   ; 0xE01000
    mov ecx, PageDirBase1
     
    call MapAddress
     
    mov eax, PageDirBase0
     
    call SwitchPageTable
     
    mov ax, FlatModeRWSelector
    mov ds, ax
    mov ebp, ObjectAddrX
    mov bx, 0x0C
    mov dh, 12
    mov dl, 33
     
    call PrintString
     
    mov eax, PageDirBase1
     
    call SwitchPageTable
     
    mov ax, FlatModeRWSelector
    mov ds, ax
    mov ebp, ObjectAddrX
    mov bx, 0x0C
    mov dh, 13
    mov dl, 31
     
    call PrintString
     
    jmp $
 
; es  --> flat mode
; eax --> virtual address
; ebx --> target  address
; ecx --> page directory base
MapAddress:
    push edi
    push esi
    push eax    ; [esp + 8]
    push ebx    ; [esp + 4]
    push ecx    ; [esp]
     
    ; 1. 取虚地址高 10 位, 计算子页表在页目录中的位置
    mov eax, [esp + 8]
    shr eax, 22
    and eax, 1111111111b
    shl eax, 2
     
    ; 2. 取虚地址中间 10 位, 计算物理地址在子页表中的位置
    mov ebx, [esp + 8]
    shr ebx, 12
    and ebx, 1111111111b
    shl ebx, 2
     
    ; 3. 取子页表起始地址
    mov esi, [esp]
    add esi, eax
    mov edi, [es:esi]
    and edi, 0xFFFFF000
     
    ; 4. 将目标地址写入子页表的对应位置
    add edi, ebx
    mov ecx, [esp + 4]
    and ecx, 0xFFFFF000
    or  ecx, PG_P | PG_USU | PG_RWW
    mov [es:edi], ecx
     
    pop ecx
    pop ebx
    pop eax
    pop esi
    pop edi
     
    ret
 
; es    --> flat mode selector
; ds:si --> source
; es:di --> destination
; cx    --> length
MemCpy32:
    push esi
    push edi
    push ecx
    push ax
     
    cmp esi, edi
     
    ja btoe
     
    add esi, ecx
    add edi, ecx
    dec esi
    dec edi
     
    jmp etob
     
btoe:
    cmp ecx, 0
    jz done
    mov al, [ds:esi]
    mov byte [es:edi], al
    inc esi
    inc edi
    dec ecx
    jmp btoe
     
etob:
    cmp ecx, 0
    jz done
    mov al, [ds:esi]
    mov byte [es:edi], al
    dec esi
    dec edi
    dec ecx
    jmp etob
 
done:  
    pop ax
    pop ecx
    pop edi
    pop esi
    ret
     
; eax --> page dir base selector
; ebx --> page table base selector
; ecx --> page table base
InitPageTable:
    push es
    push eax  ; [esp + 12]
    push ebx  ; [esp + 8]
    push ecx  ; [esp + 4]
    push edi  ; [esp]
     
    mov es, ax
    mov ecx, 1024    ;  1K sub page tables
    mov edi, 0
    mov eax, [esp + 4]
    or  eax, PG_P | PG_USU | PG_RWW
     
    cld
     
stdir:
    stosd
    add eax, 4096
    loop stdir
     
    mov ax, [esp + 8]
    mov es, ax
    mov ecx, 1024 * 1024   ; 1M pages
    mov edi, 0
    mov eax, PG_P | PG_USU | PG_RWW
     
    cld
     
sttbl:
    stosd
    add eax, 4096
    loop sttbl
     
    pop edi
    pop ecx
    pop ebx
    pop eax
    pop es
     
    ret  
 
; eax --> page directory base
SwitchPageTable:
    push eax
     
    mov eax, cr0
    and eax, 0x7FFFFFFF
    mov cr0, eax
     
    mov eax, [esp]
    mov cr3, eax
    mov eax, cr0
    or  eax, 0x80000000
    mov cr0, eax
     
    pop eax
     
    ret
 
 
; ds:ebp    --> string address
; bx        --> attribute
; dx        --> dh : row, dl : col
PrintString:
    push ebp
    push eax
    push edi
    push cx
    push dx
     
print:
    mov cl, [ds:ebp]
    cmp cl, 0
    je end
    mov eax, 80
    mul dh
    add al, dl
    shl eax, 1
    mov edi, eax
    mov ah, bl
    mov al, cl
    mov [gs:edi], ax
    inc ebp
    inc dl
    jmp print
 
end:
    pop dx
    pop cx
    pop edi
    pop eax
    pop ebp
     
    ret
     
Code32SegLen    equ    $ - CODE32_SEGMENT
 
[section .gs]
[bits 32]
STACK32_SEGMENT:
    times 1024 * 4 db 0
     
Stack32SegLen equ $ - STACK32_SEGMENT
TopOfStack32  equ Stack32SegLen - 1

 

运行结果如下:

 

 

 

 

 

 

 

 

 

 

 

 

 

 代码如下:

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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
%include "inc.asm"
 
PageDirBase0    equ    0x200000
PageTblBase0    equ    0x201000
PageDirBase1    equ    0x700000
PageTblBase1    equ    0x701000
 
ObjectAddrX     equ    0x401000
TargetAddrY     equ    0xD01000
TargetAddrZ     equ    0xE01000
 
org 0x9000
 
jmp ENTRY_SEGMENT
 
[section .gdt]
; GDT definition
;                                       段基址,           段界限,       段属性
GDT_ENTRY         :     Descriptor        0,                0,           0
CODE32_DESC       :     Descriptor        0,        Code32SegLen - 1,    DA_C + DA_32
VIDEO_DESC        :     Descriptor     0xB8000,         0x07FFF,         DA_DRWA + DA_32
DATA32_DESC       :     Descriptor        0,        Data32SegLen - 1,    DA_DRW + DA_32
STACK32_DESC      :     Descriptor        0,         TopOfStack32,       DA_DRW + DA_32
PAGE_DIR_DESC0    :     Descriptor    PageDirBase0,       4095,          DA_DRW + DA_32
PAGE_TBL_DESC0    :     Descriptor    PageTblBase0,       1023,          DA_DRW + DA_LIMIT_4K + DA_32
PAGE_DIR_DESC1    :     Descriptor    PageDirBase1,       4095,          DA_DRW + DA_32
PAGE_TBL_DESC1    :     Descriptor    PageTblBase1,       1023,          DA_DRW + DA_LIMIT_4K + DA_32
FUNC32_DESC       :     Descriptor        0,        Func32SegLen - 1,    DA_DR + DA_32
FLAT_MODE_RW_DESC :     Descriptor        0,             0xFFFFF,        DA_DRW + DA_LIMIT_4K + DA_32
FLAT_MODE_C_DESC  :     Descriptor        0,             0xFFFFF,        DA_C + DA_LIMIT_4K + DA_32
; GDT end
 
GdtLen    equ   $ - GDT_ENTRY
 
GdtPtr:
          dw   GdtLen - 1
          dd   0
           
           
; GDT Selector
 
Code32Selector      equ (0x0001 << 3) + SA_TIG + SA_RPL0
VideoSelector       equ (0x0002 << 3) + SA_TIG + SA_RPL0
Data32Selector      equ (0x0003 << 3) + SA_TIG + SA_RPL0
Stack32Selector     equ (0x0004 << 3) + SA_TIG + SA_RPL0
PageDirSelector0    equ (0x0005 << 3) + SA_TIG + SA_RPL0
PageTblSelector0    equ (0x0006 << 3) + SA_TIG + SA_RPL0
PageDirSelector1    equ (0x0007 << 3) + SA_TIG + SA_RPL0
PageTblSelector1    equ (0x0008 << 3) + SA_TIG + SA_RPL0
Func32Selector      equ (0x0009 << 3) + SA_TIG + SA_RPL0
FlatModeRWSelector  equ (0x000A << 3) + SA_TIG + SA_RPL0
FlatModeCSelector   equ (0x000B << 3) + SA_TIG + SA_RPL0
; end of [section .gdt]
 
TopOfStack16    equ 0x7c00
 
[section .dat]
[bits 32]
DATA32_SEGMENT:
    DTOS               db  "D.T.OS!", 0
    DTOS_LEN           equ $ - DTOS
    DTOS_OFFSET        equ DTOS - $$
    HELLO_WORLD        db  "Hello World!", 0
    HELLO_WORLD_LEN    equ $ - HELLO_WORLD
    HELLO_WORLD_OFFSET equ HELLO_WORLD - $$
 
Data32SegLen equ $ - DATA32_SEGMENT
 
[section .func]
[bits 32]
FUNC32_SEGMENT:
 
; cx --> n
; return:
;     eax --> n * n
Sqr:
    mov eax, 0
     
    mov ax, cx
    mul cx
     
    retf
     
SqrLen  equ  $ - Sqr
SqrFunc equ  Sqr - $$
 
; cx --> n
; return:
;     eax --> 1 + 2 + 3 + ... + n
Acc:
    push cx
     
    mov eax, 0
     
accloop:
    add ax, cx
    loop accloop
     
    pop cx
     
    retf
     
AccLen  equ  $ - Acc
AccFunc equ  Acc - $$
 
Func32SegLen  equ  $ - FUNC32_SEGMENT
 
[section .s16]
[bits 16]
ENTRY_SEGMENT:
    mov ax, cs
    mov ds, ax
    mov es, ax
    mov ss, ax
    mov sp, TopOfStack16
     
    ; initialize GDT for 32 bits code segment
    mov esi, CODE32_SEGMENT
    mov edi, CODE32_DESC
     
    call InitDescItem
     
    mov esi, DATA32_SEGMENT
    mov edi, DATA32_DESC
     
    call InitDescItem
     
    mov esi, STACK32_SEGMENT
    mov edi, STACK32_DESC
     
    call InitDescItem
     
    mov esi, FUNC32_SEGMENT
    mov edi, FUNC32_DESC
     
    call InitDescItem
     
    ; initialize GDT pointer struct
    mov eax, 0
    mov ax, ds
    shl eax, 4
    add eax, GDT_ENTRY
    mov dword [GdtPtr + 2], eax
 
    ; 1. load GDT
    lgdt [GdtPtr]
     
    ; 2. close interrupt
    cli
     
    ; 3. open A20
    in al, 0x92
    or al, 00000010b
    out 0x92, al
     
    ; 4. enter protect mode
    mov eax, cr0
    or eax, 0x01
    mov cr0, eax
     
    ; 5. jump to 32 bits code
    jmp dword Code32Selector : 0
 
 
; esi    --> code segment label
; edi    --> descriptor label
InitDescItem:
    push eax
 
    mov eax, 0
    mov ax, cs
    shl eax, 4
    add eax, esi
    mov word [edi + 2], ax
    shr eax, 16
    mov byte [edi + 4], al
    mov byte [edi + 7], ah
     
    pop eax
     
    ret
 
     
[section .s32]
[bits 32]
CODE32_SEGMENT:
    mov ax, VideoSelector
    mov gs, ax
     
    mov ax, Stack32Selector
    mov ss, ax
     
    mov eax, TopOfStack32
    mov esp, eax
     
    mov ax, Func32Selector
    mov ds, ax
     
    mov ax, FlatModeRWSelector
    mov es, ax
     
    mov esi, SqrFunc
    mov edi, TargetAddrY
    mov ecx, SqrLen
     
    call MemCpy32
     
    mov esi, AccFunc
    mov edi, TargetAddrZ
    mov ecx, AccLen
     
    call MemCpy32
     
    mov eax, PageDirSelector0
    mov ebx, PageTblSelector0
    mov ecx, PageTblBase0
     
    call InitPageTable
     
    mov eax, PageDirSelector1
    mov ebx, PageTblSelector1
    mov ecx, PageTblBase1
     
    call InitPageTable
     
    mov eax, ObjectAddrX   ; 0x401000
    mov ebx, TargetAddrY   ; 0xD01000
    mov ecx, PageDirBase0
     
    call MapAddress
     
    mov eax, ObjectAddrX   ; 0x401000
    mov ebx, TargetAddrZ   ; 0xE01000
    mov ecx, PageDirBase1
     
    call MapAddress
     
    mov eax, PageDirBase0
     
    call SwitchPageTable
     
    mov ecx, 100
     
    call FlatModeCSelector : ObjectAddrX
     
    mov eax, PageDirBase1
     
    call SwitchPageTable
     
    mov ecx, 100
     
    call FlatModeCSelector : ObjectAddrX
     
    jmp $
 
; es  --> flat mode
; eax --> virtual address
; ebx --> target  address
; ecx --> page directory base
MapAddress:
    push edi
    push esi
    push eax    ; [esp + 8]
    push ebx    ; [esp + 4]
    push ecx    ; [esp]
     
    ; 1. 取虚地址高 10 位, 计算子页表在页目录中的位置
    mov eax, [esp + 8]
    shr eax, 22
    and eax, 1111111111b
    shl eax, 2
     
    ; 2. 取虚地址中间 10 位, 计算物理地址在子页表中的位置
    mov ebx, [esp + 8]
    shr ebx, 12
    and ebx, 1111111111b
    shl ebx, 2
     
    ; 3. 取子页表起始地址
    mov esi, [esp]
    add esi, eax
    mov edi, [es:esi]
    and edi, 0xFFFFF000
     
    ; 4. 将目标地址写入子页表的对应位置
    add edi, ebx
    mov ecx, [esp + 4]
    and ecx, 0xFFFFF000
    or  ecx, PG_P | PG_USU | PG_RWW
    mov [es:edi], ecx
     
    pop ecx
    pop ebx
    pop eax
    pop esi
    pop edi
     
    ret
 
; es    --> flat mode selector
; ds:si --> source
; es:di --> destination
; cx    --> length
MemCpy32:
    push esi
    push edi
    push ecx
    push ax
     
    cmp esi, edi
     
    ja btoe
     
    add esi, ecx
    add edi, ecx
    dec esi
    dec edi
     
    jmp etob
     
btoe:
    cmp ecx, 0
    jz done
    mov al, [ds:esi]
    mov byte [es:edi], al
    inc esi
    inc edi
    dec ecx
    jmp btoe
     
etob:
    cmp ecx, 0
    jz done
    mov al, [ds:esi]
    mov byte [es:edi], al
    dec esi
    dec edi
    dec ecx
    jmp etob
 
done:  
    pop ax
    pop ecx
    pop edi
    pop esi
    ret
     
; eax --> page dir base selector
; ebx --> page table base selector
; ecx --> page table base
InitPageTable:
    push es
    push eax  ; [esp + 12]
    push ebx  ; [esp + 8]
    push ecx  ; [esp + 4]
    push edi  ; [esp]
     
    mov es, ax
    mov ecx, 1024    ;  1K sub page tables
    mov edi, 0
    mov eax, [esp + 4]
    or  eax, PG_P | PG_USU | PG_RWW
     
    cld
     
stdir:
    stosd
    add eax, 4096
    loop stdir
     
    mov ax, [esp + 8]
    mov es, ax
    mov ecx, 1024 * 1024   ; 1M pages
    mov edi, 0
    mov eax, PG_P | PG_USU | PG_RWW
     
    cld
     
sttbl:
    stosd
    add eax, 4096
    loop sttbl
     
    pop edi
    pop ecx
    pop ebx
    pop eax
    pop es
     
    ret  
 
; eax --> page directory base
SwitchPageTable:
    push eax
     
    mov eax, cr0
    and eax, 0x7FFFFFFF
    mov cr0, eax
     
    mov eax, [esp]
    mov cr3, eax
    mov eax, cr0
    or  eax, 0x80000000
    mov cr0, eax
     
    pop eax
     
    ret
 
 
; ds:ebp    --> string address
; bx        --> attribute
; dx        --> dh : row, dl : col
PrintString:
    push ebp
    push eax
    push edi
    push cx
    push dx
     
print:
    mov cl, [ds:ebp]
    cmp cl, 0
    je end
    mov eax, 80
    mul dh
    add al, dl
    shl eax, 1
    mov edi, eax
    mov ah, bl
    mov al, cl
    mov [gs:edi], ax
    inc ebp
    inc dl
    jmp print
 
end:
    pop dx
    pop cx
    pop edi
    pop eax
    pop ebp
     
    ret
     
Code32SegLen    equ    $ - CODE32_SEGMENT
 
[section .gs]
[bits 32]
STACK32_SEGMENT:
    times 1024 * 4 db 0
     
Stack32SegLen equ $ - STACK32_SEGMENT
TopOfStack32  equ Stack32SegLen - 1

 

运行结果如下:

 

 从结果上看到运行结束没有异常,若要看到具体调用过程及结果,需要使用断点调试。

 

对于页表及相关内容,总结如下:

 

 

 

  

  

posted on   lh03061238  阅读(172)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示