Copyright © Cay S. Horstmann 2011 Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.

Runtime Support for Scoping

Scopes in C

Memory Areas of a C Program

Run-Time Stack

Processor Architecture

i386 32-bit Registers

Register name Meaning
eax General purpose A
ebx General purpose B
ecx General purpose C
edx General purpose D
esi Source index
edi Destination index
esp Stack pointer
ebp Base pointer
eip Instruction pointer

AT&T Assembly Notation

Stack Allocation

Lab

???

Step 0. Software Installation

Step 1 - Reading Disassembly

  1. Where are the variables a and b allocated?
  2. How do you know?
  3. What are the assembly instructions for the statement int b = a + 10;

Step 2. Scopes

  1. Replace the printf statement with
      while (a < b)
      {
         int a = b;
         b--;
         {
            int b = a + 1;
            printf("%d\n", b);
         }
      }

    Where are the outer and inner a and b allocated?

  2. Turn the outer a into a global variable. How is a global variable accessed in assembly?

Step 3. Watching Registers and Memory

  1. What is the contents of the ebp register?
  2. Right-click on the ebp register, select Open Memory Window, then hit the up-arrow in the memory window a couple of times. What is the contents of -0xc(%ebp) before the movl instruction?
  3. Step over the movl instruction. What is the contents of -0xc(%ebp) now?