⓪編著 : 蕭沖

看了好幾年的Assembly了,總一段時間就會翻一下,尤其是在Debug hi-level language或是做reserve engineer(dont say it crack :->)時會參考。直接用ASM來寫程式是難得有機會了,所以某些學習過的記憶也經常就忘了。對於386 addressing的部份,看過許多不同的書,分類方式總有些些不同,在此想把各種版本的精華歸納出來。

Instruction Format (微指令格式)

Lower mem addr ----------------------->Higher mem addr
prefix|opcode|mod|reg|r/m|displacement|immediate data

Prefix :1 Byte, Optional, chage the default size of opcode size field(W),or
change address size, override selector....
Opcode :1~2 Bytes micro instruction
Mod+r/m :combined to operand (運算元之一).
mod 2Byte indicate that r/m (3Bytes) is mem or reg.
Reg :3 bytes operand (另一個運算元)
Displacement : 1~ 4 Bytes (位移)
Immediate data : 1~4 Bytes (立即資料,為常數)

所謂定址法(addressing mode)就是:「CPU找資料所在位置的方法」
以Instruction Format來看就是指opcode後面的那些值,(mod+r/m, reg, disp, immediate data)。
以mov ax, bx 這個Instruction來說,就是用二個相同的暫存器定址模式(register addressing mode),也就是說資料都是存放在register裡,即ax和bx裡。整個指令的意思就是把bx裡的資料copy到ax裡(注:有人直接翻mov為"搬",這樣感覺指令結束後bx會變成空的,然而不是這樣的)。
以mov ax, [mem] 來說,則用了二種不同的定址法: 其一是上面的reg addressing,另一個則是記憶體直接模式(memory direct mode or called displacement)。意思是去mem這個位址找資料然後把資料copy到ax暫存器裡。

我的分類法:
1/ 依容器來分 2/把memory模式再依取法分 3/在把memory indirect的部份依元素來分


+-REG mode +---General REG mode
| |___Segment(selector) REG mode
+-Immediate mode
|
+-Memory mode +--Direct mode(Displacement mode)
|__Indirect mode+-Memory Indirect mode(REG indirect modeS mix)
|_REG indirect mode+- base reg indirect
|_ base reg + displacement
|_ base reg + index
|_ base reg + index + disp
|_ ESP- mode
|_ ESP+ mode


定址法與高階語言的關聯:

1/ 程式中的全域變數在compile的時候就會經過計算把變數名換成"固定"的相對記憶體位址。比如說變數gVar被轉成0x11A0000的位址,而和gVar相關的statement則會被轉成Direct mode addresing,for example:
gVar += 2; 在runtime時的ASM被轉成

add dword ptr [0x004ce408], 0x02

這指令即用了 Direct mode + Immediate mode。故我稱gVar為直間變數。

2/在函式裡編寫下面的方程:


int i = 7; //C745 F4 07000000 MOV DWORD PTR SS:[EBP-C],7
int *p_var = &i;
//8D45 F4 LEA EAX,DWORD PTR SS:[EBP-C]
//8945 F0 MOV DWORD PTR SS:[EBP-10],EAX
(*p_ar)++;
//8B55 F0 MOV EDX,DWORD PTR SS:[EBP-10]
//FF02 INC DWORD PTR DS:[EDX]


注解的部份是被組譯後的情形。而最後一行INC DWORD PTR DS:[EDX]則使用了Indirect mode。
也是我想強調的部份,指標最終都是使用間接模式。故我稱指標為接間接變數!
順便也解說一下上面幾行機器碼的Instruction Foramt:
C745 F4 07000000 <<-- C7是MOV 的opcode,45是mod+r/m 與reg共1Byte,F4(C的補數)為1byte的有號值,等於-12,此為displacement的部份,最後一部份是立即值07000000共4byte。
同樣是MOV微指令,但當定址方式不同,或是二個operand位置不同等,都會造成MOV這個微指令有所不同。
arrow
arrow
    全站熱搜

    aftcast 發表在 痞客邦 留言(0) 人氣()