x86 machine code 初探 (2) - displacement only

看板ASM (組合語言)作者 (「雄辯是銀,沉默是金」)時間10年前 (2015/03/04 10:25), 10年前編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
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 -- 要有一流的運氣, 才能邂逅一流的美女。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.236.200.48 ※ 文章網址: https://www.ptt.cc/bbs/ASM/M.1425435931.A.5B6.html ※ 編輯: descent (36.236.200.48), 03/04/2015 10:29:41
文章代碼(AID): #1KzcqRMs (ASM)
文章代碼(AID): #1KzcqRMs (ASM)