Course Homepage

Pointers

Pointer operations in Slang

Reference

t1 = &x

Store the address of x (not the value) in t1.

Dereference

t2 = *t1

t1 is an address. Take the value of memory at the address and store it in t2.

Dereference assignment

*t1 = 11

t1 is an address. Store the value 11 at that address.

Complete example

begin main(a, b)
locals x, y, t1, t2, t3
x = 8
y = 9
t1 = &x
t2 = *t1
*t1 = 11
t3 = x
return x
end

What is the value of x that gets returned?

Local variables

Recall that local variables and parameters are stored at offsets from %rbp

Symbol table

begin main(a, b)
locals x, y, t1, t2, t3
Variable Offset
x -16
y -24
t1 -32
t2 -40
t3 -48
a -56
b -64
mov -16(%rbp), %rax  # Load x into %rax

Memory layout

(Diagram.)

Assignment

How does assignment work in assembly?

Use an offset from %rbp

mov $8, -32(%rbp)

Getting (referencing) a pointer

t1 = &x

Stack frame, with addresses

How do we get the address?

Computing the address

Does this involve the value of the address in anyway?

A little easier to see in when looking at the stack frame

Computing the address

(Diagram.)

Assembly code

Assume the following offsets (see the symbol table)

Variable Offset
x -16
t1 -32

Assembly code

mov %rbp, %rax      # Start with stack frame address
add $-16, %rax      # Add the offset of the referenced var (x)
mov %rax, -32(%rbp) # Store the result in the assigned var (t1)

(Diagram.)

Dereferencing a pointer

t2 = *t1

What’s the value of t2? - If t1 = 260 - If t1 = 260 and address 260 holds the value 10?

Following an address

What assembly instructions loads a value from an address?

Register indirect mov

mov (%rax), %rbx

(%rax) here means load the value at the address given in %rax.

Computing the address

Computing the address

(Diagram.)

Assembly code

Assume the following offsets

Variable Offset
x -16
t1 -32
t2 -40

Assembly code

t2 = *t1
mov -32(%rbp), %rax # Value of t1 (address)
mov (%rax), %rbx    # Deref address in %rax (*t1)
mov %rbx, -40(%rbp) # Store result in t2

Notice how the value of x is the same as the value of *t1. Why is that?

Show on stack frame why this is the case.

Pointer assignment in Slang

Dereference assignment

*t1 = 11

t1 is an address. Store the value 11 at that address.

Stack frame layout

begin main(a, b)
locals x, y, t1, t2, t3
x = 8
y = 9
t1 = &x
t2 = *t1
*t1 = 11
t3 = x
return x
end

Stack frame diagram, same as prior lecture

Assigning to a dereferenced pointer

*t1 = 11

Stack frame, with addresses

Following an address

What assembly instruction stores a value from a register to address?

Register indirect mov

mov %rbx, (%rax)

(%rax) here means store the value at the address given in %rax.

Computing the address

Computing the address

(Diagram.)

Symbol table

Assembly code

*t1 = 11              // Let t1's offset be -32
mov -32(%rbp),  %rax    # Value of t1 (address)
mov $11,        %rbx    # Right-hand-side (11)
mov %rbx,       (%rax)  # Store the result in *t1

Complete example

Slang

begin main(a, b)
locals x, y, t1, t2, t3
x = 8
y = 9
t1 = &x
t2 = *t1
*t1 = 11
t3 = x
return x
end

Assembly

    .file "stdin"
    .section .note.GNU-stack,"",@progbits
    .text
    .globl main
    .type main, @function
main:
    # Prologue, ypdate stack pointer
    pushq   %rbp        # Save old base ponter
    movq    %rsp, %rbp  # Set new base pointer
    push    %rbx        # Save old %rbx
    sub $56, %rsp       # Allocate stack space for locals
    mov %rdi, -56(%rbp) # Move parameter a to local variable
    mov %rsi, -64(%rbp) # Move parameter b to local variable

    # x = 8
    mov $8, %rax
    mov %rax, -16(%rbp)

    # y = 9
    mov $9, %rax
    mov %rax, -24(%rbp)

    # t1 = &x
    mov %rbp, %rax
    add $-16, %rax
    mov %rax, -32(%rbp)

    # t2 = *t1
    mov -32(%rbp), %rax
    mov (%rax), %rbx
    mov %rbx, -40(%rbp)

    # t1 = 11
    mov -32(%rbp), %rax
    mov $11, %rbx
    mov %rbx, (%rax)

    # t3 = x
    mov -16(%rbp), %rax
    mov %rax, -48(%rbp)

    # Set return value
    mov -16(%rbp), %rax

    # Epilogue
    add $56, %rsp       # Deallocate stack space for locals
    pop %rbx            # Restore %rbx
    pop %rbp            # Restore old base pointer
    ret                 # Return

Diagram

(Diagram.)

Coding Templates

# Reference
mov %rbp, %rax
add $OPERAND_OFFSET, %rax
mov $rax, DESTINATION_OFFSET(%rbp)

# Dereference
mov OPERAND_OFFSET(%rbp), %rax
mov (%rax), %rbx
mov %rbx DESTINATION_OFFSET(%rbp)

# Derefence assignment
mov DESTINATION_OFFSET(%rbp), %rax
mov OPERAND, %rbx
mov $rbx, ($rax)