...
/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++:
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:
.global _start.dataa: .int 0b: .int 0pa: .quad 0pb: .quad b.textmain:_start:lea a, %raxmov %rax, pamov pa, %raxmovl $1, (%rax)mov pb, %rbxmovl $1, (%rbx)mov (%rax), %ecxadd (%rbx), %ecxmov %ecx, (%rbx)mov $0x3c, %raxmov $0, %rdisyscall
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.
as MemoryPointers.asm -o MemoryPointers.old MemoryPointers.o -o MemoryPointersgdb ./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 ...