How are memory location addresses actually stored in the cpu?

0

Time and again, I've seen a representation of memory locations which is depicted as linear array of rectangular boxes with their respective memory addresses to their left side. Now, these addresses are generally written as 2000, 2004, 2008...etc. (for a 32 bit machine, I suppose).

According to said system, the memory locations are just plain integers. But when I print out addresses in C/C++, they are displayed as an alphanumeric string. Plus, if they were actually just integers, wouldn't that be kind of problematic, since a register (say R1) containing a numeric value like 2000 could suddenly act as a pointer to a memory location just by enclosing it in parentheses? Like this: (R1) - >this would point to the memory location 2000

(btw that was assembly language)

So how exactly are memory addresses stored in memory?

Anchith Acharya

Posted 2019-09-10T07:54:59.683

Reputation: 3

Answers

1

According to said system, the memory locations are just plain integers.

Yes, because they are just numbers.

But when I print out addresses in C/C++, they are displayed as an alphanumeric string.

You fail to mention what radix (or number base) these addresses are displayed in.
If the address was expressed in base 2, then there would be just 0s and 1s.
Back in the day octal representation was common, so an address could be represented with digits 0 through 7.
If the address was expressed in base 16, then the hexadecimal digits include 0 through 9 as well as the letters A through F. Is this the "alphanumeric string" you are referring to?

Plus, if they were actually just integers, wouldn't that be kind of problematic, since a register (say R1) containing a numeric value like 2000 could suddenly act as a pointer to a memory location just by enclosing it in parentheses? Like this: (R1) - >this would point to the memory location 2000

Coding an instruction or statement incorrectly (aka bugs) is always a problem in programming.
But memory address are just like integers.
That is how pointer arithmetic and calculations for table/structure displacements are accomplished.

A high-level programming language such as C will have distinct variable types to distinguish between a pointer and an unsigned int or unsigned long, but when the data types have the same storage size, there is no actual difference in the binary structure. The restrictions on usage and conversion are merely to implement programming order and avoid potential bugs.
At the CPU level, any pointer (or memory address) can be treated as an integer, and any integer can be treated as a memory address. They are all just numbers.

Bottom line: the CPU only knows how to process numbers. Everything has to be converted into (binary) numbers in order for a computer to process such "information".

So how exactly are memory addresses stored in memory?

A "memory address", aka pointer, is stored as a numeric value just like an integer (of the appropriate size).
Your premise that memory addresses are not numbers/integers is simply incorrect.

sawdust

Posted 2019-09-10T07:54:59.683

Reputation: 14 697

Thank you! I get it now. – Anchith Acharya – 2019-09-11T11:39:36.373

1

According to said system, the memory locations are just plain integers. But when I print out addresses in C/C++, they are displayed as an alphanumeric string.

Probably because you use something like %X in format string, rendering it as an hexadecimal number. Using %d will print adresses in decimal.

Plus, if they were actually just integers, wouldn't that be kind of problematic, since a register (say R1) containing a numeric value like 2000 could suddenly act as a pointer to a memory location just by enclosing it in parentheses? Like this: (R1) - >this would point to the memory location 2000

True. In C language, nothing prevent you that kind of misusage. It's programmer responsability to manage this.

(btw that was assembly language). So how exactly are memory addresses stored in memory?

In modern systems, memory addresses are usualy "virtual", mostly managed by MMU (Memory Management Unit) which is a hardware component dedicated to this task.

binarym

Posted 2019-09-10T07:54:59.683

Reputation: 320

It's not even always "misusage". Fixed memory addresses are certainly unusual in modern PC environments, but it feels like it would be a completely normal thing to do when programming for embedded devices or MS-DOS-like systems... – user1686 – 2019-09-10T09:09:48.687

Thanks for the explanation! – Anchith Acharya – 2019-09-11T11:39:17.287

0

Memory addresses can be absolute or can be offsets, with respect to a base address.

Memory addresses in a program are represented in hexadecimal format, by a variable name or label, etc. In assembly the address can be stored in a register so it can be referred as the content of the register, for example, (R1) is the address stored in register 1. Check this out: https://en.wikibooks.org/wiki/X86_Assembly/X86_Architecture#Segment_Registers.

It is important to distinguish between a physical address and a virtual memory address, the concept of paging and how the operating system system calls for memory management work, in a particular architecture.

It is necessary also to know the concept of memory segmentation, and the different segment types, the concept of shared memory, dynamic vs statically linked libraries, and so on.

Depending on the architecture, the length and data type to a memory position (what we call a pointer) will vary.

Finally, there are cases where the actual memory space is larger than what the bus address directions can address, and a switch of the memory "bank" is required, this is why the concept of far pointer was introduced. See difference between near pointers and far pointers.

So in a nutshell, the question is too open as it is formulated.

Jose Manuel Gomez Alvarez

Posted 2019-09-10T07:54:59.683

Reputation: 126