`
lobin
  • 浏览: 389145 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论
阅读更多

 

 

中断

BIOS中断

10H中断

13H功能号:Teletype模式下显示字符串

	.code16
	.global start
	.text
start:
	mov %cs, %ax
	mov %ax, %es

	mov $message, %bp
	mov $length, %cx

	mov $0x13, %ah
	mov $0x01, %al

	mov $0x00, %bh
	mov $0x1f, %bl

	mov $0x00, %dh
	mov $0x00, %dl

	int $0x10

endl:  
	mov $0x4c, %ah
	int $0x21

message:
	.ascii "Hello, This is AS!"
	.byte 0x0d, 0x0a

	length = . - message

 

13H中断:磁盘中断。直接磁盘服务(Direct Disk Service)

02H功能号:读扇区

 

读软盘的例子,有关软盘或磁盘相关的信息可参考另一篇文章:https://www.iteye.com/blog/lobin-673570

	kos_seg = 0x3000
	kos_offset = 0x0000
	kos_sector_begin = 0x01	# the begin sector number of kos
	kos_sector_size = 0x04
	
	.code16
	.global start
	.section .text

start:
	jmpl	$0x07c0, $entry

entry:
	mov	%cs, %ax
	mov	%ax, %ds

	# es, kos segment
	mov	$kos_seg, %dx 
	mov	%dx, %es


	mov	$0x02, %ah		# service 0x02. read sectors 

	mov	$0x00, %dl 		# drive 0x00 驱动器. 软盘:00H~7FH;硬盘:80H~0FFH 
	#mov	$0x80, %dl
	mov	$0x00, %dh		# head 0 磁头.

	mov	$0x00, %ch 		# track 柱面.
	mov	$kos_sector_begin, %cl	# sector 扇区 

	mov	$kos_offset, %bx 		# read sector data to es:bx
	mov	$kos_sector_size, %al	# specifics the number of sector to read
	int	$0x13

	jnc	read_sector_ok
	

read_sector_error:
	mov	$m_read_sector_error, %si 
p1:
	mov	(%si), %al 
	inc	%si 
	cmp	$0x00, %al
	je	endl

	mov	$0x0e, %ah
	mov	$0x0f, %bx
	int	$0x10
	jmp	p1

read_sector_ok:
	mov	$m_read_sector_ok, %si
p0:
	mov	(%si), %al 
	inc	%si
	cmp	$0x00, %al
	je	jmp_to

	mov	$0x0e, %ah
	mov	$0x0f, %bx
	int	$0x10
	jmp	p0


jmp_to:
	# jmp	3000h:0h
	#
	#.byte	0xea		# jmp far
	#.word	$kos_offset	# offset: kos_offset = 0x0000
	#.word	$kos_seg	# segment: kos_seg = 3000h


	# jmp	3000h:0h
	#
	#.byte	0xea			# jmp far
	#.word	$kos_offset, $kos_seg	# offset, segment. segment: kos_seg = 3000h, offset: kos_offset = 0x0000


	# jmp	3000h:0h
	jmpl	$kos_seg, $kos_offset
	
endl:
	hlt
	jmp endl

m_read_sector_ok:
	.ascii	"[ook] read sector"
	.byte	0x0d, 0x0a, 0x00

m_read_sector_error:
	.ascii	"[err] read sector"
	.byte	0x0d, 0x0a, 0x00

	# MBR扇区位于整个硬盘的第一个扇区.
	# 硬盘扇区为512字节,所以主引导程序大小仅能也只能512字节。
	.org 510

	# MBR结束标志
	.byte	0x55, 0xaa

主要代码:

	mov	$0x02, %ah		# service 0x02. read sectors 

	mov	$0x00, %dl 		# drive 0x00 驱动器. 软盘:00H~7FH;硬盘:80H~0FFH 
	#mov	$0x80, %dl
	mov	$0x00, %dh		# head 0 磁头.

	mov	$0x00, %ch 		# track 柱面.
	mov	$kos_sector_begin, %cl	# sector 扇区 

	mov	$kos_offset, %bx 	# read sector data to es:bx
	mov	$kos_sector_size, %al	# specifics the number of sector to read
	int	$0x13

 

 

21H中断:DOS中断

 

3DH功能号:打开文件

参数:

DS:DX指定文件路径(ASCIIZ字符串)

AL指定文件操作:0: 读, 1: 写, 3: 读/写

 

文件路径必须要以0作为结束符,例子:

filename	db '3.bmp', 0h

 

BMP图片格式

BMP图片是一种位图图片。

 

像素

 

想象一下,一张黑白照片或者图片:

这是一张单色位图

照片是黑白的,颜色只有黑和白两种颜色。不是灰度图片,灰度图片除了黑和白两种颜色,还有一种灰色第三种颜色。

 

为了表示黑和白这两种颜色,我们只需要使用1位(2=2的1次方)就是可以表示,比如用0表示白色,用1表示黑色。

 

假设一张照片或者图片有16种颜色:

这是一张16色位图

为了表示16中颜色,需要用到4位(16=2的4次方)来表示这16中颜色。

 

假设一张照片或者图片有256种颜色:

这是一张256色位图

为了表示256中颜色,需要用到8位(256=2的8次方)来表示这256中颜色。

 

假设一张照片或者图片有65536种颜色:

为了表示65536中颜色,需要用到16位(65536=2的16次方)来表示这65536中颜色。


假设一张照片或者图片有16777216种颜色:

这是一张24位位图

为了表示16777216中颜色,需要用到24位(16777216=2的24次方)来表示这16777216中颜色。

 

使用多少位来表示一个像素

从上面的分析可知,如果需要表示的颜色越多,就需要使用更多的位来表示这些颜色。所以在表示一个像素时,需要使用更多的位来表示一个像素。根据上面的分析,bmp图片有使用1位来表示一个像素,有使用4位来表示一个像素,有使用8位来表示一个像素,有使用16位来表示一个像素,有使用24位来表示一个像素。

 

使用1位来表示一个像素就是我们所说的单色位图。

 

使用4位来表示一个像素就是我们所说的16色位图。

 

使用8位来表示一个像素就是我们所说的256色位图。

 

使用16位来表示一个像素就是我们所说的16位位图,也就是高彩色。

 

使用24位来表示一个像素就是我们所说的24位位图,也就是真彩色。

 

BMP图片格式分析:以下面这张BMP图片为例

文件头

14字节(0-13)

42 4D 32 04 00 00 00 00  00 00 36 00 00 00 

 

0-1: 42 4D 

文件类型: BM

 

2-5: 32 04 00 00 

表示0x0432(1074)

文件大小, 以字节为单位。

 

6-7: 00 00  

保留字,必须为0

 

8-9: 00 00 

保留字,必须为0

 

10-13: 36 00 00 00 

表示0x36(54)

位图数据的起始位置,以相对于位图文件头的偏移量表示(从文件头开始到实际图像数据之间的字节偏移量),以字节为单位  

 

位图信息头

40字节(14-53)

                                           28 00

00 00 13 00 00 00 11 00  00 00 01 00 18 00 00 00

00 00 FC 03 00 00 00 00  00 00 00 00 00 00 00 00

00 00 00 00 00 00 

 

14-17: 28 00

00 00

位图信息头大小,以字节为单位。固定为0x28(40)

18-21:13 00 00 00

表示0x13(19)

位图的宽度,以像素为单位

 

22-25: 11 00  00 00

表示0x11(17)

位图的高度,以像素为单位

 

26-27:01 00

表示0x01(1)

级别(目标设备的级别),必须为1

 

28-29:18 00

表示0x18(24)

每个像素所需的位数,必须是1(双色), 4(16色), 8(256色), 16(高彩色)或24(真彩色)之一

 

30-33:00 00

00 00

压缩类型,必须是 0(不压缩),1(BI_RLE8压缩类型)或2(BI_RLE4压缩类型)之一 

 

34-37:FC 03 00 00

表示0x03FC(1020)

位图的大小,以字节为单位

 

38-41: 00 00  00 00

位图水平分辨率,每米像素数

 

42-45: 00 00 00 00

位图垂直分辨率,每米像素数

 

46-49: 00 00

00 00

位图实际使用的颜色表中的颜色数

 

50-53: 00 00 00 00 

位图显示过程中重要的颜色数

 

颜色表

 

位图数据

54-:

00 25  3F 64 9B C6 7A C0 FC 66

B4 F7 72 BD FB 79 C2 FC  81 C7 FD 7D C7 FF 7C CE

FF 73 C7 FF 78 C8 FF 81  CC FA 89 CD F2 7F C5 ED

6C BE F5 69 BC F9 6D BA  F3 68 B2 E8 68 B4 E5 00

00 00 00 18 34 59 93 BD  65 AD E9 56 A4 E7 69 B4

F2 6B B4 EE 71 B5 EA 67  AF E5 66 B6 F5 64 B7 F5

70 BE F3 84 CC FB 98 DC  FF 99 DF FF 81 D4 FF 73

C8 FF 6B B6 F4 63 AD E7  62 AE E2 00 00 00 00 21

3F 64 9F CC 6C B3 F2 68  B6 F9 7F C8 FF 68 AF E8

60 A2 D5 53 99 CE 39 86  C4 46 96 D3 59 A5 DA 67

AD DC 7A BB E2 81 C6 F1  73 C6 FE 68 BC FD 6C B9

F8 67 B0 EE 64 AF E7 00  00 00 00 1D 3B 63 9B CA

66 AB EA 69 B2 F6 6C B1  F0 32 72 AC 24 61 93 25

66 99 14 5C 98 24 6D AB  39 7F B5 4A 8A BA 5C 99

C1 6D AE DA 76 C4 FF 80  D1 FF 6D B8 FC 6A B4 F4

6A B6 F1 00 00 00 06 27  48 75 A6 D8 76 B3 F5 77

BA FF 69 A7 E7 1A 53 8B  27 5C 8E 56 8D C0 80 BE

FA 63 A4 E1 57 95 CB 60  98 C7 67 9B C4 5C 94 C3

4F 96 D5 55 A1 E9 63 AB  F3 64 AD F1 6C B6 F6 00

00 00 00 19 3C 6D 99 CE  71 A8 EB 77 B2 F8 64 9A

DB 19 49 83 4F 7A AB AA  D7 FF 7B AF EB 3B 72 AF

20 54 89 41 71 A1 64 8F  BA 56 87 B7 3D 7A BC 3C

80 CB 51 98 E2 59 A1 E9  66 B2 F4 00 00 00 00 12

38 63 88 C0 65 96 DC 3E  71 BA 1B 47 87 09 2F 69

55 76 A7 80 A1 D2 20 48  82 1F 4B 86 06 2D 64 0A

2F 61 5A 7B A8 62 89 BD  22 56 9C 24 62 B0 41 83

D2 4F 95 E2 5F AA EE 00  00 00 32 47 6D 54 75 AD

6A 94 DB 2B 58 A2 00 00  3F 00 07 40 15 2F 5E 2E

48 77 00 1D 55 3C 5E 99  34 53 8A 00 0E 41 0A 24

52 36 56 8B 1D 4B 92 14  4C 9D 39 78 C9 49 8D DA

5B A3 E9 00 00 00 2D 3F  68 86 A4 DF 54 7B C5 42

6A B5 0B 2D 6F 00 08 41  02 14 43 00 05 34 04 1D

55 05 22 5B 0A 24 5A 05  1C 4E 10 24 54 09 24 5C

03 2C 75 2B 5F B2 36 70  C2 46 85 D5 59 9E E7 00

00 00 1C 2F 5A 9C BB F8  41 68 B3 44 6C B7 24 44

86 02 1B 53 2B 3B 6A 24  34 62 24 3C 72 20 38 72

2C 41 78 27 3C 6F 32 43  74 37 4F 89 23 4B 96 25

58 AE 36 6D C2 49 86 D6  5B 9F E8 00 00 00 2D 42

6F 6F 92 D1 58 82 CD 4D  79 C6 1D 41 81 3A 56 8C

5D 70 9D 60 70 9E 56 6E  A4 5F 7A B2 68 80 B4 43

58 8B 31 45 75 39 54 8D  23 4D 9A 12 44 9C 32 67

BE 4F 87 D9 67 A6 F0 00  00 00 3A 53 7F 71 97 D7

31 62 AE 31 62 AE 46 6C  AC 64 83 B8 71 86 B3 5A

6D 98 50 6B 9E 2C 4A 81  28 43 76 22 39 6B 12 28

58 00 18 53 00 18 66 07  3C 93 37 69 C1 57 8D E0

73 B0 FA 00 00 00 33 50  7D 7A A5 E4 04 38 84 00

24 70 2E 59 98 34 54 89  43 5B 85 2E 46 70 09 26

59 00 17 4C 00 12 45 00  0E 40 06 20 4F 0C 2E 69

00 2F 7D 13 48 A2 4F 81  D9 69 9D F0 7A B4 FF 00

00 00 21 3D 66 53 7D B8  4D 82 CB 0A 42 8F 00 2B

6E 00 25 5E 00 15 43 00  13 40 00 19 4E 00 1D 54

0E 2F 61 1C 40 6E 37 5E  8B 3B 68 A1 30 68 B7 53

8D E6 69 A0 F5 77 AF FE  7A B8 FF 00 00 00 2E 42

65 54 76 AB 69 9B E3 46  81 D1 00 37 82 00 23 67

0E 3E 78 07 2E 65 25 42  7F 39 57 92 50 79 AA 65

99 C2 70 AD D5 73 B3 E9  73 B0 FF 71 B0 FF 76 B8

FF 78 BA FF 79 BD FE 00  00 00 24 38 57 5A 79 AC

73 A5 ED 5C 9C F0 2C 77  C6 2B 74 BE 51 8D CF 4F

7D BD 6C 8E D0 7B 9C DB  84 B3 E6 88 C4 EE 86 CD

F3 80 C9 FB 7F BF FF 7D  BD FF 76 BA FF 76 BD FF

77 BE FD 00 00 00 14 29  49 55 77 AC 6A 9E EA 5B

9E F3 4E 9C EF 5A A7 F6  74 B6 FD 73 A7 ED 81 A9

F1 8B B2 F6 8D C1 F6 8C  CB F7 84 CD F3 7D C7 F7

7F BC FF 7D B9 FF 77 BC  FF 76 BE FE 75 BC FB 00

00 00 

 

显示BMP图片的代码

	_width = 20
	_height = 17

	_color_table_size = 256
	_color_item_size = 4

	_pixel_data_size = 340

_data segment
	color_table	db				 000h,000h, 000h,000h,000h,000h,080h,000h,000h,080h
			db 000h,000h,000h,080h,080h,000h,080h,000h, 000h,000h,080h,000h,080h,000h,080h,080h
			db 000h,000h,0C0h,0C0h,0C0h,000h,0C0h,0DCh, 0C0h,000h,0F0h,0CAh,0A6h,000h,000h,020h
			db 040h,000h,000h,020h,060h,000h,000h,020h, 080h,000h,000h,020h,0A0h,000h,000h,020h
			db 0C0h,000h,000h,020h,0E0h,000h,000h,040h, 000h,000h,000h,040h,020h,000h,000h,040h
			db 040h,000h,000h,040h,060h,000h,000h,040h, 080h,000h,000h,040h,0A0h,000h,000h,040h
			db 0C0h,000h,000h,040h,0E0h,000h,000h,060h, 000h,000h,000h,060h,020h,000h,000h,060h
			db 040h,000h,000h,060h,060h,000h,000h,060h, 080h,000h,000h,060h,0A0h,000h,000h,060h
			db 0C0h,000h,000h,060h,0E0h,000h,000h,080h, 000h,000h,000h,080h,020h,000h,000h,080h
			db 040h,000h,000h,080h,060h,000h,000h,080h, 080h,000h,000h,080h,0A0h,000h,000h,080h
			db 0C0h,000h,000h,080h,0E0h,000h,000h,0A0h, 000h,000h,000h,0A0h,020h,000h,000h,0A0h
			db 040h,000h,000h,0A0h,060h,000h,000h,0A0h, 080h,000h,000h,0A0h,0A0h,000h,000h,0A0h
			db 0C0h,000h,000h,0A0h,0E0h,000h,000h,0C0h, 000h,000h,000h,0C0h,020h,000h,000h,0C0h
			db 040h,000h,000h,0C0h,060h,000h,000h,0C0h, 080h,000h,000h,0C0h,0A0h,000h,000h,0C0h
			db 0C0h,000h,000h,0C0h,0E0h,000h,000h,0E0h, 000h,000h,000h,0E0h,020h,000h,000h,0E0h
			db 040h,000h,000h,0E0h,060h,000h,000h,0E0h, 080h,000h,000h,0E0h,0A0h,000h,000h,0E0h
			db 0C0h,000h,000h,0E0h,0E0h,000h,040h,000h, 000h,000h,040h,000h,020h,000h,040h,000h
			db 040h,000h,040h,000h,060h,000h,040h,000h, 080h,000h,040h,000h,0A0h,000h,040h,000h
			db 0C0h,000h,040h,000h,0E0h,000h,040h,020h, 000h,000h,040h,020h,020h,000h,040h,020h
			db 040h,000h,040h,020h,060h,000h,040h,020h, 080h,000h,040h,020h,0A0h,000h,040h,020h
			db 0C0h,000h,040h,020h,0E0h,000h,040h,040h, 000h,000h,040h,040h,020h,000h,040h,040h
			db 040h,000h,040h,040h,060h,000h,040h,040h, 080h,000h,040h,040h,0A0h,000h,040h,040h
			db 0C0h,000h,040h,040h,0E0h,000h,040h,060h, 000h,000h,040h,060h,020h,000h,040h,060h
			db 040h,000h,040h,060h,060h,000h,040h,060h, 080h,000h,040h,060h,0A0h,000h,040h,060h
			db 0C0h,000h,040h,060h,0E0h,000h,040h,080h, 000h,000h,040h,080h,020h,000h,040h,080h
			db 040h,000h,040h,080h,060h,000h,040h,080h, 080h,000h,040h,080h,0A0h,000h,040h,080h
			db 0C0h,000h,040h,080h,0E0h,000h,040h,0A0h, 000h,000h,040h,0A0h,020h,000h,040h,0A0h
			db 040h,000h,040h,0A0h,060h,000h,040h,0A0h, 080h,000h,040h,0A0h,0A0h,000h,040h,0A0h
			db 0C0h,000h,040h,0A0h,0E0h,000h,040h,0C0h, 000h,000h,040h,0C0h,020h,000h,040h,0C0h
			db 040h,000h,040h,0C0h,060h,000h,040h,0C0h, 080h,000h,040h,0C0h,0A0h,000h,040h,0C0h
			db 0C0h,000h,040h,0C0h,0E0h,000h,040h,0E0h, 000h,000h,040h,0E0h,020h,000h,040h,0E0h
			db 040h,000h,040h,0E0h,060h,000h,040h,0E0h, 080h,000h,040h,0E0h,0A0h,000h,040h,0E0h
			db 0C0h,000h,040h,0E0h,0E0h,000h,080h,000h, 000h,000h,080h,000h,020h,000h,080h,000h
			db 040h,000h,080h,000h,060h,000h,080h,000h, 080h,000h,080h,000h,0A0h,000h,080h,000h
			db 0C0h,000h,080h,000h,0E0h,000h,080h,020h, 000h,000h,080h,020h,020h,000h,080h,020h
			db 040h,000h,080h,020h,060h,000h,080h,020h, 080h,000h,080h,020h,0A0h,000h,080h,020h
			db 0C0h,000h,080h,020h,0E0h,000h,080h,040h, 000h,000h,080h,040h,020h,000h,080h,040h
			db 040h,000h,080h,040h,060h,000h,080h,040h, 080h,000h,080h,040h,0A0h,000h,080h,040h
			db 0C0h,000h,080h,040h,0E0h,000h,080h,060h, 000h,000h,080h,060h,020h,000h,080h,060h
			db 040h,000h,080h,060h,060h,000h,080h,060h, 080h,000h,080h,060h,0A0h,000h,080h,060h
			db 0C0h,000h,080h,060h,0E0h,000h,080h,080h, 000h,000h,080h,080h,020h,000h,080h,080h
			db 040h,000h,080h,080h,060h,000h,080h,080h, 080h,000h,080h,080h,0A0h,000h,080h,080h
			db 0C0h,000h,080h,080h,0E0h,000h,080h,0A0h, 000h,000h,080h,0A0h,020h,000h,080h,0A0h
			db 040h,000h,080h,0A0h,060h,000h,080h,0A0h, 080h,000h,080h,0A0h,0A0h,000h,080h,0A0h
			db 0C0h,000h,080h,0A0h,0E0h,000h,080h,0C0h, 000h,000h,080h,0C0h,020h,000h,080h,0C0h
			db 040h,000h,080h,0C0h,060h,000h,080h,0C0h, 080h,000h,080h,0C0h,0A0h,000h,080h,0C0h
			db 0C0h,000h,080h,0C0h,0E0h,000h,080h,0E0h, 000h,000h,080h,0E0h,020h,000h,080h,0E0h
			db 040h,000h,080h,0E0h,060h,000h,080h,0E0h, 080h,000h,080h,0E0h,0A0h,000h,080h,0E0h
			db 0C0h,000h,080h,0E0h,0E0h,000h,0C0h,000h, 000h,000h,0C0h,000h,020h,000h,0C0h,000h
			db 040h,000h,0C0h,000h,060h,000h,0C0h,000h, 080h,000h,0C0h,000h,0A0h,000h,0C0h,000h
			db 0C0h,000h,0C0h,000h,0E0h,000h,0C0h,020h, 000h,000h,0C0h,020h,020h,000h,0C0h,020h
			db 040h,000h,0C0h,020h,060h,000h,0C0h,020h, 080h,000h,0C0h,020h,0A0h,000h,0C0h,020h
			db 0C0h,000h,0C0h,020h,0E0h,000h,0C0h,040h, 000h,000h,0C0h,040h,020h,000h,0C0h,040h
			db 040h,000h,0C0h,040h,060h,000h,0C0h,040h, 080h,000h,0C0h,040h,0A0h,000h,0C0h,040h
			db 0C0h,000h,0C0h,040h,0E0h,000h,0C0h,060h, 000h,000h,0C0h,060h,020h,000h,0C0h,060h
			db 040h,000h,0C0h,060h,060h,000h,0C0h,060h, 080h,000h,0C0h,060h,0A0h,000h,0C0h,060h
			db 0C0h,000h,0C0h,060h,0E0h,000h,0C0h,080h, 000h,000h,0C0h,080h,020h,000h,0C0h,080h
			db 040h,000h,0C0h,080h,060h,000h,0C0h,080h, 080h,000h,0C0h,080h,0A0h,000h,0C0h,080h
			db 0C0h,000h,0C0h,080h,0E0h,000h,0C0h,0A0h, 000h,000h,0C0h,0A0h,020h,000h,0C0h,0A0h
			db 040h,000h,0C0h,0A0h,060h,000h,0C0h,0A0h, 080h,000h,0C0h,0A0h,0A0h,000h,0C0h,0A0h
			db 0C0h,000h,0C0h,0A0h,0E0h,000h,0C0h,0C0h, 000h,000h,0C0h,0C0h,020h,000h,0C0h,0C0h
			db 040h,000h,0C0h,0C0h,060h,000h,0C0h,0C0h, 080h,000h,0C0h,0C0h,0A0h,000h,0F0h,0FBh
			db 0FFh,000h,0A4h,0A0h,0A0h,000h,080h,080h, 080h,000h,000h,000h,0FFh,000h,000h,0FFh
			db 000h,000h,000h,0FFh,0FFh,000h,0FFh,000h, 000h,000h,0FFh,000h,0FFh,000h,0FFh,0FFh
			db 000h,000h,0FFh,0FFh,0FFh,000h
	pixel_data	db				 00Ah,0AEh, 0B7h,0B7h,0B7h,0B7h,0B7h,0B7h,0B7h,0B7h
			db 0B7h,0B7h,0B7h,0B7h,0B7h,0B7h,0B7h,0B7h, 0B7h,000h,00Ah,06Eh,0AFh,06Fh,0B7h,0B7h
			db 0B7h,0AFh,0B7h,0B7h,0B7h,0B7h,0BFh,0BFh, 0BFh,0B7h,0B7h,0AFh,0AFh,000h,00Ah,0AEh
			db 0B7h,0B7h,0B7h,0AFh,0AFh,06Eh,066h,06Fh, 06Fh,0AFh,0B7h,0B7h,0B7h,0B7h,0B7h,0B7h
			db 0AFh,000h,00Ah,0AEh,0AFh,0B7h,0B7h,065h, 05Dh,05Dh,01Dh,05Dh,066h,066h,06Eh,0AFh
			db 0B7h,0BFh,0B7h,0B7h,0B7h,000h,00Ah,0AFh, 0B7h,0B7h,0AFh,01Ch,05Ch,066h,0B7h,0AFh
			db 06Eh,0AEh,0AEh,06Eh,06Fh,06Fh,0AFh,0AFh, 0B7h,000h,00Ah,0AEh,0AFh,0B7h,0AFh,014h
			db 065h,0BFh,0AFh,065h,05Ch,065h,0A6h,066h, 066h,066h,06Fh,06Fh,0B7h,000h,00Ah,0A6h
			db 0AFh,066h,014h,00Bh,065h,0AFh,054h,014h, 00Bh,00Bh,065h,0A6h,05Dh,05Eh,067h,06Fh
			db 06Fh,000h,053h,065h,0AFh,05Dh,00Ah,00Ah, 00Bh,054h,00Bh,05Dh,05Ch,00Ah,00Bh,05Ch
			db 015h,015h,066h,067h,06Fh,000h,053h,0AFh, 066h,05Eh,00Bh,00Ah,00Ah,00Ah,00Bh,00Bh
			db 00Bh,00Ah,00Bh,00Bh,00Ch,05Eh,066h,067h, 06Fh,000h,00Bh,0B7h,05Eh,05Eh,054h,00Bh
			db 053h,053h,054h,054h,054h,053h,054h,054h, 055h,05Dh,05Eh,067h,06Fh,000h,053h,0AFh
			db 066h,066h,014h,05Ch,065h,0A5h,05Dh,066h, 0A6h,05Ch,054h,05Ch,055h,015h,05Eh,067h
			db 0AFh,000h,05Ch,0AFh,05Dh,05Dh,05Dh,0A6h, 0A6h,05Dh,05Dh,054h,054h,053h,00Bh,00Bh
			db 00Bh,015h,05Eh,067h,0B7h,000h,05Ch,0AFh, 014h,00Ch,05Dh,05Ch,05Ch,054h,00Bh,00Ah
			db 00Ah,00Ah,00Ah,00Bh,00Ch,015h,067h,0AFh, 0B7h,000h,053h,066h,066h,014h,00Bh,00Bh
			db 00Ah,00Ah,00Ah,00Bh,00Bh,013h,05Ch,05Dh, 05Eh,067h,0AFh,0AFh,0B7h,000h,053h,065h
			db 0AFh,067h,014h,00Bh,014h,00Bh,054h,05Dh, 065h,0AEh,0AFh,0B7h,0B7h,0B7h,0B7h,0B7h
			db 0B7h,000h,053h,065h,0AFh,06Fh,066h,066h, 066h,066h,0A7h,0AFh,0B7h,0B7h,0B7h,0B7h
			db 0B7h,0B7h,0B7h,0B7h,0B7h,000h,00Ah,065h, 0AFh,06Fh,06Fh,06Fh,0B7h,0AFh,0AFh,0B7h
			db 0B7h,0B7h,0B7h,0B7h,0B7h,0B7h,0B7h,0B7h, 0B7h,000h
_data ends

_text segment
_set_palette proc near
	xor	cx, cx
	lea	si, color_table
_loop:
	cmp	cx, _color_table_size
	jge	_out

	mov	dx, 3c8h
	mov	al, cl
	out	dx, al

	mov	dx, 3c9h

	mov	al, ds:[si + 2]
	shr	al, 1
	shr	al, 1
	out	dx, al

	mov	al, ds:[si + 1]
	shr	al, 1
	shr	al, 1
	out	dx, al

	mov	al, ds:[si]
	shr	al, 1
	shr	al, 1
	out	dx, al

	add	si, _color_item_size

	inc	cx
	jmp	_loop

_out:
	ret
_set_palette endp
	
_set_mode_13h proc near
	; 通过BIOS设置VGA模式 
	mov	ah, 00h 
	mov	al, 13h		; 13h:640×480 256色
	int	10h

	ret
_set_mode_13h endp

_set_mode_03h proc near
	; 通过BIOS设置VGA模式 
	mov	ah, 00h 
	mov	al, 03h
	int	10h 

	ret
_set_mode_03h endp

_draw proc near
	mov	bx, 0a000h
        mov	es, bx

	lea	bx, pixel_data
	add	bx, _pixel_data_size

	xor	cx, cx
_loop1:
	cmp	cx, _height
	jge	_out1

	mov	ax, 320
	mul	cx
	mov	di, ax
	
	sub	bx, _width

	xor	si, si
_loop2:
	cmp	si, _width
	jge	_out2

	mov	al, [bx][si]
	mov	es:[di], al 

	inc	si
	inc	di
	jmp	_loop2

_out2:
	inc	cx
	jmp	_loop1

_out1:	
	ret
_draw endp
	
_start:	
	mov	ax, _data
	mov	ds, ax

	call	_set_mode_13h
	call	_set_palette
	call	_draw

	; wait key for input
	mov	ah, 00h
	int	16h

	call _set_mode_03h

	mov ah, 4ch
	int 21h
_text ends
	end _start

 

 

显卡

 

显示器

 

磁盘

 

软盘

 

硬盘

硬盘操作读写有两种寻址方式

寻址方式

CHS方式,LBA方式

可以通过13H BIOS中断和端口两种方式操作读写硬盘,其中13H BIOS中断只提供了CHS方式读写硬盘,端口方式即可以使用CHS方式读写硬盘,也可以使用LBA方式读写硬盘。

 

通过13H BIOS中断操作读写硬盘可参考中断部分内容。这里只介绍通过端口方式操作读写硬盘。

 

CHS方式

CHS寻址方式通过IO端口读硬盘的例子:

 

LBA方式

包括LBA28和LBA48.LBA28使用28位来表示逻辑块地址,LBA48采用48位来表示逻辑块地址。

 

LBA28寻址方式通过IO端口读硬盘的例子:

	kos_seg = 0x9000
	kos_offset = 0x0000

	kos_sector_begin = 0x01		# the begin sector number of kos on lba(logical block address) mode
	kos_sector_size = 0x04
	
	.code16
	.global start
	.section .text

start:
	jmpl	$0x07c0, $entry

entry:
	mov	%cs, %ax
	mov	%ax, %ds

	# es, kos segment
	mov	$kos_seg, %dx 
	mov	%dx, %es
	# di, kos offset
	mov	$kos_offset, %di

	
	# write the number of sector to read to port 0x1f2
	mov	$kos_sector_size, %al
	mov	$0x1f2, %dx	# 扇区计数器寄存器, 设置需要读写的扇区数
	out	%al, %dx

	# write the logical block address(lba) to ports 0x1f3, 0x1f4, 0x1f5, 0x1f6
	# for lba28
	# write bit0-7 of logical block address(lba) to port 0x1f3
	# write bit8-15 of logical block address(lba) to port 0x1f4
	# write bit16-23 of logical block address(lba) to port 0x1f5
	# write bit24-27 of logical block address(lba) to bit0-3 of port 0x1f6

	mov	$kos_sector_begin, %eax

	# write bit0-7 of logical block address(lba) to port 0x1f3
	mov	$0x1f3, %dx
	out	%al, %dx

	# write bit8-15 of logical block address(lba) to port 0x1f4
	shr	$8, %eax
	mov	$0x1f4, %dx
	out	%al, %dx

	# write bit16-23 of logical block address(lba) to port 0x1f5
	shr	$8, %eax
	mov	$0x1f5, %dx
	out	%al, %dx

	# write bit24-27 of logical block address(lba) to bit0-3 of port 0x1f6
	shr	$8, %eax
	btr	$4, %eax	# set drive(bit4), 0: master(primary), this usually to be hard disk; 1: slave(secondary), this usually to be compact disc(CD)
	bts	$6, %eax	# set bit6, 0: chs; 1: lba
	mov	$0x1f6, %dx
	out	%al, %dx

	#shr	$8, %eax
	#and	$0x0f, %al
	#or	$0xe0, %al
	#mov	$0x1f6, %dx
	#out	%al, %dx
	

	# write command to port 0x1f7, command register
	mov	$0x20, %al
	mov	$0x1f7, %dx
	out	%al, %dx

check_status:
	# read status from port 0x1f7, status register
	in	%dx, %al

	# bit 0: err bit. if bit 0 is 1, an error occurs
	bt	$0, %eax
	jc	read_sector_error

	# bit 7: busy bit. if bit 0 is 1, the disk is busy
	bt	$7, %eax
	jc	check_status


	mov	$kos_sector_size, %ax
	mov	$256, %dx
	mul	%dx

	mov	%ax, %cx

	mov	$0x1f0, %dx
read_data:
	# read data from port 0x1f0, data register. read 2byte/times
	in	%dx, %ax
	mov	%ax, %es:(%di)
	add	$2, %di
	loop	read_data


	jmp	read_sector_ok
	

read_sector_error:
	mov	$m_read_sector_error, %si 
p1:
	mov	(%si), %al 
	inc	%si 
	cmp	$0x00, %al
	je	endl

	mov	$0x0e, %ah
	mov	$0x0f, %bx
	int	$0x10
	jmp	p1

read_sector_ok:
	mov	$m_read_sector_ok, %si
p0:
	mov	(%si), %al 
	inc	%si
	cmp	$0x00, %al
	je	jmp_to

	mov	$0x0e, %ah
	mov	$0x0f, %bx
	int	$0x10
	jmp	p0


jmp_to:
	# jmp	3000h:0h
	#
	#.byte	0xea		# jmp far
	#.word	$kos_offset	# offset: kos_offset = 0x0000
	#.word	$kos_seg	# segment: kos_seg = 3000h


	# jmp	3000h:0h
	#
	#.byte	0xea			# jmp far
	#.word	$kos_offset, $kos_seg	# offset, segment. segment: kos_seg = 3000h, offset: kos_offset = 0x0000


	# jmp	3000h:0h
	jmpl	$kos_seg, $kos_offset
	
endl:
	hlt
	jmp endl

m_read_sector_ok:
	.ascii	"[ook] read sector"
	.byte	0x0d, 0x0a, 0x00

m_read_sector_error:
	.ascii	"[err] read sector"
	.byte	0x0d, 0x0a, 0x00

	# MBR扇区位于整个硬盘的第一个扇区.
	# 硬盘扇区为512字节,所以主引导程序大小仅能也只能512字节。
	.org 510

	# MBR结束标志
	.byte	0x55, 0xaa

 

 

鼠标

 

网卡

 

 

 

主引导程序

AS开发主引导程序

	.code16
	.global _start
	.text
_start:
	jmpl	$0x07c0, $start2
start2:
	mov %cs, %ax
	mov %ax, %ds

	mov $message, %si

print_message:
	mov (%si), %al
	cmp $'$', %al
	je  endl

	mov $0x0e, %ah
	mov $0x0f, %bx
	int $0x10

	inc %si
	jmp print_message

endl:  
	hlt  
	jmp endl 

message:
	.ascii "Hello, AS!"
	.byte 0x0d, 0x0a, '$'

	.org 510
	
mbr_flag:
	.word 0xaa55

Windows下AS编写主引导程序到系统内核

	#.equ b_seg, 0x07c0
	b_seg = 0x07c0
	cs_sector = 0x0008
	ds_sector = 0x0010
	.code16
	#.arch i386,nojumps

	.text

start:
	jmpl	$b_seg, $entry

entry:
	mov %cs, %ax
	mov %ax, %ds
	
	call _bprint_real_mode

	# set _gdtr:base
	mov $_gdtr, %si
	movl $gdt_base + 0x7c00, 2(%si)

	#cli
 
	# load _gdtr(6 bytes) into register gdtr
	lgdt _gdtr

	# enable a20
	mov $0xdd, %al
	out %al, $0x64

	mov %cr0, %eax
	or $0x01, %eax
	mov %eax, %cr0

	jmp do

	# do far jmp to enter protected mode

	# jmp cs_sector:_pinit
	#.byte	0xea				# jmp far
_this:
	.word	_pinit				# offset: _pinit
	.word	cs_sector			# segment: cs_sector

do:
	nop
	nop
	#addw	$0x7c00, ($_this)
	#jmpl	*%cs:($_this)
	nop
	nop
	jmpl $cs_sector, $_pinit + 0x7c00

# do init for protect mode
	.code32
_pinit:
	xorl %eax, %eax
	xorl %eax, %eax
	xorl %eax, %eax
	xorl %eax, %eax
	xorl %eax, %eax

	# reset segment register, here should be segment sector.
	movw $ds_sector, %cx
	movw %cx, %ds

	
	# print 

	# via video memory: memory i/o mapped
	# memory 0xb8000

	# here, write to ds:[0b8000h], ds is segment sector: ds_sector, it's 
	# base address is 00000000h, segment offset: 0b8000h, so finally, write 
	# to memory address 0xb8000.
	movl $0x000B8000, %edi

	mov $'A', %al
	mov $0b11010010, %ah	#
	movw %ax, (%edi)

_exit:	
	hlt
	jmp _exit

	.code16
_bprint_real_mode:
	mov $_real_mode, %si

_loop:	
	mov (%si), %al
	cmp $'$', %al
	je _ret

	mov $0x0e, %ah
	mov $0x0f, %bx
	int $0x10

	inc %si
	jmp _loop

_ret:
	ret

_gdt:
_gdt_0:
	# gdt 0
	# base address of segment: 00000000h, limit of segment: 00h(0000 0000 0000 0000 0000b)
	.word 0x0000		# the bit 0-15 of limit of segment.
	.word 0x0000		# the bit 0-15 of base address of segment.
	.byte 0x00			# the bit 16-23 of base address of segment. 
	.byte 0x00			# 0-3: type, 4: s, 5-6: dpl, 7: p
	.byte 0x00			# 0-3: the bit 16-19 of limit of segment, 4: avl, 5: l, 6: d/b, 7: g
	.byte 0x00			# the bit 24-31 of base address of segment.
_gdt_1:
	# gdt 1
	# base address of segment: 00000000h, limit of segment: 0fffffh(1111 1111 1111 1111 1111b)
	.word 0xffff		# the bit 0-15 of limit of segment.
	.word 0x0000		# the bit 0-15 of base address of segment.
	.byte 0x00			# the bit 16-23 of base address of segment. 
	.byte 0b10011010		# 0-3: type, 4: s, 5-6: dpl, 7: p
	.byte 0b11001111		# 0-3: the bit 16-19 of limit of segment, 4: avl, 5: l, 6: d/b, 7: g
	.byte 0x00			# the bit 24-31 of base address of segment.
_gdt_2:
	# gdt 2
	# base address of segment: 00000000h, limit of segment: 0fffffh(1111 1111 1111 1111 1111b)
	.word 0xffff		# the bit 0-15 of limit of segment.
	.word 0x0000		# the bit 0-15 of base address of segment.
	.byte 0x00			# the bit 16-23 of base address of segment. 
	.byte 0b10010010		# 0-3: type, 4: s, 5-6: dpl, 7: p
	.byte 0b11001111		# 0-3: the bit 16-19 of limit of segment, 4: avl, 5: l, 6: d/b, 7: g
	.byte 0x00			# the bit 24-31 of base address of segment.

	gdt_size = . - _gdt		# the size of _gdt
	gdt_limit = gdt_size - 1	# the limit of gdt
	gdt_base = _gdt
_gdtr:
	.byte 0x17, 0x00
	.word 0x0000, 0x0000

	#cs_sector = _gdt_1 - _gdt_0	# _gdt_1 - _gdt_0 = 8(0000 0000 0000 1000b), in this case, bit 3-15: index(1), bit 2: ti(0), bit 0-1: rpl(0)
	
	#ds_sector = _gdt_2 - _gdt_0	# _gdt_2 - _gdt_0 = 16(0000 0000 0001 0000b), in this case, bit 3-15: index(2), bit 2: ti(0), bit 0-1: rpl(0)

_real_mode:
	.ascii "hello, real-address mode!"
	.byte 0x0d, 0x0a, '$'

	
	.org 510

	.byte 85, 170	# 0x55, 0xaa


Bochs

这部分参考另一篇文章:https://www.iteye.com/blog/lobin-2041659

 

QEMU

这部分参考另一篇文章:https://www.iteye.com/blog/lobin-673570

 

 

 

  • 大小: 1 KB
  • 大小: 803 Bytes
  • 大小: 2.3 KB
  • 大小: 5 KB
  • 大小: 31.4 KB
  • 大小: 28.1 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics