...

/

Memory Pointers Project: Memory Layout

Memory Pointers Project: Memory Layout

Learn how to use pointers to manipulate memory.

Memory pointers in C/C++

We have the following data declaration and definition in C/C++:

Press + to interact
int a, b;
int *pa, *pb = &b;

Memory pointers in assembly language

The project code corresponds to the following pseudocode and assembly language instructions.

Source code

The source code of memory pointers is following:

Press + to interact
.global _start
.data
a: .int 0
b: .int 0
pa: .quad 0
pb: .quad b
.text
main:
_start:
lea a, %rax
mov %rax, pa
mov pa, %rax
movl $1, (%rax)
mov pb, %rbx
movl $1, (%rbx)
mov (%rax), %ecx
add (%rbx), %ecx
mov %ecx, (%rbx)
mov $0x3c, %rax
mov $0, %rdi
syscall

Memory pointers in GDB disassembly

Our memory pointers project is described in detail below.

Compile and execute GDB commands

First, we need to compile and link the course code and load the executable into GDB. We get to the GDB environment with the following commands.

Press + to interact
as MemoryPointers.asm -o MemoryPointers.o
ld MemoryPointers.o -o MemoryPointers
gdb ./MemoryPointers

Note: You can practice all the commands in the coding playground provided at the end of the lesson.

After running the above commands, the output should be as follows:

We put a breakpoint in the main function, run the program until GDB breaks in, and then disassemble the main function:

break main

A breakpoint is shown below:

Now run the program:

set disable-randomization off
run

The output after execution is given below:

Now we disassemble the ...