第13章程序加载和动态执行

栈的描述符段界限大小的计算

1
2
3
4
5
6
7
8
9
mov dword [ebx+0x18],0x7c00fffe    ;粒度为4KB 
mov dword [ebx+0x1c],0x00cf9600

高: 0x00cf9600
低: 0x7c00fffe
线性地址:0x00007c00
c:1100 ->4kb,操作数32bit,
limit:0xffffe
96:1001_0110_B:数据段读写向下扩展

向上扩展的段界限偏移地址从0开始计算,所以limit就是段的大小,

栈向下扩展,段界限给出来的是EIP能够访问的最小值:(0xffffe+1)*4kb = 0xFFFFF000,

0xFFFF F000 <= EIP <=0xFFFF FFFF

物理地址=偏移地址+段基址(位于描述符高速缓存器中)

0x00006c00 <= <= 0x00007bff

大小:0x7bff-0x6c00+1 = 4KB


  • 所有能被512整除的数字低9位为0

新学到的指令:

  1. sgdt:(Store global descriptor table register):将gdtr寄存器的内容保存到指定的内存位置

  2. movzx(Move with zero-extend),不够逇用0补充

    movsx(Move with Sign-extension):根据符号位来补充0或者1

  3. 比较(compare string operands):16bit: DS:SIES:DI,32bit:DS:EDIES:EDI,

    cmpsb :字节比较

    cmpsw 字比较

    cmpsd 双字比较

  4. 上面的指令只比较1次,指令前缀rep

    repe(repz):相等或者=0就重复

    repne(repnz):不相等或者非零就重复

  • 汇编debug起来实在困难,写了几遍才把写出预期效果,(有的例程在16bit下写过,主要是gdt和段选择子,)
  1. mbr
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
    core_base_address   equ 0x00040000
core_start_sector equ 0x00000001

mov ax,cs
mov ss,ax
mov sp,0x7c00

mov eax,[cs:pgdt+0x7c00+0x02]
xor edx,edx
mov ebx,16
div ebx

mov ds,eax
mov ebx,edx

mov dword [ebx+0x08],0x0000ffff ;基地址为0,段界限为0xFFFFF
mov dword [ebx+0x0c],0x00cf9200 ;粒度为4KB,存储器段描述符

;创建保护模式下初始代码段描述符
mov dword [ebx+0x10],0x7c0001ff ;基地址为0x00007c00,界限0x1FF
mov dword [ebx+0x14],0x00409800 ;粒度为1个字节,代码段描述符

;建立保护模式下的堆栈段描述符 ;基地址为0x00007C00,界限0xFFFFE
mov dword [ebx+0x18],0x7c00fffe ;粒度为4KB
mov dword [ebx+0x1c],0x00cf9600

;建立保护模式下的显示缓冲区描述符
mov dword [ebx+0x20],0x80007fff ;基地址为0x000B8000,界限0x07FFF
mov dword [ebx+0x24],0x0040920b

mov word [cs:pgdt+0x7c00],39

lgdt [cs:pgdt+0x7c00]

in al,0x92
or al,0000_0010B
out 0x92,al

cli

mov eax,cr0
or eax,1
mov cr0,eax

jmp dword 0x0010:flush

[bits 32]
flush:
mov eax,0x0008
mov ds,eax

mov eax,0x0018
mov ss,eax
xor esp,esp

mov edi,core_base_address

mov eax,core_start_sector
mov ebx,edi
call read_hard_disk_0

mov eax,[edi]
xor edx,edx
mov ecx,512
div ecx

or edx,edx
jnz @1
dec eax

@1:
or eax,eax
jz setup

mov ecx,eax
mov eax,core_start_sector
inc eax
@2:
call read_hard_disk_0
inc eax
loop @2

setup:
mov esi,[0x7c00+pgdt+0x02]

mov eax,[edi+0x04]
mov ebx,[edi+0x08]
sub ebx,eax
dec ebx
add eax,edi
mov ecx,0x00409800
call make_gdt_descriptor
mov [esi+0x28],eax
mov [esi+0x2c],edx

mov eax,[edi+0x08]
mov ebx,[edi+0x0c]
sub ebx,eax
dec ebx
add eax,edi
mov ecx,0x00409200
call make_gdt_descriptor
mov [esi+0x30],eax
mov [esi+0x34],edx

mov eax,[edi+0x0c]
mov ebx,[edi+0x00]
sub ebx,eax
dec ebx
add eax,edi
mov ecx,0x00409800
call make_gdt_descriptor
mov [esi+0x38],eax
mov [esi+0x3c],edx

mov word [0x7c00+pgdt],63

lgdt [0x7c00+pgdt]

jmp far [edi+0x10]

;-------------------------------------------------------------------------------
read_hard_disk_0: ;从硬盘读取一个逻辑扇区
;EAX=逻辑扇区号
;DS:EBX=目标缓冲区地址
;返回:EBX=EBX+512
push eax
push ecx
push edx

push eax

mov dx,0x1f2
mov al,1
out dx,al ;读取的扇区数

inc dx ;0x1f3
pop eax
out dx,al ;LBA地址7~0

inc dx ;0x1f4
mov cl,8
shr eax,cl
out dx,al ;LBA地址15~8

inc dx ;0x1f5
shr eax,cl
out dx,al ;LBA地址23~16

inc dx ;0x1f6
shr eax,cl
or al,0xe0 ;第一硬盘 LBA地址27~24
out dx,al

inc dx ;0x1f7
mov al,0x20 ;读命令
out dx,al

.waits:
in al,dx
and al,0x88
cmp al,0x08
jnz .waits ;不忙,且硬盘已准备好数据传输

mov ecx,256 ;总共要读取的字数
mov dx,0x1f0
.readw:
in ax,dx
mov [ebx],ax
add ebx,2
loop .readw

pop edx
pop ecx
pop eax

ret
make_gdt_descriptor:
mov edx,eax
shl eax,16
or ax,bx

and edx,0xffff0000
rol edx,8
bswap edx

xor bx,bx
or edx,ebx

or edx,ecx

ret
pgdt dw 0
dd 0x00007e00
times 510-($-$$) db 0
db 0x55,0xaa
  1. core
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
       core_code_seg_sel    equ 0x38
core_data_seg_sel equ 0x30
sys_routine_seg_sel equ 0x28
video_ram_seg_sel equ 0x20
core_stack_seg_sel equ 0x18
mem_0_4_gb_seg_sel equ 0x08

core_length dd core_end

sys_routine_seg dd section.sys_routine.start

core_data_seg dd section.core_data.start

core_code_seg dd section.core_code.start

core_entry dd start
dw core_code_seg_sel
[bits 32]
SECTION sys_routine vstart=0
put_string:
push ecx
.getc:
mov cl,[ebx]
or cl,cl
jz .exit
call put_char
inc ebx
jmp .getc
.exit:
pop ecx
retf
put_char:
pushad

mov dx,0x3d4
mov al,0x0e
out dx,al
inc dx
in al,dx
mov ah,al

dec dx
mov al,0x0f
out dx,al
inc dx
in al,dx
mov bx,ax

cmp cl,0x0d
jnz .put_0a
mov ax,bx
mov bl,80
div bl
mul bl
mov bx,ax
jmp .set_cursor

.put_0a:
cmp cl,0x0a
jnz .put_other
add bx,80
jmp .roll_screen
.put_other:
push es
mov eax,video_ram_seg_sel
mov es,eax
shl bx,1
mov [es:bx],cl
pop es

shr bx,1
inc bx

.roll_screen:
cmp bx,2000
jl .set_cursor

push ds
push es
mov eax,video_ram_seg_sel
mov ds,eax
mov es,eax

cld

mov esi,0xa0
mov edi,0x00
mov ecx,1920
rep movsd
mov bx,3840
mov ecx,80
.cls:
mov word [es:bx],0x0720
add bx,2
loop .cls

pop es
pop ds

mov bx,1920

.set_cursor:
mov dx,0x3d4
mov al,0x0e
out dx,al
inc dx
mov al,bh
out dx,al
dec dx
mov al,0x0f
out dx,al
inc dx
mov al,bl
out dx,al

popad
ret
read_hard_disk_0: ;从硬盘读取一个逻辑扇区
;EAX=逻辑扇区号
;DS:EBX=目标缓冲区地址
;返回:EBX=EBX+512
push eax
push ecx
push edx

push eax

mov dx,0x1f2
mov al,1
out dx,al ;读取的扇区数

inc dx ;0x1f3
pop eax
out dx,al ;LBA地址7~0

inc dx ;0x1f4
mov cl,8
shr eax,cl
out dx,al ;LBA地址15~8

inc dx ;0x1f5
shr eax,cl
out dx,al ;LBA地址23~16

inc dx ;0x1f6
shr eax,cl
or al,0xe0 ;第一硬盘 LBA地址27~24
out dx,al

inc dx ;0x1f7
mov al,0x20 ;读命令
out dx,al

.waits:
in al,dx
and al,0x88
cmp al,0x08
jnz .waits ;不忙,且硬盘已准备好数据传输

mov ecx,256 ;总共要读取的字数
mov dx,0x1f0
.readw:
in ax,dx
mov [ebx],ax
add ebx,2
loop .readw

pop edx
pop ecx
pop eax

retf ;段间返回

;-------------------------------------------------------------------------------
;汇编语言程序是极难一次成功,而且调试非常困难。这个例程可以提供帮助
put_hex_dword: ;在当前光标处以十六进制形式显示
;一个双字并推进光标
;输入:EDX=要转换并显示的数字
;输出:无
pushad
push ds

mov ax,core_data_seg_sel ;切换到核心数据段
mov ds,ax

mov ebx,bin_hex ;指向核心数据段内的转换表
mov ecx,8
.xlt:
rol edx,4
mov eax,edx
and eax,0x0000000f
xlat

push ecx
mov cl,al
call put_char
pop ecx

loop .xlt

pop ds
popad
retf
allocate_memory:
push ds
push eax
push ebx

mov eax,core_data_seg_sel
mov ds,eax

mov eax,[ram_alloc]
add eax,ecx

mov ecx,[ram_alloc]

mov ebx,eax
and ebx,0xfffffffc
add ebx,4
test eax,0x00000003
cmovnz eax,ebx
mov [ram_alloc],eax

pop ebx
pop eax
pop ds

retf
set_up_gdt_descriptor:
push eax
push ebx
push edx

push ds
push es

mov ebx,core_data_seg_sel
mov ds,ebx

sgdt [pgdt]

mov ebx,mem_0_4_gb_seg_sel
mov es,ebx

movzx ebx,word [pgdt]
inc bx
add ebx,[pgdt+2]

mov [es:ebx],eax
mov [es:ebx+4],edx

add word [pgdt],8

lgdt [pgdt]

mov ax,[pgdt]
xor dx,dx
mov bx,8
div bx
mov cx,ax
shl cx,3

pop es
pop ds

pop edx
pop ebx
pop eax

retf
make_seg_descriptor:
mov edx,eax
shl eax,16
or ax,bx

and edx,0xffff0000
rol edx,8
bswap edx

xor bx,bx
or edx,ebx

or edx,ecx

retf
;===============================================================================
SECTION core_data vstart=0 ;系统核心的数据段
;-------------------------------------------------------------------------------
pgdt dw 0 ;用于设置和修改GDT
dd 0

ram_alloc dd 0x00100000 ;下次分配内存时的起始地址

;符号地址检索表
salt:
salt_1 db '@PrintString'
times 256-($-salt_1) db 0
dd put_string ;偏移地址
dw sys_routine_seg_sel
;段选择子
salt_2 db '@ReadDiskData'
times 256-($-salt_2) db 0
dd read_hard_disk_0
dw sys_routine_seg_sel

salt_3 db '@PrintDwordAsHexString'
times 256-($-salt_3) db 0
dd put_hex_dword
dw sys_routine_seg_sel

salt_4 db '@TerminateProgram'
times 256-($-salt_4) db 0
dd return_point
dw core_code_seg_sel

salt_item_len equ $-salt_4
salt_items equ ($-salt)/salt_item_len

message_1 db ' If you seen this message,that means we '
db 'are now in protect mode,and the system '
db 'core is loaded,and the video display '
db 'routine works perfectly.',0x0d,0x0a,0

message_5 db ' Loading user program...',0

do_status db 'Done.',0x0d,0x0a,0

message_6 db 0x0d,0x0a,0x0d,0x0a,0x0d,0x0a
db ' User program terminated,control returned.',0

bin_hex db '0123456789ABCDEF'
;put_hex_dword子过程用的查找表
core_buf times 2048 db 0 ;内核用的缓冲区

esp_pointer dd 0 ;内核用来临时保存自己的栈指针

cpu_brnd0 db 0x0d,0x0a,' ',0
cpu_brand times 52 db 0
cpu_brnd1 db 0x0d,0x0a,0x0d,0x0a,0

SECTION core_code vstart=0
load_relocate_program:
push ebx
push ecx
push edx
push esi
push edi

push ds
push es

mov eax,core_data_seg_sel
mov ds,eax

mov eax,esi
mov ebx,core_buf
call sys_routine_seg_sel:read_hard_disk_0

mov eax,[core_buf]
mov ebx,eax
and ebx,0xfffffe00
add ebx,512
test eax,0x000001ff
cmovnz eax,ebx

mov ecx,eax
call sys_routine_seg_sel:allocate_memory
mov ebx,ecx
push ebx
xor edx,edx
mov ecx,512
div ecx
mov ecx,eax

mov eax,mem_0_4_gb_seg_sel
mov ds,eax

mov eax,esi
.b1:
call sys_routine_seg_sel:read_hard_disk_0
inc eax
loop .b1 ;循环读,直到读完整个用户程序

;建立程序头部段描述符
pop edi ;恢复程序装载的首地址
mov eax,edi ;程序头部起始线性地址
mov ebx,[edi+0x04] ;段长度
dec ebx ;段界限
mov ecx,0x00409200 ;字节粒度的数据段描述符
call sys_routine_seg_sel:make_seg_descriptor
call sys_routine_seg_sel:set_up_gdt_descriptor
mov [edi+0x04],cx

;建立程序代码段描述符
mov eax,edi
add eax,[edi+0x14] ;代码起始线性地址
mov ebx,[edi+0x18] ;段长度
dec ebx ;段界限
mov ecx,0x00409800 ;字节粒度的代码段描述符
call sys_routine_seg_sel:make_seg_descriptor
call sys_routine_seg_sel:set_up_gdt_descriptor
mov [edi+0x14],cx

;建立程序数据段描述符
mov eax,edi
add eax,[edi+0x1c] ;数据段起始线性地址
mov ebx,[edi+0x20] ;段长度
dec ebx ;段界限
mov ecx,0x00409200 ;字节粒度的数据段描述符
call sys_routine_seg_sel:make_seg_descriptor
call sys_routine_seg_sel:set_up_gdt_descriptor
mov [edi+0x1c],cx

;建立程序堆栈段描述符
mov ecx,[edi+0x0c] ;4KB的倍率
mov ebx,0x000fffff
sub ebx,ecx ;得到段界限
mov eax,4096
mul dword [edi+0x0c]
mov ecx,eax ;准备为堆栈分配内存
call sys_routine_seg_sel:allocate_memory
add eax,ecx ;得到堆栈的高端物理地址
mov ecx,0x00c09600 ;4KB粒度的堆栈段描述符
call sys_routine_seg_sel:make_seg_descriptor
call sys_routine_seg_sel:set_up_gdt_descriptor
mov [edi+0x08],cx

;重定位SALT
mov eax,[edi+0x04]
mov es,eax ;es -> 用户程序头部
mov eax,core_data_seg_sel
mov ds,eax

cld ;串行指令正向移动

mov ecx,[es:0x24] ;用户程序的SALT条目数
mov edi,0x28 ;用户程序内的SALT位于头部内0x2c
.b2:
push ecx
push edi

mov ecx,salt_items ;内部循环对比内核的salt
mov esi,salt ;内核salt首地址
.b3:
push edi
push esi
push ecx

mov ecx,64 ;检索表中,每条目的比较次数
repe cmpsd ;每次比较4字节
jnz .b4
mov eax,[esi] ;若匹配,esi恰好指向其后的地址数据
mov [es:edi-256],eax ;将字符串改写成偏移地址
mov ax,[esi+4]
mov [es:edi-252],ax ;以及段选择子
.b4:

pop ecx
pop esi
add esi,salt_item_len
pop edi ;从头比较
loop .b3

pop edi
add edi,256
pop ecx
loop .b2

mov ax,[es:0x04]

pop es ;恢复到调用此过程前的es段
pop ds ;恢复到调用此过程前的ds段

pop edi
pop esi
pop edx
pop ecx
pop ebx

ret

start:
mov ecx,core_data_seg_sel
mov ds,ecx

mov ebx,message_1
call sys_routine_seg_sel:put_string

;显示处理器品牌信息
mov eax,0x80000002
cpuid
mov [cpu_brand + 0x00],eax
mov [cpu_brand + 0x04],ebx
mov [cpu_brand + 0x08],ecx
mov [cpu_brand + 0x0c],edx

mov eax,0x80000003
cpuid
mov [cpu_brand + 0x10],eax
mov [cpu_brand + 0x14],ebx
mov [cpu_brand + 0x18],ecx
mov [cpu_brand + 0x1c],edx

mov eax,0x80000004
cpuid
mov [cpu_brand + 0x20],eax
mov [cpu_brand + 0x24],ebx
mov [cpu_brand + 0x28],ecx
mov [cpu_brand + 0x2c],edx

mov ebx,cpu_brnd0
call sys_routine_seg_sel:put_string
mov ebx,cpu_brand
call sys_routine_seg_sel:put_string
mov ebx,cpu_brnd1
call sys_routine_seg_sel:put_string

mov ebx,message_5
call sys_routine_seg_sel:put_string
mov esi,50
call load_relocate_program

mov ebx,do_status
call sys_routine_seg_sel:put_string

mov [esp_pointer],esp

mov ds,ax

jmp far [0x10]

return_point:
mov eax,core_data_seg_sel
mov ds,eax

mov eax,core_stack_seg_sel
mov ss,eax
mov esp,[esp_pointer]

mov ebx,message_6
call sys_routine_seg_sel:put_string

hlt
SECTION core_trail
core_end:

  1. program
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
SECTION header vstart=0
program_length dd program_end

head_len dd header_end

stack_seg dd 0
stack_len dd 1

prgentry dd start

code_seg dd section.code.start
code_len dd code_end

data_seg dd section.data.start
data_len dd data_end

salt_items dd (header_end-salt)/256

salt:
PrintString db '@PrintString'
times 256-($-PrintString) db 0

TerminateProgram db '@TerminateProgram'
times 256-($-TerminateProgram) db 0

ReadDiskData db '@ReadDiskData'
times 256-($-ReadDiskData) db 0
header_end:
SECTION data vstart=0
buffer times 1024 db 0
message_1 db 0x0d,0x0a,0x0d,0x0a
db '**********User program is runing**********'
db 0x0d,0x0a,0
message_2 db ' Disk data:',0x0d,0x0a,0
data_end:

[bits 32]
SECTION code vstart=0
start:
mov eax,ds
mov fs,eax

mov eax,[stack_seg]
mov ss,eax
mov esp,0

mov eax,[data_seg]
mov ds,eax

mov ebx,message_1
call far [fs:PrintString]

mov eax,100
mov ebx,buffer
call far [fs:ReadDiskData]

mov ebx,message_2
call far [fs:PrintString]

mov ebx,buffer
call far [fs:PrintString]

jmp far [fs:TerminateProgram]

code_end:
SECTION trail
program_end: