UP | HOME

Pointer assignment
Compiler Implementation
COP-3402

Pointer assignment in SimpleIR

Dereference assignment

*t1 := 11

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

Stack frame layout

function main
localvars a b x y t1 t2 t3
params a b
x := 8
y := 9
t1 := &x
t2 := *t1
*t1 := 11
t3 := x
return x

Stack frame diagram, same as prior lecture

Assigning to a dereferenced pointer

*t1 := 11

Stack frame, with addresses

  • What's the value of t1?
  • What's the value of *t1?

Following an address

What assembly instruction stores a value from to address?

Register indirect mov

mov %rbx, (%rax)

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

Computing the address

  • Get the value of the variable (the address)
  • Get the value of memory at that address
  • Store that the value of memory at that address
  • Add the offset (it's negative so it's equivalent to subtraction)
  • Store the result

Computing the address

(Diagram)

Assembly code

Assume the following offsets

Variable Offset
t1 -40

Assembly code

# *t1 := 11
mov -40(%rbp), %rax  # value of the deref'ed variable (t1)
mov $11, %rbx # value of the right-hand side (11)
mov %rbx, (%rax) # store the result in the address (*t1)

Author: Paul Gazzillo

Created: 2024-11-20 Wed 09:49

Validate