Search⌘ K

A Memory Trace

Explore how simple C code accesses memory in a paging system. Learn to trace virtual memory accesses through assembly instructions, page table translations, and physical memory addresses to grasp paging's practical effects.

Memory accesses in array.c

Before closing, we now trace through a simple memory access example to demonstrate all of the resulting memory accesses that occur when using paging. The code snippet (in C, in a file called array.c) that we are interested in is as follows:

int array[1000];
...
for (i = 0; i < 1000; i++)
     array[i] = 0;

We compile array.c and run it with the following commands:

prompt> gcc -o array array.c -Wall -O
prompt> ./array

Assembly code

Of course, to truly understand what memory accesses this code snippet (which simply initializes an array) will make, we’ll have to know (or assume) a few more things. First, we’ll have to disassemble the resulting binary (using objdump on Linux, or otool on a Mac) to see what assembly instructions are used to initialize the array in a loop. Here is the resulting assembly code:

1024 movl $0x0,(%edi,%eax,4)
1028 incl %eax
1032 cmpl $0x03e8,%eax
1036 jne  0x1024
...