0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

20260419 x86 enter命令

0
Posted at

初めに

BorlandCという古いコンパイラでC言語をアセンブリ言語に変換してみると、enterという命令が出てきました。

今までGCCを使っていた時には一度も出てきた事が無く、今回初めて見かけたので使用方法を調べてみました。

実証

nasm -f elf32 test.asm -o test.o
ld -m elf_i386 test.o -o test
objdump -d ./test -M intel
gdb ./test
run
info registers ebp esp
continue
info registers ebp esp

enter

enter 0x10は以下と同じ動きをする。

    push ebp
    mov ebp, esp
    sub esp, 0x10

32ビットのebppushしているのでesp = 0xffffd2a0 - 4バイト = 0xffffd29c

espebpに代入するのでebp = 0xffffd2a0となる

esp = 0xffffd29c - 0x10 = 0xffffd28c

0x08049001 in _start ()
(gdb) info registers ebp esp
ebp            0x0                 0x0
esp            0xffffd2a0          0xffffd2a0
(gdb) continue
Continuing.

Program received signal SIGTRAP, Trace/breakpoint trap.
0x08049006 in _start ()
(gdb) info registers ebp esp
ebp            0xffffd29c          0xffffd29c
esp            0xffffd28c          0xffffd28c
test.asm
section .text
global _start

_start:

    ; 方法1: enterを使う
    int3               ; ここでブレークして初期状態を確認
    enter 0x10, 0
    
    int3               ; enter 後のレジスタ状態を確認
    leave
    
    ; 終了
    mov eax, 1
    mov ebx, 0
    int 0x80

手動

test.asm
section .text
global _start

_start:
    
    ; 方法2: subを使う
    int3               ; リセット後
    push ebp
    mov ebp, esp
    sub esp, 0x10
    
    int3               ; sub 後のレジスタ状態を確認
    mov esp, ebp
    pop ebp
    
    ; 終了
    mov eax, 1
    mov ebx, 0
    int 0x80
0x08049001 in _start ()
(gdb) info registers ebp esp
ebp            0x0                 0x0
esp            0xffffd2a0          0xffffd2a0
(gdb) continue
Continuing.

Program received signal SIGTRAP, Trace/breakpoint trap.
0x08049008 in _start ()
(gdb) info registers ebp esp
ebp            0xffffd29c          0xffffd29c
esp            0xffffd28c          0xffffd28c
0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?