Last active
November 20, 2018 07:11
-
-
Save edom18/6b2708f89ef06573c8c9 to your computer and use it in GitHub Desktop.
アセンブラに手を出してみる ref: https://qiita.com/edo_m18/items/83c63cd69f119d0b9831
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; for intel syntax | |
mov eax, 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# for AT&T syntax | |
mov $1, %eax |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int add(int a, int b) { | |
int c = a + b; | |
return c; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mov al, [esi] | |
mov [edi], al | |
add edi, 1 | |
add esi, 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int add(int a, int b) { | |
int c = a + b; | |
return c; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0x00000h +----> + + | |
| | | |
| | | |
+-----------+ | |
| int c | | |
+-----------+ | |
| rbp | | |
+-----------+ | |
| int a | | |
+-----------+ | |
| int b | | |
+-----------+ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0x00000h +----> + + | |
| | | |
| | | |
+-----------+ | |
| int c | | |
+-----------+ | |
| rbp | | |
+-----------+ | |
| int a | | |
+-----------+ | |
| int b | | |
+-----------+ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int func0(int a, char *b, double *c); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
push(ポインタcの値); | |
push(ポインタbの値); | |
push(aの値); | |
call(int(func0)); | |
add(esp, 3 * 4); /* 引数が3個なので3 * 4 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void func(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
call(int(func)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
esp + 8 : c | |
esp + 4 : b | |
esp + 0 : a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
esp + 12: c | |
esp + 8 : b | |
esp + 4 : a | |
esp + 0 : 次の命令のアドレス |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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に格納する |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mov rax, rbx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ebp - 4 + (edx * 4)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mov rax, 0x2000004 ; System call write = 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mov rax, 5 | |
mov rbx, 3 | |
add rax, rbx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mov rax, 5 | |
mov rbx, 3 | |
sub rax, rbx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mov rax, 5 | |
mov rbx, 3 | |
mul rbx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mov rdx, 0 | |
mov rax, 5 | |
mov rbx, 3 | |
div rbx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mov rcx, 5 | |
mov rax, 0 | |
mov rbx, 3 | |
LOOPLABEL: | |
add rax, rbx | |
loop LOOPLABEL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mov [AFLAG], BYTE PTR 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mov al, [esi] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment