x86 machine code 初探 (2) - displacement only

作者: descent (「雄辯是銀,沉默是金」)   2015-03-04 10:25:14
intel 手冊我很早就印出來拜讀, 所以我的版本沒有 64 bit 部份, 我大部份的心力都放
在 16/32 bit 環境下。
這次我們來看看特例的例子:
address_mode.S
1 # practice x86 machine code
3 .code32
4 .text
5 .global begin
6 begin:
7 mov 0x1234,%esi
address_mode.S L7 僅僅使用 displacement 這個欄位。這是把 0x1234 位址的 4 個
byte 複製到 %esi。
objdump -d address_mode.elf
1 descent@w-linux:x86_machine_code$ objdump -d address_mode.elf
2
3 address_mode.elf: file format elf32-i386
4
5
6 Disassembly of section .text:
7
8 00000100 <_text>:
9 100: 8b 35 34 12 00 00 mov 0x1234,%esi
來看看 machine code, 8b 就不提了, 應該很簡單。
8b 查這個表
http://pdos.csail.mit.edu/6.828/2006/readings/i386/appa.htm (
http://goo.gl/ioQbT1 )
-> Gv, Ev
E
A modR/M byte follows the opcode and specifies the operand. The operand is
either a general register or a memory address. If it is a memory address,
the address is computed from a segment register and any of the following
values: a base register, an index register, a scaling factor, a
displacement.
G
The reg field of the modR/M byte selects a general register; e.g., ADD (
http://goo.gl/qiBRQP ) (00).
v
Word or double word, depending on operand size attribute.
感覺好像是 Ev, word 是 2 byte, double word 是 4 byte。
modrm: 35
mod: 00
reg: 110
r/m: 101
reg: 110 -> esi
mod:00
r/m: 101
結果可不是 ebp base register, 如果是 ebp base register, 那組合語言就是
mov (%ebp),%esi
不過範例中的組合語言不是這個, 這是一個例外, 很意外嗎?不過還有另外一個例外。
這是表示後面接 displacement。所以 34 12 00 00 就是 0x1234 displacement。
那 mov (%ebp),%esi 會產生什麼樣的 machine code 呢?
8b 75 00 mov 0x0(%ebp),%esi
modrm: 75
mod: 01
reg: 110 -> esi
r/m: 101
mod:01
r/m: 101
查表http://goo.gl/mL4AgR (
http://pdos.csail.mit.edu/6.828/2006/readings/i386/s17_02.htm ) 得到 ->
disp8[ebp]
00 是 8bit displacement, 所以還是可以使用 ebp 當 base register, 只不過
displacement 要為 0, 比其他的 base register 多出一個 byte。和 epb 搭配的預設
segment register 是 ss, 在處理 stack 時會用到。
一次只談簡單的例子, 這樣應該很容易理解。
// 本文使用 Blog2BBS 自動將Blog文章轉成縮址的BBS純文字 http://goo.gl/TZ4E17 //
blog 原文:
descent-incoming.blogspot.tw/2013/06/x86-machine-code-2-displacement-only.html

Links booklink

Contact Us: admin [ a t ] ucptt.com