Skip to content

Instantly share code, notes, and snippets.

@edom18
Last active November 20, 2018 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edom18/6b2708f89ef06573c8c9 to your computer and use it in GitHub Desktop.
Save edom18/6b2708f89ef06573c8c9 to your computer and use it in GitHub Desktop.
アセンブラに手を出してみる ref: https://qiita.com/edo_m18/items/83c63cd69f119d0b9831
; for intel syntax
mov eax, 1
# for AT&T syntax
mov $1, %eax
int add(int a, int b) {
int c = a + b;
return c;
}
mov al, [esi]
mov [edi], al
add edi, 1
add esi, 1
int add(int a, int b) {
int c = a + b;
return c;
}
0x00000h +----> + +
| |
| |
+-----------+
| int c |
+-----------+
| rbp |
+-----------+
| int a |
+-----------+
| int b |
+-----------+
0x00000h +----> + +
| |
| |
+-----------+
| int c |
+-----------+
| rbp |
+-----------+
| int a |
+-----------+
| int b |
+-----------+
int func0(int a, char *b, double *c);
push(ポインタcの値);
push(ポインタbの値);
push(aの値);
call(int(func0));
add(esp, 3 * 4); /* 引数が3個なので3 * 4 */
void func();
call(int(func));
esp + 8 : c
esp + 4 : b
esp + 0 : a
esp + 12: c
esp + 8 : b
esp + 4 : a
esp + 0 : 次の命令のアドレス
movl -4(%ebp, %edx, 4), %eax # 完全な例: (ebp - 4 + (edx * 4))のアドレスの内容をeaxに転送する
movl -4(%ebp), %eax # よくある例: スタックの値をeaxに転送する
movl (%ecx), %edx # オフセットのない場合: ポインタの指す内容をレジスタに転送する
leal 8(,%eax,4), %eax # 算術演算の例: eaxに4を掛け8を足したアドレスをeaxに格納する
leal (%eax,%eax,2), %eax # 算術演算の例: eaxの指す値を3倍したアドレスをeaxに格納する
mov rax, rbx
[ebp - 4 + (edx * 4)]
The number of the syscall has to be passed in register rax.
rdi - used to pass 1st argument to functions
rsi - used to pass 2nd argument to functions
rdx - used to pass 3rd argument to functions
rcx - used to pass 4th argument to functions
r8 - used to pass 5th argument to functions
r9 - used to pass 6th argument to functions
A system-call is done via the syscall instruction. The kernel destroys registers rcx and r11.
mov rax, 0x2000004 ; System call write = 4
section .data
hello_world db "Hello World!", 0x0a
section .text
global start
start:
mov rax, 0x2000004 ; System call write = 4
mov rdi, 1 ; Write to standard out = 1
mov rsi, hello_world ; The address of hello_world string
mov rdx, 14 ; The size to write
syscall ; Invoke the kernel
mov rax, 0x2000001 ; System call number for exit = 1
mov rdi, 0 ; Exit success = 0
syscall ; Invoke the kernel
mov rax, 5
mov rbx, 3
add rax, rbx
mov rax, 5
mov rbx, 3
sub rax, rbx
mov rax, 5
mov rbx, 3
mul rbx
mov rdx, 0
mov rax, 5
mov rbx, 3
div rbx
mov rcx, 5
mov rax, 0
mov rbx, 3
LOOPLABEL:
add rax, rbx
loop LOOPLABEL
mov [AFLAG], BYTE PTR 1
mov al, [esi]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment