Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

tutorial memory

Tianshu Huang edited this page Oct 7, 2018 · 13 revisions

Memory

Computer memory is organized as a continuous block of addresses index by an integer. These integers are typically 32-bit (for 32-bit systems such as ARM Cortex M4 and x86) or 64-bit (for 64 bit systems such as x86-64).

Description Address Range
Flash ROM 0x0000.0000 - 0x0003FFFF
(empty) 0x0004.0000 - 0x1FFF.FFFF
RAM 0x2000.0000 - 0x2000.7FFF
(empty) 0x2000.8000 - 0x3FFF.FFFF
I/O Ports 0x4000.0000 - 0x400F.FFFF
(empty) 0x4010.0000 - 0xDFFF.FFFF
Internal I/O 0xE000.0000 - 0xE004.1FFF

As you can see, the same set of 32-bit addresses are used by not just the RAM and ROM, but also by I/O. This is called memory mapped I/O.

Pointers

A pointer is an integer that specifies a location in memory. Since the TM4C is a 32-bit system, pointers on the TM4C are 32-bit integers that specify a memory location.

In C, we use * to indicate pointer dereferencing, and & to fetch the pointer to a variable.

For example, consider the following code:

int x;
x = 10;
int *y;
y = &x;
int **z;
z = &y;

Let's look at the memory. To simplify the diagram, assume we are looking only at the last 8 bits of the memory address.

Address Contents Description
0xF0 0x00 x (second 8 bits)
0xF1 0x01 x (first 8 bits)
0xF2 ... y (bits 24-31)
0xF3 ... y (bits 16-23)
0xF4 ... y (bits 8-15)
0xF5 0xF0 y (bits 0-7)
0xF6 ... z (bits 24-31)
0xF7 ... z (bits 16-23)
0xF8 ... z (bits 8-15)
0xF9 0xF2 z (bits 0-7)

First we declare an integer x. This is stored somewhere in memory. Here, we've put it into a memory address ending in 0xF0.

Then, we declare *y. Since * indicates a pointer, int *y means that we want ```y`` to be a pointer to an int.

Next, y is set to &x. & refers to the location of a variable; therefore, we are putting the location of x into y. This gives y the value of 0x______F0.

Now, we declare int **z. int *z would be a pointer to an integer; however we have 2 *s! This can be interpreted as int *(*z); that is, if *z is a pointer to an integer, **z is a pointer to a pointer to an integer.

When we set z = &y, this means that z points to y. This now sets z to 0x______F2.

The Stack

This section under construction

The Heap

Segmentation Faults

A segmentation fault occurs when a program attempts to access memory that isn't owned by it. For example, by convention, the memory address 0x0 is always owned by the operating system; if you attempt to dereference 0x0, you'll get a segmentation fault.

The TM4C has no operating system; you can access whatever you want. However, it does have many memory addresses that simply don't exist.

For example, if you attempt to access 0x4000.0000, this memory address doesn't exist, and will create a hard fault. In order to handle this, a fault handler branches to an infinite loop.

Clone this wiki locally