RE: Base Address's, etc...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Dan Erickson wrote:
> int x;
> x = 5;
> 
>     X is just a place in memory that we have named x for human
> readability purposes. Now x (the place in memory we have named x) 
> holds the value five. How does it accomplish this??

Dan,

The following glosses over LOTS of detail, but it hits the high spots.

When the compiler processes "int x;", it reserves a location in memory to
hold any value given to variable x. The address of the location in memory is
encoded as a binary number. 

The binary equivalents of the first few natural numbers are:

1 = 0000 0000 0000 0001
2 = 0000 0000 0000 0010
3 = 0000 0000 0000 0011
4 = 0000 0000 0000 0100
5 = 0000 0000 0000 0101

The spaces are just for clarity. Each digit represents one bit. Each bit
position represents twice the value of the bit to its right, just as a
decimal digit represents ten times the value of the digit to its right,
e.g.. 1, 10, 100.

The length of memory reserved is determined by the type of the variable, in
this case an "int" which I will say for the example is two bytes long (16
bits). Generally, for modern machines, the granularity of a memory address
is one byte, or 8 bits. 

Let us say that the compiler reserves memory location 4 for a length of 2
bytes. 

The compiler processes "x = 5", it first codes the "5" constant into the
corresponding binary value 0000 0000 0000 0101. 

The compiler then generates code for the assignment operation. There are
many variations of this part. For the sake of simplicity of the example, if
we imagine a machine that can execute English words and has a single
hardware register to load memory data into, and to store back to memory, we
might get the following instructions for "x = 5;":

	LOAD_IMMEDIATE_VALUE	0000 0000 0000 0101	# load constant "5"
	STORE_AT_ADDRESS    	0000 0000 0000 0100	# store to address
"4"

When the program gets run, the CPU pushes the bits around the hardware as it
is told to by these instructions, which themselves are stored in memory. The
transistors of the address decoder in the memory subsystem route the value
"5" so only the two byte locations indexed by "4" are changed when the CPU
stores the value. 

That's basically what happens when any computer assigns a value to a
variable. Did that make sense or do you want to go in a different direction?


When learning, the first step is always the hardest.

Regards,
Ed Vance

---------------------------------------------------------------- 
Ed Vance              edv@macrolink.com
Macrolink, Inc.       1500 N. Kellogg Dr  Anaheim, CA  92807
----------------------------------------------------------------

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux