在显示中文这一章,会使用python来自动化提取汉字,没有下载python的可以去下载:Download Python | Python.org我这里用的是python 3.6.5,最好别下载最新版本。
之后,去网上百度下载HZK16的字库。
创建一个叫AsmCNFont的文件夹。
把HZK16字库给放进去。
新建一个叫FontExtraction.py的文件,代码如下:
#提取字模
import binascii
KEYS = [0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01]
rect_list = [] * 16
for i in range(16):
rect_list.append([] * 16)
f=open('font.txt')
text=f.read()
f.close()
gb2312 = text.encode('gb2312')
hex_str = binascii.b2a_hex(gb2312)
result = str(hex_str, encoding='utf-8')
area = eval('0x' + result[:2]) - 0xA0
index = eval('0x' + result[2:]) - 0xA0
of2set = (94 * (area-1) + (index-1)) * 32
font_rect = None
with open("HZK16", "rb") as f:
f.seek(of2set)
font_rect = f.read(32)
for k in range(len(font_rect) // 2):
row_list = rect_list[k]
for j in range(2):
for i in range(8):
asc = font_rect[k * 2 + j]
flag = asc & KEYS[i]
row_list.append(flag)
f2=open('fonttemp.txt','w')
a=''
for row in rect_list:
for i in row:
if i:
a=a+'1'
else:
a=a+'0'
a=a+'\n'
f2.write(a)
f2.close()
#转换为asm。
d=0
e=0
f3=open('pyfont.txt','w')
f3.write("mov ah,0\nmov al,12h\nint 10h\nmov si,0\nmov bp,0\nmov bl,0x0f\n")
a=''
global g
g=d
c=e+1
f4 = open("fonttemp.txt","r")
for i in range(16):
a = f4.readline()
if '\n' in a:
a=a[:-1:]
if '1' in a:
b=1
d=g
for i in range(16):
if a[b-1:b:]=='1':
f3.write('mov al,bl\nmov cx,si\nadd cx,'+str(d)+'\nmov dx,bp\nadd dx,'+str(c)+'\nmov ah,0ch\nint 10h\n')
d+=1
b+=1
else:
b+=1
d+=1
else:
pass
c+=1
f3.write("jmp $")
f3.close()
f4.close()
注:其中HZK16中提取点阵的代码来源于:Python print 玩转点阵字 - 知乎 (zhihu.com)
新建一个叫font.txt的文件,里面输入一个你想显示的汉字,我在这里用”哈“来举例。
如果提示UnicodeDecodeError: 'gbk' codec can't decode byte 0xXX in position 2: incomplete multibyte sequence的话,检查font.txt文件编码是否为ANSI。
把生成出来的pyfont.txt的内容编译后运行。
启动DOS BOX调试。
成功了!
这篇文章就到这了。
我们在开发操作系统(6)再见。