Assigning numbers to memory locations

Remember, a represents the address (location) of the storage unit and is also the name of the address 000055555555802c. Here (a) refers to the contents (number) stored at address a.

Assigning numbers in C and C++

In C and C++, a is a variable. We write the command to assign it a number, just like we would any variable, like so:

a = 1;

Assigning numbers in assembly language

In Intel assembly language, we write:

mov $1, a

Assigning numbers in GDB disassembly

In the GDB disassembly, we’ll see the following code, with the variable a and the address shown in the comment:

movl $0x1, 0x2ef2(%rip)      # 0x55555555802c <a>

The right column of the table below shows the translation of our pseudocode to the assembler language:

Notice that movl is used instead of mov. This is because a and b can point to 32-bit memory cells (such as %EAX or %EDX registers) and 64-bit memory cells (such as %RAX and %RDX registers). In the registers’ case, it’s clear from their names whether we use 6464-bit %RAX or 3232-bit %EAX. But in the case of memory addresses a and b, it is not clear whether they refer to 64-bit or 32-bit cells. We use movl to disambiguate and show that we are using 32-bit memory cells, which can hold integers from 00 to 42949672954294967295.

The 0x2ef2(%rip) address is how the compiler generates code to calculate the address of a instead of specifying it directly. Such code requires less memory space. Literal constants have $ as a prefix. Using the example of $0x1, the 0x prefix means the following number is hexadecimal. The leading zeroes of the address are also omitted in the comment. For example, the variable a has address 000055555555802c, which is represented as 0x55555555802c. Note that the movement direction is from left to right in both the disassembly output and the pseudocode.

After executing the first two assembly language instructions, we have the memory layout shown in the figure below.

Assigning numbers to registers

Assigning numbers to registers is similar to memory assignments. We can write, in pseudocode:

1 -> register
(a) -> register

Note that we do not use brackets when we refer to register contents. The second instruction above assigns (copies) the number at the location (address) a to a register.

Assigning numbers in assembly language

In assembly language, we write:

Press + to interact
mov $1, %eax # 1 is copied to the first half of %RAX register
mov $1, %rax # full contents of %RAX register are replaced with 1
mov a, %eax
mov a, %rax

Assigning numbers in GDB disassembly

In the GDB disassembly output, we can see the following code:

mov    $0x1, %eax