1. 程式人生 > 實用技巧 >15分鐘搭建RocketMQ原始碼除錯環境

15分鐘搭建RocketMQ原始碼除錯環境

1.實驗任務1

 1 assume cs: code,ds: data
 2 
 3 data segment
 4 db 119,101,108,99,111,109,101,32,116,111,32,109,97,115,109,33  ;welcome to masm!(不包含屬性值)
 5 data ends
 6 
 7 code segment
 8 start:
 9 mov ax, 0b800h
10 mov ds, ax     ;設定記憶體段地址
11 mov bx, data
12 mov es, bx    ;data段的段地址
13 
14 mov di, 0    ;第一行
15 mov bx, 1984  ;根據計算,在螢幕的正中間為160*12+64
16 mov cx, 16 17 s0: 18 mov al, es:[di] 19 20 mov [bx], al ;ascii 21 inc bx 22 mov word ptr [bx], 2 ;feature, 0 000 0 010 23 inc bx 24 25 inc di 26 loop s0 27 28 mov di, 0  ;第二行 29 mov bx, 2144  ;在第一行的基礎上+160 30 mov cx, 16 31 s1: 32 mov al, es:[di] 33 34 mov [bx], al ;ascii 35 inc bx 36 mov word ptr [bx], 36
;綠地紅色 0 010 0 100 37 inc bx 38 39 inc di 40 loop s1 41 42 mov di, 0  ;第三行 43 mov bx, 2304  ;在第二行的基礎上+160 44 mov cx, 16 45 s2: 46 mov al, es:[di] 47 48 mov [bx], al ;ascii 49 inc bx 50 mov word ptr [bx], 113 ;白底藍色 0 111 0 001 51 inc bx 52 53 inc di 54 loop s2 55 56 mov ax, 4c00h 57 int 21h 58 59 code ends
60 end start

2.實驗任務2

 1 assume cs:code, ds:data
 2 data segment
 3 str db 'try', 0
 4 data ends
 5 code segment
 6 start:
 7 mov ax, data
 8 mov ds, ax  ;設定入口引數ds
 9 mov si, offset str  ;設定入口引數si
10 mov al, 2  ;設定入口引數al
11 call printStr
12 mov ah, 4ch
13 int 21h
14 printStr:
15 push bx
16 push cx
17 push si
18 push di
19 mov bx, 0b800H
20 mov es, bx
21 s: mov cl, [si]
22 mov ch, 0
23 jcxz over
24 mov ch, al
25 mov es:[di], cx
26 inc si
27 add di, 2
28 jmp s
29 over:
30 pop di
31 pop si
32 pop cx
33 pop bx
34 ret
35 code ends
36 end start

修改line3,line12後的執行結果截圖

這樣用的目的是在子程式的開始將子程式中所有用到的暫存器中的內容都儲存起來,在子程式返回前再回復,避免暫存器的衝突問題。

將當前記憶體中字元的值和屬性等存入彩色字元模式顯示緩衝區。

3. 實驗任務3

使用d命令檢視資料段的資料,1984已經轉換為字串

assume cs: code, ds: data
data segment
x dw 1984
str db 16 dup(0)
data ends
code segment
start:
mov ax, data
mov ds, ax
mov ax, x
mov di, offset str
call num2str

mov si, offset str
mov al, 2
call show  ;呼叫task2的子程式

mov ah, 4ch
int 21h

num2str:
push ax
push bx
push cx
push dx
mov cx, 0
mov bl, 10
s1:
div bl
inc cx
mov dl, ah
push dx
mov ah, 0
cmp al, 0
jne s1
s2:
pop dx
or dl, 30h
mov [di], dl
inc di
loop s2
pop dx
pop cx
pop bx
pop ax
ret

show:
push bx
push cx
push si
push di
mov bx, 0b800H
mov es, bx
mov di, 0
s3: 
mov cl, [si]
mov ch, 0
jcxz over
mov ch, al
mov es:[di], cx
inc si
add di, 2
jmp s3
over:
pop di
pop si
pop cx
pop bx
ret

code ends
end start

執行測試截圖

4. 實驗任務4

 1 assume cs:code, ds:data
 2 data segment
 3 str db 80 dup(?)
 4 data ends
 5 code segment
 6 start:
 7 mov ax, data
 8 mov ds, ax
 9 mov si, 0
10 s1:
11 mov ah, 1
12 int 21h
13 mov [si], al
14 cmp al, '#'
15 je next
16 inc si
17 jmp s1
18 next:
19 mov cx, si
20 mov si, 0
21 s2: mov ah, 2
22 mov dl, [si]
23 int 21h
24 inc si
25 loop s2
26 mov ah, 4ch
27 int 21h
28 code ends
29 end start

將從鍵盤輸入的字元存入記憶體中,直到當前字元為“#”,則跳轉到next子程式。

在螢幕上顯示出line12-19存入記憶體的字串。

5. 實驗任務5

觀察反彙編過程,發現函式的引數入棧順序與形參的順序相反。例如sum函式的形參順序是a,b,引數的實際入棧順序為b,a,返回值通過暫存器(本實驗中為eax)返回。

形參的傳遞是通過記憶體單元傳遞,將記憶體單元的值傳遞到子函式裡。暫存器ebp的值經過一定的修改,作為地址暫存器。