匯編語言 之屏幕顯示輸出Hello World

ScottePerk 2022-01-07 17:13:16 阅读数:237

屏幕 hello world

 

使用的系統是manjaro linux。使用的工具是emu8086模擬器,是安裝在windows xp 系統裏面的(用wine也能運行),真實的運行環境是windows xp 虛擬機,語法格式為masm。因為本人已經很久沒用windows,而匯編的開發工具windows是比linux好的,特別是這個emu8086模擬器,非常的好用。但是目前來看emu8086是最好用的開發環境了,不光具有調試功能,而且集成了內存視圖,文件列錶(lst文件)等非常使用的工具,emu8086使用的是masm語法。其實本人更喜歡nasm的語法格式。

開始正題:

用匯編在顯示器上實現hello world其實也不難。下面給出代碼並解釋。下面給出masm格式的代碼實現,在後面會給出一份nasm格式的實現。其實都差不多。

 

 

 


; You may customize this and other start-up templates;
; The location of this template is c:\emu8086\inc\0_com_template.txt
mov ax,0xb800
mov ds,ax ;跳轉到顯存的比特置
mov [0x00],0x41 ;字符a
mov [0x01],0x04 ;顏色為紅色
mov [0x02],'s'
mov [0x03],0x14 ;隨便取色
mov [0x04],'s'
mov [0x05],0x24
mov [0x06],'e'
mov [0x07],0x34
mov [0x08],'m'
mov [0x09],0x44
mov [0x0a],'b'
mov [0x0b],0x54
mov [0x0c],'l'
mov [0x0d],0x64
mov [0x0e],'y'
mov [0x1f],0x74
mov [0x10],'.'
mov [0x11],0x84
db 510-(0x5f) dup(0)
db 0x55,0xaa

首先顯示文本是需要用到顯卡的顯存的,其實就是一塊存儲器,一般有幾M。對應內存地址的一段地址空間。這個地址空間的起點是0xb800。這個是固定的。別的設備也會站一段屬於自己的地址空間。mov ax,0xb800 mov ds,ax就是跳轉到顯存的起始比特置。

字符的結構由兩個byte組成。第一個byte錶示ascii錶對應的編號比如a對應的編號是0x41,也可以直接寫成'a'.

第二個byte錶示顏色,顏色的格式是krgb irgb。前4比特錶示背景色,後4比特錶示前景色。k錶示是否閃爍,1錶示閃爍,0錶示不閃爍。i錶示是否是灰度,1錶示是,0錶示否。

例如0x04,0對應0000,也就是krgb全是0,錶示背景色純黑不閃爍。4對應0100,錶示無灰度的紅也就是irgb中的r為1,其它都為0。

 

最後要說一些最後兩行。

先說說最後一行 db 0x55,0xaa。db錶示手動寫入內容。這裏手動寫入0x55和0xaa。這兩個兩個值是什麼意思?這段程序是要寫到主引導扇區的,主引導扇區規定最後兩個字節就是這兩個,最為結束的標記。主引導扇區有512個字節。而且主引導扇區需要填滿。所以512-2還有510個字節。需要减去我們寫的代碼占用的大小。我們怎麼知道我們寫的代碼在內存中的大小呢?可以通過lst文件查看。這個lst數據是可以通過工具生成的。emu8086自帶這個功能直接幫你生成。比特置在調試器的view->listing

EMU8086 GENERATED LISTING. MACHINE CODE <- SOURCE.
noname.bin_ -- emu8086 assembler version: 4.08
[ 2021-3-25 -- 18:31:09 ]
===================================================================================================
[LINE] LOC: MACHINE CODE SOURCE
===================================================================================================
[ 1] :
[ 2] : ; You may customize this and other start-up templates;
[ 3] : ; The location of this template is c:\emu8086\inc\0_com_template.txt
[ 4] :
[ 5] 0000: B8 00 B8 mov ax,0xb800
[ 6] 0003: 8E D8 mov ds,ax
[ 7] :
[ 8] 0005: C6 06 00 00 41 mov [0x00],0x41 ;字符A
[ 9] 000A: C6 06 01 00 04 mov [0x01],0x04 ;顏色為紅色
[ 10] :
[ 11] 000F: C6 06 02 00 73 mov [0x02],'s'
[ 12] 0014: C6 06 03 00 14 mov [0x03],0x14
[ 13] :
[ 14] 0019: C6 06 04 00 73 mov [0x04],'s'
[ 15] 001E: C6 06 05 00 24 mov [0x05],0x24
[ 16] :
[ 17] 0023: C6 06 06 00 65 mov [0x06],'e'
[ 18] 0028: C6 06 07 00 34 mov [0x07],0x34
[ 19] :
[ 20] 002D: C6 06 08 00 6D mov [0x08],'m'
[ 21] 0032: C6 06 09 00 44 mov [0x09],0x44
[ 22] :
[ 23] 0037: C6 06 0A 00 62 mov [0x0a],'b'
[ 24] 003C: C6 06 0B 00 54 mov [0x0b],0x54
[ 25] :
[ 26] 0041: C6 06 0C 00 6C mov [0x0c],'l'
[ 27] 0046: C6 06 0D 00 64 mov [0x0d],0x64
[ 28] :
[ 29] 004B: C6 06 0E 00 79 mov [0x0e],'y'
[ 30] 0050: C6 06 1F 00 74 mov [0x1f],0x74
[ 31] :
[ 32] 0055: C6 06 10 00 2E mov [0x10],'.'
[ 33] 005A: C6 06 11 00 84 mov [0x11],0x84
[ 34] :
[ 35] : ;times 510-($-$$) db 0
[ 36] 005F: 00 00 00 00 00 00 00 00 00 00 00 00 db 510-(0x5f) dup(0)
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00
[ 37] 01FE: 55 AA db 0x55,0xaa
[ 38] :
[ 39] :
[ 40] :
[ 41] :
[ 42] :
[ 43] :
[ 44] :
[ 45] :
[ 46] :
===================================================================================================
===================================================================================================

注意標號為36的地方,地址值為5f,也就是錶示我們前面顯示文字的代碼占用了5f個字節的地址空間,所以db 510-(0x5f) dup(0)這行代碼就非常好解釋了。dup錶示重複執行次數為510-(0x5f),十進制和二進制是可以混合寫的。dup(0) 錶示寫入的內容為0,就是將剩餘的空間全部填充為0.

下面給出nasm格式的代碼,其實差不多只是語法不太一樣。nasm可以指定值的單比特是byte還是word  1word=2byte.重複使用times而不是dup。

 

mov ax,0xb800
mov ds,ax
mov byte [0x00],0x41 ;字符A
mov byte [0x01],0x04 ;顏色為紅色
mov byte [0x02],'s'
mov byte [0x03],0x14
mov byte [0x04],'s'
mov byte [0x05],0x24
mov byte [0x06],'e'
mov byte [0x07],0x34
mov byte [0x08],'m'
mov byte [0x09],0x44
mov byte [0x0a],'b'
mov byte [0x0b],0x54
mov byte [0x0c],'l'
mov byte [0x0d],0x64
mov byte [0x0e],'y'
mov byte [0x1f],0x74
mov byte [0x10],'.'
mov byte [0x11],0x84
times 510-($-$$) db 0
;db 510-(0x5f) dup(0)
db 0x55,0xaa

每次都要計算非常的麻煩,更簡單的方法就是設置標志比特,在開始的地方設置start標志比特,名字可以隨便取。結束的地方設置另一個標志比特兩個相减。


start:
mov ax,0xb800
mov ds,ax
mov [0x00],0x41 ;字符A
mov [0x01],0x04 ;顏色為紅色
...
current:
db (current-start) dup(0)

還有跟簡單的方法就是使用提供的特殊符號來錶示

db ($-$$) dup(0) $錶示開始,$$錶示結束。

最後的顯示效果,是有不同顏色的喲!雖然顏色很醜。。。

 

 

 

版权声明:本文为[ScottePerk]所创,转载请带上原文链接,感谢。 https://gsmany.com/2022/01/202201071713162700.html