Академический Документы
Профессиональный Документы
Культура Документы
Pointers in x86-64 1
int main() {
int
int*
int*
x = 42, y = 99;
p1 = &x;
p2 = &y;
int** p3 = &p2;
int aa = *p1;
*p1 = 10;
int bb = **p3;
return 0;
}
CS@VT
Computer Organization I
2005-2015 McQuain
rbp
. . .
movl
movl
leaq
movq
leaq
movq
leaq
movq
movq
movl
movl
movq
movl
movq
movq
movl
movl
. . .
rbp -
main:
CS@VT
Pointers in x86-64 2
$42, -28(%rbp)
$99, -32(%rbp)
-28(%rbp), %rax
%rax, -8(%rbp)
-32(%rbp), %rax
%rax, -40(%rbp)
-40(%rbp), %rax
%rax, -16(%rbp)
-8(%rbp), %rax
(%rax), %eax
%eax, -20(%rbp)
-8(%rbp), %rax
$10, (%rax)
-16(%rbp), %rax
(%rax), %rax
(%rax), %eax
%eax, -24(%rbp)
old ebp
8
p1
rbp - 16
p3
rbp - 20
aa
rbp - 24
bb
rbp 28
rbp 32
rbp - 40
p2
Computer Organization I
the Stack
2005-2015 McQuain
Pointers in x86-64 3
rbp
main:
. . .
# x = 42; y = 99;
movl
$42, -28(%rbp)
movl
$99, -32(%rbp)
. . .
rbp -
old ebp
8
p1
rbp - 16
p3
rbp - 20
aa
rbp - 24
bb
rbp 28
x:
42
rbp 32
y:
99
rbp - 40
p2
the Stack
CS@VT
Computer Organization I
2005-2015 McQuain
Pointers in x86-64 4
. . .
main:
. . .
# p1 = &x
leaq
-28(%rbp), %rax
movq
%rax, -8(%rbp)
# p2 = &y
leaq
-32(%rbp), %rax rbp
movq
%rax, -40(%rbp)
rbp . . .
old ebp
8
p1: rbp - 28
rbp - 16
p3
rbp - 20
aa
rbp - 24
bb
rbp 28
x:
42
y:
99
&x
rbp 32
&y
rbp - 40
p2: rbp - 32
the Stack
CS@VT
Computer Organization I
2005-2015 McQuain
Pointers in x86-64 5
. . .
main:
. . .
# p3 = &p2
leaq
-40(%rbp), %rax
movq
%rax, -16(%rbp)
. . .
rbp
rbp -
&p2
old ebp
8
p1: rbp - 28
rbp - 16
p3: rpb - 40
rbp - 20
aa
rbp - 24
bb
rbp 28
x:
42
rbp 32
y:
99
rbp - 40
p2: rbp - 32
the Stack
CS@VT
Computer Organization I
2005-2015 McQuain
Pointers in x86-64 6
. . .
main:
. . .
# aa = *p1
movq
-8(%rbp), %rax # rax = *(rbp 8) = p1
movl
(%rax), %eax
# rax = *rax = *(*(rbp 8) = *p1
movl
%eax, -20(%rbp) # aa = *(rbp 20) = eax
. . .
rbp
rbp -
old ebp
8
p1: rbp - 28
rbp - 16
p3: rpb - 40
rbp - 20
aa:
rbp - 24
42
bb
rbp 28
x:
42
rbp 32
y:
99
rbp - 40
p2: rbp - 32
the Stack
CS@VT
Computer Organization I
2005-2015 McQuain
Pointers in x86-64 7
. . .
main:
. . .
# *p1 = 10
movq
-8(%rbp), %rax
movl
$10, (%rax)
. . .
# rax = *(rbp 4) = p1
# *p1 = *rax = 10
rbp
rbp -
old ebp
8
p1: rbp - 28
rbp - 16
p3: rpb - 40
rbp - 20
aa:
rbp - 24
42
bb
rbp 28
x:
10
rbp 32
y:
99
rbp - 40
p2: rbp - 32
the Stack
CS@VT
Computer Organization I
2005-2015 McQuain
Pointers in x86-64 8
. . .
main:
. . .
# bb = **p3
movq
-16(%rbp), %rax
movq
(%rax), %rax
movl
(%rax), %eax
movl
%eax, -24(%rbp)
. . .
#
#
#
#
rbp
rbp -
old ebp
8
p1: rbp - 28
rbp - 16
p3: rpb - 40
rbp - 20
aa:
42
rbp - 24
bb:
99
rbp 28
x:
10
rbp 32
y:
99
rbp - 40
CS@VT
Computer Organization I
p2: rbp - 32
the Stack
2005-2015 McQuain
uint32_t
{
Pointers in x86-64 9
CS@VT
Computer Organization I
2005-2015 McQuain
CS@VT
Pointers in x86-64 10
rbp
rbp -
old ebp
4
...
rbp 20
loop variable x
...
...
rbp - 40
array
rbp - 44
size
Computer Organization I
the Stack
2005-2015 McQuain
Pointers in x86-64 11
. . .
ArrayExample:
# Test if int* array (i.e. %rbp 40) is NULL.
cmpq
$0, -40(%rbp)
# If array is not NULL jump to the body of the function.
jne
.L2
# We return 0 if array == NULL, so we put 0 in %eax then
# jump to the very end of the function.
movl
$0, %eax
jmp
.L3
rbp
rbp -
old rbp
4
...
rbp 20
loop variable x
...
...
rbp - 40
array
rbp - 44
size
the Stack
CS@VT
Computer Organization I
2005-2015 McQuain
Pointers in x86-64 12
. . .
# After the body, do the loop test
.L4:
# Put the loop variable in %eax
movl
-20(%rbp), %eax
# The loop test, compare x and size.
# The condition in the C code was x < size
# Performs the operation x size, discarding the result.
cmpl
-44(%rbp), %eax
# Repeat if x is below size
jb
.L5
rbp
rbp -
old rbp
4
...
rbp 20
loop variable x
...
...
rbp - 40
array
rbp - 44
size
the Stack
CS@VT
Computer Organization I
2005-2015 McQuain
Pointers in x86-64 13
$0, -20(%rbp)
.L4
movl
leaq
-20(%rbp), %eax
0(,%rax,4), %rdx
#
#
#
movq
-40(%rbp), %rax
#
leaq
(%rdx,%rax), %rbx #
#
#
# snip. . . %eax stores the
movl
%eax, (%rbx)
#
#
#
#
#
addl
$1, -20(%rbp)
.L5:
.L4
. . .
CS@VT
Computer Organization I
2005-2015 McQuain
Aside: leal
Pointers in x86-64 14
0(,%rax,2), %rdx
# rdx = 4*rax
# i.e. x*sizeof(uint32_t)
. . .
CS@VT
Computer Organization I
2005-2015 McQuain
Pointers as Parameters
Pointers in x86-64 15
#include <stdint.h>
int main() {
uint32_t X = 100;
uint32_t Y = 200;
Swap(&X, &Y);
return 0;
}
void Swap(uint32_t* A, uint32_t* B) {
uint32_t Temp = *A;
*A = *B;
*B = Temp;
// Temp = 100
// X = 200
// Y = 100
The pass-by-pointer protocol provides a called function with the ability to modify the
value of the caller's variable.
CS@VT
Computer Organization I
2005-2015 McQuain
Pointers in x86-64 16
. . .
Swap:
pushq
movq
subq
movq
movq
movq
movl
movl
movq
movl
movq
movl
movq
movl
movl
leave
ret
. . .
CS@VT
%rbp
%rsp, %rbp
$32, %rsp
%rdi, -24(%rbp)
%rsi, -32(%rbp)
-24(%rbp), %rax
(%rax), %eax
%eax, -4(%rbp)
-32(%rbp), %rax
(%rax), %edx
-24(%rbp), %rax
%edx, (%rax)
-32(%rbp), %rax
-4(%rbp), %edx
%edx, (%rax)
rbp + 8
X:
100
Y:
200
return address
rbp
old rbp
rbp -
rbp -
Temp:
??
rbp - 12
...
...
rbp 24
&X
rbp 32
&Y
the Stack
Computer Organization I
2005-2015 McQuain