1. 程式人生 > 實用技巧 >組合語言實驗2 彙編源程式編寫與彙編、除錯

組合語言實驗2 彙編源程式編寫與彙編、除錯

四、實驗結論

1. 實驗任務1

給出ex1.asm原始碼

; ex1.asm
assume cs:code
code segment
    mov ax, 0b810h
    mov ds, ax

    mov byte ptr ds:[0], 1
    mov byte ptr ds:[1], 1
    mov byte ptr ds:[2], 2
    mov byte ptr ds:[3], 2
    mov byte ptr ds:[4], 3
    mov byte ptr ds:[5], 3
    mov byte ptr ds:[6], 4
    mov byte ptr ds:
[7], 4 mov ah, 4ch int 21h code ends end

給出使用masm、link工具彙編、連結的命令列及執行結果截圖

給出使用debug除錯的截圖。結合可執行檔案載入後暫存器CX的值,使用u命令精確反彙編截圖

檢視PSP的命令及截圖

使用g命令執行到line16退出執行之前,操作截圖

2.實驗任務2

給出ex2.asm原始碼

; ex2.asm
assume cs:code
code segment
    mov ax, 0b810h
    mov ds, ax

    mov bx, 0
    mov ax, 101H
    
mov cx, 4 s: mov [bx], ax add bx, 2 add ax, 101H loop s mov ah, 4ch int 21h code ends end

給出使用masm、link工具彙編、連結的命令列及執行結果截圖

給出使用debug除錯的截圖。結合可執行檔案載入後暫存器CX的值,使用u命令精確反彙編截圖

靈活使用t命令/p命令、g命令,對ex2.exe進行除錯的截圖

回答問題

結合上述實驗和觀察,分析、對比ex2.asm和ex1.asm,它們實現的是否是相同的功能和效果?在具體實現上有什麼不同?

答:作用相同,都是向b810:0000開始的記憶體空間中寫入0101 0202 0303 0404四個字。在具體實現上,ex1.asm使用8行(幾乎)重複的程式碼完成寫入操作,而ex2.asm使用了loop結構完成寫入操作。

3.實驗任務3

給出原始碼

; ex3.asm
assume cs:code
code segment
    mov ax, 0b800h
    mov ds, ax

    mov bx, 07b8h
    mov cx, 16

s:  mov [bx], 0237h
    inc bx
    inc bx
    loop s

    mov ah, 4ch
    int 21h
code ends
end

給出執行結果截圖

把填充的字資料,從0237H 改成0239H,再次儲存後,彙編、連結、執行,觀察結果。

把填充的字資料,從0237H 改成0437H,再次儲存後,彙編、連結、執行,觀察結果。

猜測並分析,這個字資料中高位位元組裡存放的是什麼資訊,低位位元組裡存放的是什麼資訊。

容易想到,37h為字元'7'對應的ASCII碼,39h為字元'9'對應的ASCII碼。在網路上檢索到相關資料:

Reference:(BelalHashmi,Assembly Language Programming Lecture Notes, Chap. 6)

The video device is seen by the computer as a memory area containing the ASCII codes that are currently displayed on the screen ......Therefore if that appropriate block of the screen is cleared, the screen will be cleared. If theASCII of ‘A’ is placed somewhere in that block, the shape of ‘A’ will appear onthe screen at a corresponding place ......This correspondence must be defined as the memory is a singledimensional space while the screen is two dimensional having 25 rows and80 columns. The memory is linearly mapped on this two dimensional space,just like a two dimensional is mapped in linear memory. There is one wordper character in which a byte is needed for the ASCII code and the other byteis used for the character’s attributes discussed later. Now the first 80 wordswill correspond to the first row of the screen and the next 80 words willcorrespond to the next row. By making the memory on the video controlleraccessible to the processor via the system bus, the processor is now incontrol of what is displayed on the screen ......It was fixed at the physical memory location ofB8000. The first byte at this location contains the ASCII for the characterdisplayed at the top left of the video screen ......The second byte in the word designated for one screen location holds theforeground and background colors for the character. This is called its videoattribute. So the pair of the ASCII code in one byte and the attribute in thesecond byte makes the word that corresponds to one location on the screen.The lower address contains the code while the higher one contains theattribute.The attribute byte as detailed below has the RGB for the foreground and the background.

由於0x07b8 = 1976 = 988*2 = (12*80+28)*2,因此向b800:07b8寫入資料就相當於向顯示器第13行第29個字元位置寫入資料。同時,0x0237就表示前景顏色為綠色的字元'7';0x0237就表示前景顏色為綠色的字元'9';0x0437就表示前景顏色為紅色的字元'7'。從下圖也可以看出第一個字元出現的位置正是顯示器第13行第29個字元。

同理,0x1737就表示前景顏色為白色、背景顏色為藍色的字元'7'。

4.實驗任務4

程式原始碼

 1 ; ex4.asm
 2 assume cs:code
 3 code segment
 4     mov ax, 0
 5     mov ds, ax
 6 
 7     mov bx, 200h
 8     mov al, 0
 9     mov cx, 40h
10 
11 ; clear memory
12 t:      mov byte ptr [bx], 0
13     inc bx
14     loop t
15     mov bx, 200h
16     mov cx, 40h
17 
18 s:      mov byte ptr [bx], al
19     inc bx
20     inc al
21     loop s
22 
23     mov ah, 4ch
24     int 21h
25 code ends
26 end

彙編、連結無誤後,靈活使用debug的t命令、g命令、p命令除錯,用d命令檢視0:200~0:23F,確認是否將0~63傳送至此段記憶體區域。

首先執行至Line 17,確保0:200h附近的空間資料為0;然後執行至Line 23,檢視記憶體。

可見0~63被正確傳送至此段記憶體區域。

5.實驗任務5

填空以後的原始碼

 1 ; ex5.asm
 2 assume cs:code
 3 code segment
 4     mov ax,   cs  
 5     mov ds, ax
 6 
 7     mov ax, 20h
 8     mov es, ax
 9 
10     mov bx, 0
11     mov cx,   0  
12 
13 s:  mov al, [bx]
14     mov es:[bx], al
15     inc bx
16     loop s
17 
18     mov ah, 4ch
19     int 21h
20 code ends
21 end

以文字方式說明空白處填寫依據

注意Line 11中cx首先被置為0。為了確定Line 1 - 16的指令長度,cx首先被置為0,接著通過對程式進行debug檢視Line 1 - 16的指令長度,再將Line 11修改為該長度。將ax設為cs是因為確定程式碼段起始位置。

由上圖看出,將cx修改為16h即可。修改之後再次編譯、連結。

首先檢視20:0附近的記憶體,可以看出記憶體中的資料仍為實驗4中移動的資料。程式執行至Line 17,再次檢視記憶體。

可以看出程式被成功複製。

6.實驗任務6

原始碼

 1 ; example.asm
 2 
 3 global _start
 4 
 5 section .data
 6     msg db "a simple test", 0xa
 7     len equ $ - msg
 8 
 9 section .text
10 _start:
11     mov eax, 4
12     mov ebx, 1
13     mov ecx, msg
14     mov edx, len
15     int 0x80
16 
17     mov eax, 1
18     mov ebx, 7
19     int 0x80

對example.asm進行彙編、連結、執行。

將example.asm中line18行中暫存器ebx的值改為7,重新彙編、連結、執行。

五、實驗總結

關於DOS程式執行結束後的返回

在以上ex1-ex5中,程式均包含以下兩行程式碼

mov ah, 4ch
int 21h

翻閱書本,在P81有

這兩條指令所實現的功能就是程式返回。

在目前階段,我們不必去理解int 21h指令的含義,和為什麼要在這條指令的前面加上指令movax, 4c00h。我們只要知道,在程式的末尾使用這兩條指令就可以實現程式返回。

此兩行程式碼涉及DOS中Interrupt(中斷)的知識,詳細內容可在書本Chap. 12-13被參照。此處僅做最基本的介紹。int為一條x86指令,其作用為產生軟體中斷,

在電腦科學中,中斷Interrupt)是指處理器接收到來自硬體或軟體的訊號,提示發生了某個事件,應該被注意,這種情況就稱為中斷。軟體中斷。是一條CPU指令,用以自陷一箇中斷。由於軟中斷指令通常要執行一個切換CPU至核心態(Kernel Mode/Ring 0)的子例程,它常被用作實現系統呼叫。

int指令的格式為

int X

其中X用於指出具體的中斷。int 21h表示呼叫系統服務。該指令根據ax暫存器的高位元組(ah)中的資料,又可實現不同的功能。當ah為4ch時,表示終止程式。故程式均包含該兩行程式碼以正常退出。對於其它的中斷,讀者可參照該網站以及該pdf獲取更多資訊。