forked from descent/simple_os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
c_init_by_boot_loader.s
189 lines (167 loc) · 3.35 KB
/
c_init_by_boot_loader.s
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
.code16
.extern __bss_start__
.extern __bss_end__
.text
.global _start
_start:
mov %cs, %ax
mov %ax, %ds
mov %ax, %es
#movw $0x0, %ax
movw %ax, %ds
mov %ax, %ss
# setup stack
mov $0xfff0, %sp # why not setting 0xffff to %sp, in ms dos, 0xfff0 is ok, 0xffb will get core dump
# call disp_str
call init_bss_asm
# for bss
# movw $0x0, %ax
# movw %ax, %ds
# call p
call start_c
# call text_test
# call disp_str2
jmp .
#disp_str:
# mov $BootMessage, %ax
# mov %ax,%bp
# mov $16,%cx
# mov $0x1301,%ax
# mov $0x00c,%bx
# mov $0,%dl
# int $0x10
# ret
#BootMessage:.ascii "Hello, c-env world!"
#disp_str2:
# mov $str2, %ax
# mov %ax,%bp
# mov $5,%cx
# mov $0x1301,%ax
# mov $0x00c,%bx
# mov $0,%dl
# int $0x10
# ret
#str2:.ascii "after"
.globl write_mem8
write_mem8:
pushl %ebp
mov %esp, %ebp
#movw $0xa000, %ax # graphic mode
movw $0xb800, %ax # text mode
movw %ax, %gs
movl 8(%ebp), %ecx;
# mov $0x0, %ecx;
movb 12(%ebp), %al;
movb %al, %gs:(%ecx)
#movb $'A', %gs:(%ecx)
popl %ebp
ret
# init bss
init_bss_asm:
movw $__bss_end__, %di /* Destination */
movw $__bss_start__, %si /* Source */
# movw $0x0, %ax
# movw %ax, %gs
jmp 2f
1:
movw %si, %ax
movb $0x1, (%eax)
# movb $0x1, %ds:(%eax)
add $1, %si
2:
cmpw %di, %si
jne 1b
ret
# copy from: write_os/src/chapter3/8/lib.h
# ex:
# asm_memcpy((u8*)0x100, (u8 *)IMAGE_LMA, 512*3);
# copy IMAGE_LMA to 0x7000:0x100
.globl asm_memcpy
asm_memcpy:
pushl %ebp
mov %esp, %ebp
pushl %esi
pushl %edi
pushl %ecx
mov $0x7000, %ax
mov %ax, %fs
mov 8(%ebp), %edi /* Destination */
mov 12(%ebp), %esi /* Source */
mov 16(%ebp), %ecx /* Counter */
MemCpy.1:
cmp $0, %ecx /* Loop counter */
jz MemCpy.2
movb %ds:(%esi), %al
inc %esi
movb %al, %fs:(%edi)
inc %edi
dec %ecx
jmp MemCpy.1
MemCpy.2:
mov 8(%ebp), %eax
pop %ecx
pop %edi
pop %esi
mov %ebp, %esp
pop %ebp
ret
.globl asm_absolute_memcpy
# ex:
# asm_absolute_memcpy((u8*)0x100, (u8 *)IMAGE_LMA, 512*3);
# copy IMAGE_LMA to 0x100
asm_absolute_memcpy:
pushl %ebp
mov %esp, %ebp
pushl %esi
pushl %edi
pushl %ecx
mov $0, %ax
mov %ax, %fs
mov 8(%ebp), %edi /* Destination */
mov 12(%ebp), %esi /* Source */
mov 16(%ebp), %ecx /* Counter */
1:
cmp $0, %ecx /* Loop counter */
jz 2f
movb %ds:(%esi), %al
inc %esi
movb %al, %fs:(%edi)
inc %edi
dec %ecx
jmp 1b
2:
mov 8(%ebp), %eax
pop %ecx
pop %edi
pop %esi
mov %ebp, %esp
pop %ebp
ret
# movw 16(%ebp), %ecx /* Counter */
#1:
# cmp $0, %ecx /* Loop counter */
# jz MemCpy.2
# movb %ds:(%esi), %al
# inc %esi
# movb %al, %es:(%edi)
# inc %edi
# dec %ecx
# jmp MemCpy.1
#2:
# mov 8(%ebp), %eax
# pop %ecx
# pop %edi
# pop %esi
# mov %ebp, %esp
# pop %ebp
# ret
#.bss
#_bss_start_:.word __bss_start__
#_bss_end_:.word __bss_end__
.data
#LABEL_STACK:
#.space 512, 0
#.section ss
# mov %ax, %es
#.org 510
#.word 0xaa55