Assembly - moving data 組合語言,移動資料
從記憶體中讀資料
load date from memory into register
把暫存器中存資料存進記憶體
store register data into memory
Figure 1. integer registes,
source:https://www.cs.cmu.edu/~fp/courses/15213-s07/misc/asm64-handout.pdf
mov operand combinations(mov的運算整理)
mov imm to register, mov $0x11,%eax -> var_a=0x11;
mov imm to mem, mov $0x39,(%eax) -> *p=0x39;
mov reg to reg, mov %edx,%eax -> var_a=var_b;
mov reg to mem, mov %eax,(%edx) -> *p=var_a;
mov mem to reg, mov (%eax),%eax -> var_a=*p;
*can not do memory to memory transfer in single instruction.
Here is an simple swap code:
void swap(int *xp, int *yp)
{
int a=*xp;
int b=*yp;
*xp=b;
*yp=a;
}
Let's see how above code done by assembly.
Variables store in which memory place and registers is assigned by complier.
讓我們來看上面的程式碼,如何使用組合語言完成,
變數會放在哪個register對應到哪個memory位址,是由complier決定的。
gcc -g -c mov.c -m32 -o mov32
00000000 :
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 83 ec 10 sub $0x10,%esp
//start-- int a=*xp;
//move Memory[ebp+0x8](xp) to register exa.
//move memory content(*xp) to register eax
//move value(*xp) in eax to Memory[ebp-0x4](variable a).
6: 8b 45 08 mov 0x8(%ebp),%eax
9: 8b 00 mov (%eax),%eax
b: 89 45 fc mov %eax,-0x4(%ebp)
//end int a=*xp;
// int b=*yp;
// *yp會存在記憶體中ebp+0xC的位址,先把這位址存到eax中
// 再把exa記憶體中對應的值取出來存到,eax
//接著把exa的值,存到b變數所在的記憶體ebp-0x8中
e: 8b 45 0c mov 0xc(%ebp),%eax
11: 8b 00 mov (%eax),%eax
13: 89 45 f8 mov %eax,-0x8(%ebp)
//end int b=*yp;
16: 8b 45 08 mov 0x8(%ebp),%eax
19: 8b 55 f8 mov -0x8(%ebp),%edx
1c: 89 10 mov %edx,(%eax)
1e: 8b 45 0c mov 0xc(%ebp),%eax
21: 8b 55 fc mov -0x4(%ebp),%edx
24: 89 10 mov %edx,(%eax)
26: c9 leave
27: c3 ret
X86-64
gcc -g -c mov.c -o mov64
0000000000000000:
0: 55 push %rbp
1: 48 89 e5 mov %rsp,%rbp
4: 48 89 7d e8 mov %rdi,-0x18(%rbp)
8: 48 89 75 e0 mov %rsi,-0x20(%rbp)
c: 48 8b 45 e8 mov -0x18(%rbp),%rax
10: 8b 00 mov (%rax),%eax
12: 89 45 fc mov %eax,-0x4(%rbp)
15: 48 8b 45 e0 mov -0x20(%rbp),%rax
19: 8b 00 mov (%rax),%eax
1b: 89 45 f8 mov %eax,-0x8(%rbp)
1e: 48 8b 45 e8 mov -0x18(%rbp),%rax
22: 8b 55 f8 mov -0x8(%rbp),%edx
25: 89 10 mov %edx,(%rax)
27: 48 8b 45 e0 mov -0x20(%rbp),%rax
2b: 8b 55 fc mov -0x4(%rbp),%edx
2e: 89 10 mov %edx,(%rax)
30: 5d pop %rbp
31: c3 retq
reference:
https://www.cs.cmu.edu/~fp/courses/15213-s07/misc/asm64-handout.pdf
load date from memory into register
把暫存器中存資料存進記憶體
store register data into memory
source:https://www.cs.cmu.edu/~fp/courses/15213-s07/misc/asm64-handout.pdf
mov operand combinations(mov的運算整理)
mov imm to register, mov $0x11,%eax -> var_a=0x11;
mov imm to mem, mov $0x39,(%eax) -> *p=0x39;
mov reg to reg, mov %edx,%eax -> var_a=var_b;
mov reg to mem, mov %eax,(%edx) -> *p=var_a;
mov mem to reg, mov (%eax),%eax -> var_a=*p;
*can not do memory to memory transfer in single instruction.
Here is an simple swap code:
void swap(int *xp, int *yp)
{
int a=*xp;
int b=*yp;
*xp=b;
*yp=a;
}
Let's see how above code done by assembly.
Variables store in which memory place and registers is assigned by complier.
讓我們來看上面的程式碼,如何使用組合語言完成,
變數會放在哪個register對應到哪個memory位址,是由complier決定的。
gcc -g -c mov.c -m32 -o mov32
00000000 :
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 83 ec 10 sub $0x10,%esp
//start-- int a=*xp;
//move Memory[ebp+0x8](xp) to register exa.
//move memory content(*xp) to register eax
//move value(*xp) in eax to Memory[ebp-0x4](variable a).
6: 8b 45 08 mov 0x8(%ebp),%eax
9: 8b 00 mov (%eax),%eax
b: 89 45 fc mov %eax,-0x4(%ebp)
//end int a=*xp;
// int b=*yp;
// *yp會存在記憶體中ebp+0xC的位址,先把這位址存到eax中
// 再把exa記憶體中對應的值取出來存到,eax
//接著把exa的值,存到b變數所在的記憶體ebp-0x8中
e: 8b 45 0c mov 0xc(%ebp),%eax
11: 8b 00 mov (%eax),%eax
13: 89 45 f8 mov %eax,-0x8(%ebp)
//end int b=*yp;
16: 8b 45 08 mov 0x8(%ebp),%eax
19: 8b 55 f8 mov -0x8(%ebp),%edx
1c: 89 10 mov %edx,(%eax)
1e: 8b 45 0c mov 0xc(%ebp),%eax
21: 8b 55 fc mov -0x4(%ebp),%edx
24: 89 10 mov %edx,(%eax)
26: c9 leave
27: c3 ret
X86-64
gcc -g -c mov.c -o mov64
0000000000000000
0: 55 push %rbp
1: 48 89 e5 mov %rsp,%rbp
4: 48 89 7d e8 mov %rdi,-0x18(%rbp)
8: 48 89 75 e0 mov %rsi,-0x20(%rbp)
c: 48 8b 45 e8 mov -0x18(%rbp),%rax
10: 8b 00 mov (%rax),%eax
12: 89 45 fc mov %eax,-0x4(%rbp)
15: 48 8b 45 e0 mov -0x20(%rbp),%rax
19: 8b 00 mov (%rax),%eax
1b: 89 45 f8 mov %eax,-0x8(%rbp)
1e: 48 8b 45 e8 mov -0x18(%rbp),%rax
22: 8b 55 f8 mov -0x8(%rbp),%edx
25: 89 10 mov %edx,(%rax)
27: 48 8b 45 e0 mov -0x20(%rbp),%rax
2b: 8b 55 fc mov -0x4(%rbp),%edx
2e: 89 10 mov %edx,(%rax)
30: 5d pop %rbp
31: c3 retq
留言