Credits and Disclaimers Pointers in x 86 64

  • Slides: 17
Download presentation
Credits and Disclaimers Pointers in x 86 -64 1 The examples and discussion in

Credits and Disclaimers Pointers in x 86 -64 1 The examples and discussion in the following slides have been adapted from a variety of sources, including: Chapter 3 of Computer Systems 3 nd Edition by Bryant and O'Hallaron x 86 Assembly/GAS Syntax on Wiki. Books (http: //en. wikibooks. org/wiki/X 86_Assembly/GAS_Syntax) Using Assembly Language in Linux by Phillip ? ? (http: //asm. sourceforge. net/articles/linasm. html) The C code was compiled to assembly with gcc version 4. 8. 3 on Cent. OS 7. Unless noted otherwise, the assembly code was generated using the following command line: gcc –S –m 64 -fno-asynchronous-unwind-tables –mno-red-zone –O 0 file. c AT&T assembly syntax is used, rather than Intel syntax, since that is what the gcc tools use. CS@VT Computer Organization I © 2005 -2015 Mc. Quain

C Example Pointers in x 86 -64 2 int main() { int* x =

C Example Pointers in x 86 -64 2 int main() { int* x = 42, y = 99; p 1 = &x; p 2 = &y; // p 1 stores address of variable x // p 2 stores address of variable y int** p 3 = &p 2; // p 3 stores address of variable p 2 int aa = *p 1; *p 1 = 10; // aa stores value of the target of p 1, 42 // the target of p 1, which is x, stores 10 int bb = **p 3; // bb stores value of the target of the // target of p 3; p 3 points to p 1 and // p 1 points to x, so bb gets value 99 return 0; } CS@VT Computer Organization I © 2005 -2015 Mc. Quain

x 86 -64 Assembly View. . . rbp . . . movl leaq movq

x 86 -64 Assembly View. . . rbp . . . movl leaq movq movl movq movl. . . rbp - main: CS@VT Pointers in x 86 -64 3 $42, -28(%rbp) $99, -32(%rbp) -28(%rbp), %rax, -8(%rbp) -32(%rbp), %rax, -40(%rbp), %rax, -16(%rbp) -8(%rbp), %rax (%rax), %eax, -20(%rbp) -8(%rbp), %rax $10, (%rax) -16(%rbp), %rax (%rax), %eax, -24(%rbp) old rbp 8 p 1 rbp - 16 p 3 rbp - 20 aa rbp - 24 bb rbp – 28 x rbp – 32 y rbp - 40 p 2 Computer Organization I the Stack © 2005 -2015 Mc. Quain

x 86 -64 Assembly View. . . Pointers in x 86 -64 4 rbp

x 86 -64 Assembly View. . . Pointers in x 86 -64 4 rbp main: . . . # x = 42; y = 99; movl $42, -28(%rbp) movl $99, -32(%rbp). . . rbp - old rbp 8 p 1 rbp - 16 p 3 rbp - 20 aa rbp - 24 bb rbp – 28 x: 42 rbp – 32 y: 99 rbp - 40 p 2 the Stack CS@VT Computer Organization I © 2005 -2015 Mc. Quain

x 86 -64 Assembly View Pointers in x 86 -64 5 . . .

x 86 -64 Assembly View Pointers in x 86 -64 5 . . . main: . . . # p 1 = &x leaq -28(%rbp), %rax movq %rax, -8(%rbp) # rax = rbp – 28 = &x # p 1 = *(rbp – 8) = rax # p 2 = &y leaq -32(%rbp), %rax rbp movq %rax, -40(%rbp) rbp. . . old rbp 8 p 1: rbp - 28 rbp - 16 p 3 rbp - 20 aa rbp - 24 bb rbp – 28 x: 42 y: 99 &x the address of x: rbp - 28 rbp – 32 &y the address of y: rbp - 32 rbp - 40 p 2: rbp - 32 the Stack CS@VT Computer Organization I © 2005 -2015 Mc. Quain

x 86 -64 Assembly View Pointers in x 86 -64 6 . . .

x 86 -64 Assembly View Pointers in x 86 -64 6 . . . main: . . . # p 3 = &p 2 leaq -40(%rbp), %rax movq %rax, -16(%rbp). . . # rax = rbp – 28 = &p 2 # p 3 = *(rbp – 16) = eax rbp - &p 2 the address of p 2: rbp – 40 old rbp 8 p 1: rbp - 28 rbp - 16 p 3: rpb - 40 rbp - 20 aa rbp - 24 bb rbp – 28 x: 42 rbp – 32 y: 99 rbp - 40 p 2: rbp - 32 the Stack CS@VT Computer Organization I © 2005 -2015 Mc. Quain

x 86 -64 Assembly View Pointers in x 86 -64 7 . . .

x 86 -64 Assembly View Pointers in x 86 -64 7 . . . main: . . . # aa = *p 1 movq -8(%rbp), %rax # rax = *(rbp – 8) = p 1 movl (%rax), %eax # rax = *(*(rbp – 8) = *p 1 movl %eax, -20(%rbp) # aa = *(rbp – 20) = eax. . . rbp - old rbp 8 p 1: rbp - 28 rbp - 16 p 3: rpb - 40 rbp - 20 aa: rbp - 24 42 bb rbp – 28 x: 42 rbp – 32 y: 99 rbp - 40 p 2: rbp - 32 the Stack CS@VT Computer Organization I © 2005 -2015 Mc. Quain

x 86 -64 Assembly View Pointers in x 86 -64 8 . . .

x 86 -64 Assembly View Pointers in x 86 -64 8 . . . main: . . . # *p 1 = 10 movq -8(%rbp), %rax movl $10, (%rax). . . # rax = *(rbp – 4) = p 1 # *p 1 = *rax = 10 rbp - old rbp 8 p 1: rbp - 28 rbp - 16 p 3: rpb - 40 rbp - 20 aa: rbp - 24 42 bb rbp – 28 x: 10 rbp – 32 y: 99 rbp - 40 p 2: rbp - 32 the Stack CS@VT Computer Organization I © 2005 -2015 Mc. Quain

x 86 -64 Assembly View Pointers in x 86 -64 9 . . .

x 86 -64 Assembly View Pointers in x 86 -64 9 . . . main: . . . # bb = **p 3 movq -16(%rbp), %rax movq (%rax), %rax movl (%rax), %eax movl %eax, -24(%rbp). . . # # rax = *(rbp – 16) = p 3 rax = *(*(rbp – 16)) = *p 3 rax = *(*(*(rbp – 16) ) = y = **p 3 bb = **p 3 rbp - old rbp 8 p 1: rbp - 28 rbp - 16 p 3: 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 p 2: rbp - 32 the Stack © 2005 -2015 Mc. Quain

C and x 86 -64 Array Example Pointers in x 86 -64 10 uint

C and x 86 -64 Array Example Pointers in x 86 -64 10 uint 32_t Array. Example(int * array, uint 32_t size) { if (array == NULL) return 0; for (uint 32_t x = 0; x < size; x++) { array[x] = rand() % 1024; } return size; } CS@VT Computer Organization I © 2005 -2015 Mc. Quain

x 86 -64 Assembly View. . . Array. Example: . . . cmpq $0,

x 86 -64 Assembly View. . . Array. Example: . . . cmpq $0, -40(%rbp) jne. L 2 movl $0, %eax jmp. L 3. L 2: movl $0, -20(%rbp) jmp. L 4. L 5: # code snip. . L 4: movl -20(%rbp), %eax cmpl -44(%rbp), %eax jb. L 5 movl -44(%rbp), %eax. L 3: . . . CS@VT Pointers in x 86 -64 11 rbp - old rbp 4 rbp – 20 . . . loop variable x . . . rbp - 40 array rbp - 44 size Computer Organization I the Stack © 2005 -2015 Mc. Quain

x 86 -64 Assembly View Pointers in x 86 -64 12 . . .

x 86 -64 Assembly View Pointers in x 86 -64 12 . . . Array. Example: # 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. L 2 # 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. L 3 rbp - old rbp 4 rbp – 20 . . . loop variable x . . . rbp - 40 array rbp - 44 size the Stack CS@VT Computer Organization I © 2005 -2015 Mc. Quain

x 86 -64 Assembly View Pointers in x 86 -64 13 . . .

x 86 -64 Assembly View Pointers in x 86 -64 13 . . . # After the body, do the loop test. L 4: # 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. L 5 rbp - old rbp 4 rbp – 20 . . . loop variable x . . . rbp - 40 array rbp - 44 size the Stack CS@VT Computer Organization I © 2005 -2015 Mc. Quain

x 86 -64 Assembly View Pointers in x 86 -64 14 # The function

x 86 -64 Assembly View Pointers in x 86 -64 14 # The function body, after the if statement. . L 2: movl jmp $0, -20(%rbp). L 4 # initialize loop variable x # jump to the loop test. movl leaq -20(%rbp), %eax 0(, %rax, 4), %rdx # # # movq -40(%rbp), %rax # leaq (%rdx, %rax), %rbx # # snip. . . %eax stores the get x for this iteration rdx = 4*rax, i. e. x*sizeof(uint 32_t) get the pointer, array add array + x*sizeof(uint 32_t) giving us a pointer to the current element in the array result of rand() % 1024 movl %eax, (%rbx) # # # Remember: rbx is a pointer to the current array element. i. e. array + x*sizeof(uint 32_t) So we are storing the random number into array index x addl $1, -20(%rbp) # update the loop variable x . L 5: . L 4. . . CS@VT Computer Organization I © 2005 -2015 Mc. Quain

Aside: leal Pointers in x 86 -64 15 You also noticed another use of

Aside: leal Pointers in x 86 -64 15 You also noticed another use of the leal instruction: . . . leal 0(, %rax, 4), %rdx # rdx = 4*rax # i. e. x*sizeof(uint 32_t) . . . The particular form of the instruction used here is: leal Imm 1(src 1, src 2, Imm 2), dst = Imm 2*src 2 + src 1 + Imm 1 The execution of the instruction offers some additional performance advantages and is often used when indexing arrays, both for the “sizeof” calculation and to compute the pointer to the corresponding element. CS@VT Computer Organization I © 2005 -2015 Mc. Quain

Pointers as Parameters Pointers in x 86 -64 16 #include <stdint. h> int main()

Pointers as Parameters Pointers in x 86 -64 16 #include <stdint. h> int main() { uint 32_t X = 100; uint 32_t Y = 200; Swap(&X, &Y); return 0; } void Swap(uint 32_t* A, uint 32_t* B) { uint 32_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 Mc. Quain

x 86 -64 Assembly View Pointers in x 86 -64 17 . . .

x 86 -64 Assembly View Pointers in x 86 -64 17 . . . (frame for main) Swap: pushq movq subq movq movl movq movl leave ret. . . CS@VT %rbp %rsp, %rbp $32, %rsp %rdi, -24(%rbp) %rsi, -32(%rbp) -24(%rbp), %rax (%rax), %eax, -4(%rbp) -32(%rbp), %rax (%rax), %edx -24(%rbp), %rax %edx, (%rax) -32(%rbp), %rax -4(%rbp), %edx, (%rax) rbp + 8 X: 100 Y: 200 return address rbp old rbp - 4 rbp - 8 Temp: ? ? rbp - 12. . . rbp – 24 &X rbp – 32 &Y the Stack Computer Organization I © 2005 -2015 Mc. Quain