Skip to content

Commit b23ab06

Browse files
committed
Lab02
第二个实验
1 parent 8e2d760 commit b23ab06

File tree

8 files changed

+397
-0
lines changed

8 files changed

+397
-0
lines changed

Lab02/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
设计满足下列要求的原型操作系统:
2+
1.用户程序大小不等,但最大不超过5个扇区。
3+
2.允许用控制台命令指定一组要执行的用户程序。比如,磁盘上有3个用户程序,可以用命令指定按某顺序运行其中1个、2个或3个程序。
4+

Lab02/disk/Lab2.img

3 KB
Binary file not shown.

Lab02/doc/实验说明.doc

794 KB
Binary file not shown.

Lab02/src/boot.asm

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
;
2+
; 本文件为引导程序的源码文件。
3+
;
4+
org 7c00h ; BIOS将把引导扇区加载到0:7C00h处,并开始执行
5+
START:
6+
mov ax,cs ; 用 cs 的值初始化 ds,es
7+
mov ds,ax
8+
mov es,ax
9+
10+
call ReadOS ; 装载操作系统
11+
call dword 9000h; ; 跳转到操作系统的执行
12+
13+
ReadOS: ; 读入软盘第2和第3个扇区到7E00H处
14+
xor ax,ax ; 相当于mov ax,0
15+
mov es,ax ; ES=0
16+
mov bx,9000H ; ES:BX=读入数据到内存中的存储地址
17+
mov ah,2 ; 功能号
18+
mov al,2 ; 要读入的扇区数
19+
mov dl,0 ; 软盘驱动器号(对硬盘和U盘,此处的值应改为80H)
20+
mov dh,0 ; 磁头号
21+
mov ch,0 ; 柱面号
22+
mov cl,2 ; 起始扇区号(编号从1开始)
23+
int 13H ; 调用13H号中断
24+
ret ; 从例程返回
25+
26+
times 510-($-$$) db 0 ; 用0填充引导扇区剩下的空间
27+
db 55h, 0aah ; 引导扇区结束标志

Lab02/src/myos.asm

+193
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
;
2+
; 本文件为引导后启动的操作系统的源码文件。
3+
;
4+
org 9000h ; 告诉编译器程序加载到7C00H处
5+
mov ax,cs ; 初始化数据段与附加段寄存器与代码段的相同
6+
mov ds,ax
7+
mov es,ax
8+
9+
call Main ; 读取主函数
10+
11+
Main: ; 可视为主函数
12+
call Clear ; 清屏
13+
call ReInit ; 将变量重新初始化
14+
call DispStr ; 调用显示字符串
15+
jmp Keyin ; 等待用户进行输入选择
16+
17+
DispStr: ; 显示字符串
18+
; 显示字符串1 "Chen-OS 1.0"(开始)
19+
mov ah,13h ; 功能号
20+
mov al,1 ; 光标放到串尾
21+
mov bl,0ah ; 亮绿
22+
mov bh,0 ; 第0页
23+
mov dh,05h ; 第5行
24+
mov dl,20h ; 第32列
25+
mov bp,str1 ; BP=串地址
26+
mov cx,Length1 ; 串长为 Length1
27+
int 10h ; 调用10H号中断
28+
; 显示字符串1(结束)
29+
; 显示字符串2 "(C) 2014 liaojch3"(开始)
30+
mov ah,13h ; 功能号
31+
mov al,1 ; 光标放到串尾
32+
mov bl,0dh ; 黑底品红字
33+
mov bh,0 ; 第0页
34+
mov dh,07h ; 第7行
35+
mov dl,1eh ; 第 30 列
36+
mov bp,str2 ; BP=串地址
37+
mov cx,Length2 ; 串长为 Length2
38+
int 10h ; 调用10H号中断
39+
; 显示字符串2(结束)
40+
; 显示字符串3
41+
mov ah,13h ; 功能号
42+
mov al,1 ; 光标放到串尾
43+
mov bl,0ch ; 黑底红字
44+
mov bh,0 ; 第0页
45+
mov dh,0ah ; 第10行
46+
mov dl,0 ; 第 0 列
47+
mov bp,str3 ; BP=串地址
48+
mov cx,Length3 ; 串长为 Length3
49+
int 10h ; 调用10H号中断
50+
; 显示字符串3(结束)
51+
ret ; 返回调用处
52+
53+
54+
ReadPro: ; 读入程序(软盘第4 或 第5 或 第6个扇区)到内存7E00H处)
55+
xor ax,ax ; 相当于mov ax,0
56+
mov es,ax ; ES=0
57+
mov bx,7e00H ; ES:BX=读入数据到内存中的存储地址
58+
mov ah,2 ; 功能号
59+
mov al,1 ; 要读入的扇区数 1
60+
mov dl,0 ; 软盘驱动器号(对硬盘和U盘,此处的值应改为80H)
61+
mov dh,0 ; 磁头号
62+
mov ch,0 ; 柱面号
63+
mov cl,byte[p] ; 起始扇区号(编号从1开始)
64+
int 13H ; 调用13H号中断
65+
ret ; 返回
66+
67+
68+
Keyin:
69+
mov ah,0 ; 功能号
70+
int 16h ; 调用16H号中断
71+
cmp al,0dh ; 判断是否是回车,回车的 Ascii 码为 0dh(13)
72+
je Excute ; 开始执行用户选择的程序顺序
73+
jmp showch ; 没有回车,则显示用户键入的字符
74+
75+
showch: ; 显示键入字符
76+
mov ah,0eh ; 功能号
77+
mov bl,0 ; 对文本方式置0
78+
int 10h ; 调用10H号中断
79+
jmp Continue
80+
81+
Continue: ; 继续执行
82+
inc word[i] ; i++
83+
mov ah,0 ; ah 置零
84+
cmp word[i],4 ; 判断用户是否键入超过 3 个字符
85+
je Main ; 超过 3 个则进行刷新显示
86+
cmp word[i],2 ; 是否是输入的第二个字符(i = 2)
87+
je I2 ; 跳转到 I2,存储第二个字符到 y
88+
cmp word[i],3 ; 是否是输入的第三个字符
89+
je I3 ; 跳转到 I3,存储第三个字符到 z
90+
mov word[x],ax ; 以上不成立,则是第一个字符,存入 x
91+
jmp Keyin ; 跳转到准备接受下一个输入处
92+
I2: mov word[y],ax ; 存储第二个字符到 y
93+
jmp Keyin ; 跳转到准备接受下一个输入处
94+
I3: mov word[z],ax ; 存储第三个字符到 z
95+
jmp Keyin ; 跳转到准备接受下一个输入处
96+
97+
Excute: ; 开始执行用户选择的程序
98+
mov ax,0 ; ax 置零
99+
CMP1: ;判断用户选择的第一个程序是否执行
100+
cmp word[x],ax ; 判断 x 是否为 0,0 代表执行过
101+
je CMP2 ; x 为 0 则开始判断 y
102+
jmp Ex1 ; x 不为 0 开始执行用户选择的第一个程序
103+
Ex1: ;执行用户选择的第一个程序
104+
mov ax,word[x] ; 把 x 放到 ax
105+
mov word[cur],ax; 把 ax 放到 cur 变量
106+
mov ax,0 ; ax 置零
107+
mov word[x],ax ; ax 放到 x, 将 x 清零
108+
call Run ; 开始执行
109+
jmp Excute ; 执行完继续跳转到 Excute
110+
CMP2: ;判断用户选择的第二个程序是否执行
111+
cmp word[y],ax ; 判断 y 是否为 0,0 代表执行过
112+
je CMP3 ; y 为 0 开始判断 z
113+
jmp Ex2 ; y 不为 0 开始执行用户选择的第二个程序
114+
Ex2: ;执行用户选择的第二个程序
115+
mov ax,word[y] ; 把 y 放到 ax
116+
mov word[cur],ax; ax 放到 cur
117+
mov ax,0 ; ax 置零
118+
mov word[y],ax ; ax 放到 y ,将 y 置零
119+
call Run ; 开始调用
120+
jmp Excute ; 执行完继续跳转到 Excute
121+
CMP3: ;判断用户选择的第三个程序是否执行
122+
cmp word[z],ax ; z 是否为 0
123+
je END ; 若 z 也为 0,则没有程序等待执行,结束本次调用
124+
jmp Ex3 ; 执行用户选择的第三个程序
125+
Ex3: ;执行用户选择的第三个程序
126+
mov ax,word[z] ; z 放到 ax
127+
mov word[cur],ax; ax 放到 cur
128+
mov ax,0 ; ax 置零
129+
mov word[z],ax ; z 置零,标记为执行过
130+
call Run ; 开始执行
131+
jmp Excute ; 执行完继续跳转到 Excute
132+
END: ;本次用户选择的程序已调用完
133+
call Clear ; 清屏
134+
jmp Main ; 回到主函数,相当于把控制权交给操作系统
135+
136+
Run: ; 执行当前用户程序
137+
mov bx,48 ; bx 置为 48
138+
mov ax,word[cur]; 把 cur 装载到 ax
139+
sub ax,bx ; 让 ax = ax-bx
140+
add ax,3 ; ax += 3 找到对应扇区号
141+
mov byte[p],al ; 把扇区号放到 p
142+
call ReadPro ; 装载程序到 7e00h
143+
call dword 7e00h; 调用程序
144+
ret
145+
146+
Clear: ; 清屏
147+
mov ax,0003H ; 设置清屏属性
148+
int 10H ; 功能号
149+
ret ; 返回
150+
151+
ReInit: ;对变量进行重新初始化
152+
mov ax,0 ; ax 置零
153+
mov word[x],ax ; x 置零
154+
mov word[y],ax ; y 置零
155+
mov word[z],ax ; z 置零
156+
mov word[i],ax ; i 置零
157+
mov word[cur],ax; cur 置零
158+
ret ; 返回
159+
160+
Data: ; 数据定义
161+
x dw 0 ; 声明 x ,x 代表用户输入第一个字符
162+
y dw 0 ; 声明 y , y 代表用户输入的第二个字符
163+
z dw 0 ; 声明 z , z 代表用户输入的第三个字符
164+
i dw 0 ; 声明 i , i 为用户已经输入的字符数
165+
cur dw 0 ; 声明 cur ,cur 为当前应该调用的程序
166+
p db 0
167+
168+
str1: ; 字符串1
169+
db "Chen-OS 1.0"
170+
Length1: equ ($-str1) ; 字符串 1 的长度
171+
str2: ; 字符串2
172+
db "(C) 2014 liaojch3"
173+
Length2: equ ($-str2) ; 字符串 2 的长度
174+
str3: ; 字符串3
175+
db " We have total 3 program: Program 1 program 2 Program 3"
176+
db 0ah,0dh ; 换行
177+
db 0ah,0dh
178+
db " Please choose one, two or three program with the order you want"
179+
db 0ah,0dh
180+
db " Then press the Enter"
181+
db 0ah,0dh
182+
db 0ah,0dh
183+
db " Notice that input with no space,like key in 12 or 231."
184+
db 0ah,0dh
185+
db " Then the os will excute it."
186+
db 0ah,0dh
187+
db 0ah,0dh
188+
db " Please Key in here:"
189+
Length3: equ ($-str3) ; 字符串 3 的长度
190+
times 1024-($-$$) db 0 ; 用0填充剩余部分
191+
; ($=当前地址、$$=当前节地址)
192+
;db 55h,0aah ; 启动扇区的结束标志
193+

Lab02/src/pro1.asm

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
;
2+
; 本文件为用户程序1的源码文件
3+
;
4+
org 7e00h ; 加载到0:7e00h处,并开始执行
5+
START:
6+
mov ax,cs ; 设置 DS和ES = CS
7+
mov ds,ax
8+
mov es,ax
9+
call Clear ; 清屏
10+
call DispStr ; 显示字符串
11+
call Keyin ; 接受用户任意输入
12+
retf ; 段间返回
13+
14+
DispStr: ; 显示字符串
15+
; 显示字符串 "Hello, Here is Program 1 ~"(开始)
16+
mov ah,13h ; 功能号
17+
mov al,1 ; 光标放到串尾
18+
mov bl,0ch ; 黑底红字
19+
mov bh,0 ; 第0页
20+
mov dh,05h ; 第5行
21+
mov dl,1ch ; 第28列
22+
mov bp,BootMsg ; BP=串地址
23+
mov cx,Length1 ; 串长为length1
24+
int 10h ; 调用10H号中断
25+
; 显示字符串(结束)
26+
; 显示字符串2 "Please Key in Esc to quit:"(开始)
27+
mov ah,13h ; 功能号
28+
mov al,1 ; 光标放到串尾
29+
mov bl,0dh ; 黑底品红字
30+
mov bh,0 ; 第0页
31+
mov dh,07h ; 第7行
32+
mov dl,1ch ; 第28列
33+
mov bp,Tips ; BP=串地址
34+
mov cx,Length2 ; 串长
35+
int 10h ; 调用10H号中断
36+
; 显示字符串2(结束)
37+
ret
38+
39+
Keyin: ; 读按键
40+
mov ah,0 ; 功能号
41+
int 16h ; 调用16H号中断
42+
ret ; 退出
43+
44+
Clear: ;清屏
45+
mov ax,0003H ; 清屏属性
46+
int 10H ; 调用中断
47+
ret ; 返回
48+
49+
BootMsg: ; 引导字符串
50+
db "Hello. Here is Program 1 ~ "
51+
Length1: equ ($-BootMsg) ; 欢迎字符串的长度
52+
Tips: ; 退出提示
53+
db "Press Key to quit..."
54+
Length2: equ ($-Tips) ; 提示的长度
55+
56+
times 512-($-$$) db 0 ; 用0填充扇区的剩余部分
57+
;db 55h,0aah ; 启动扇区的结束标志

Lab02/src/pro2.asm

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
;
2+
; 本文件为用户程序2的源码文件
3+
;
4+
org 7e00h ; 加载到 8000h处,并开始执行
5+
START:
6+
mov ax,cs ; 设置 DS和ES = CS
7+
mov ds,ax
8+
mov es,ax
9+
call Clear ; 清屏
10+
call DispStr ; 显示字符串
11+
call Keyin ; 等待用户输入任意键
12+
retf ; 无限循环
13+
14+
DispStr: ; 显示字符串
15+
; 显示字符串 "Haha. This is Program 2 ~"(开始)
16+
mov ah,13h ; 功能号
17+
mov al,1 ; 光标放到串尾
18+
mov bl,0ch ; 黑底红字
19+
mov bh,0 ; 第0页
20+
mov dh,05h ; 第5行
21+
mov dl,1ch ; 第28列
22+
mov bp,BootMsg ; BP=串地址
23+
mov cx,Length1 ; 串长为 Length1
24+
int 10h ; 调用10H号中断
25+
; 显示字符串(结束)
26+
; 显示字符串2 "Please Key in Esc to quit:"(开始)
27+
mov ah,13h ; 功能号
28+
mov al,1 ; 光标放到串尾
29+
mov bl,0dh ; 黑底品红字
30+
mov bh,0 ; 第0页
31+
mov dh,07h ; 第7行
32+
mov dl,1ch ; 第28列
33+
mov bp,Tips ; BP=串地址
34+
mov cx,Length2 ; 串长
35+
int 10h ; 调用10H号中断
36+
; 显示字符串2(结束)
37+
ret
38+
39+
Keyin: ; 读按键
40+
mov ah,0 ; 功能号
41+
int 16h ; 调用16H号中断
42+
ret ; 退出
43+
44+
Clear: ;清屏
45+
MOV AX,0003H
46+
INT 10H
47+
ret
48+
49+
BootMsg: ; 引导字符串
50+
db "Haha. This is Program 2 ~ "
51+
Length1: equ ($-BootMsg)
52+
Tips: ; 退出提示
53+
db "Press key to quit..."
54+
Length2: equ ($-Tips)
55+
56+
times 512-($-$$) db 0 ; 用0填充扇区的剩余部分
57+
;db 55h,0aah ; 启动扇区的结束标志
58+

0 commit comments

Comments
 (0)