1

Can anyone please let me know how to deterimine the number of address lines in a processor using C ? This can't be equal to size of the processor registers since the number of address lines may be different compared to the size of registers (for example in the 8086 the address lines are 20 while size of registers is 16).

Similarly, can we find the word size of RAM (meaning the size of each addressable location) through a C program?

AShelly
  • 34,686
  • 15
  • 91
  • 152
mezda
  • 3,537
  • 6
  • 30
  • 37
  • By "word size", do you mean the size of a DDR burst? – Oliver Charlesworth Mar 19 '12 at 18:42
  • What is a ddr burst, i dont know that. by word size i mean one storage location that is uniquely addressable. – mezda Mar 19 '12 at 18:50
  • I'm trying to understand what it is that you're actually asking. Specifically, what you mean by "one storage location that is uniquely addressable". As far as the CPU is concerned, memory is uniquely addressable at the byte (char) level. – Oliver Charlesworth Mar 19 '12 at 18:51
  • see if we have say 32 address lines, then we can address 2^32 storage locations and if the size of each storage location (which i am referring as word here) is 1 byte, then using 32 address lines we are able to address 4gigabytes of memory. on the other hand, if each storage location is 2bytes say, then we can address 2*4gigabytes of memory. so in these two cases, the word size is one byte or two bytes. please let me know if i am still not clear – mezda Mar 19 '12 at 18:57
  • 1
    Ok. Then no, there's probably no way of obtaining this information (except perhaps on specific platforms). It's also not a well-defined problem; many platforms have a hierarchical memory system (L1, L2, L3 cache), each with a differently-sized addressable unit. And then you have systems with more than one address space (Harvard architectures, for example). – Oliver Charlesworth Mar 19 '12 at 19:03
  • @OliCharlesworth : this is a new thing, i havn't read this. can you point me to some links which can help me understand this better ? thanks. – mezda Mar 19 '12 at 19:18
  • 1
    I suggest reading http://en.wikipedia.org/wiki/CPU_cache and http://en.wikipedia.org/wiki/Harvard_architecture. – Oliver Charlesworth Mar 19 '12 at 20:09

2 Answers2

3

The short answer to pretty much all of these is that (at least in a portable way that's really defined by C itself) you can't.

Address lines, for example, is a particularly tough one. Just for example, quite a DSPs have two or even three completely separate address spaces connected to an equal number of physical memory channels -- and each will potentially have a unique size. Along with that, even if there are N address lines on the processor, there may (and often will) be less memory than that actually connected.

That said, sizeof(void *) * CHAR_BIT will usually give at least a reasonable approximation of the number of address lines -- at least the number theoretically allowed by the architecture, though the amount of addressable memory may well differ.

The standard says int is supposed to be the "natural size suggested by the processor". More often than not (but definitely not always) that's the same as the size of the processors integer registers.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Thanks for the reply, I have one more doubt on this. in C each byte is individually addressable. But if we have the word size of RAM to be that of int as you pointed (ie the word size if either 2 or 4 in general), then that means that each word (either 2 or 4 bytes) is individually addressable rather than each byte. Can you please clarify this, where i am getting wrong in understanding this. thanks – mezda Mar 19 '12 at 18:41
  • @user1182722: There really is no answer to that. According to C, you have to be able to address individual `char`s, but whether that's directly supported by the hardware is open to question -- it is on most current machines, but definitely hasn't always been (e.g., early Crays addressed memory only in 64-bit words -- a `char *` had a memory address in the top bits, and an offset within the word in the bottom bits). – Jerry Coffin Mar 19 '12 at 18:44
  • thanks for the reply, but i am not able to exactly understand this. would it be possible for you to find some time to explain this a little bit. it will be a great help. thanks a lot. – mezda Mar 19 '12 at 18:48
0

As far as I know the answer to both questions is implementation defined. There's no standardized concepts of these things in the C language, hence there's no standard code to write the tests. You may be able to guess them, but that doesn't make your code portable. A specific compiler suite may provide these facilities, but again that won't make your code portable.

ldav1s
  • 15,885
  • 2
  • 53
  • 56