Pages: [1]   Go Down
Author Topic: How many bytes in a memory address?  (Read 1329 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I need to memorize many times and in different locations a 4 bytes value. I was thinking of memorizing the address of that value using a pointer, how much memory will the address take?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can use the sizeof function to determine that answer for yourself.

Code:
Serial.print("int size: ");
Serial.println(sizeof(int));

Serial.print("int * size: ");
Serial.println(sizeof(int *));

Change int to byte, float, etc. to see the various type sizes.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17257
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That is an interesting question. I would have said that a C pointer would be be an unsigned integer in size, so two bytes?

However the newer Arduino Mega boards use a Mega1280 chip that has 128KB of program memory, so I'm not sure how the GCC compiler handles memory pointers for larger then 64KB of program memory. Anyone know?

Lefty
« Last Edit: December 05, 2009, 09:48:37 pm by retrolefty » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 197
Posts: 12737
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

A bit of research shows how little I know and how quickly I can be confused!

I built a little program for the Arduino Mega and dumped the assembly.  Code and data pointers all appear to be 16 bit.  It appears 16 bit pointers are used for small programs built for the Mega.

The Wikipedia articles claim that code pointers are 24 bits for the Mega processor.  There is an implication that data pointers are still 16 bit.

The Atmel documentation describes several new registers that are used to extend addresses past 16 bits.  The new registers appear to provide access to a "window" of memory.  Some of the instructions (like CALL) appear to work on word boundaries so they may be able to access the full 128K without a "window" register.  Even on the Mega processor, the instruction set appears to favor the first 64K (or a 64K window; I can't tell for certain).

Ugh!  Right now I'm VERY grateful to have a C++ compiler that hides those details!
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17257
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I too read a little of the datasheet for the mega1280. Seems that program memory addresses point to 16 bit words, as all AVR machine code instructions are either 16 or 32 bits wide.

So the program memory specification of 128KB is the same as 64K, 16 bit words, so a 16 bit address pointer (unsigned int) should cover all the flash memory of the mega1280. The next size larger Mega chip probably uses the extended address bit(s)? you mentioned to cover the larger program memory space they have.

So I guess we have come to the same conclusion, 16 bit pointers work fine for the mega1280 chip. Now I wonder if that's actually correct.  ;D

Lefty
« Last Edit: December 06, 2009, 02:25:46 am by retrolefty » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 197
Posts: 12737
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Now I wonder if that's actually correct
Disassembly addresses are always even.  As you said, machine instructions are always two or four bytes.  From the Atmel documentation: "The Program memory is organized in 16-bit words".  It all comes out even addresses.  I think you're right.

That's rather clever.  I'm starting to like those Atmel processors.   smiley
Logged

0
Offline Offline
Faraday Member
**
Karma: 23
Posts: 3469
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Aha, so you are saying one should not think of 128k Bytes but 64k Words? This is interesting. I think this knowledge is helpful smiley
Logged

Check out my experiments http://blog.blinkenlight.net

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17257
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Aha, so you are saying one should not think of 128k Bytes but 64k Words? This is interesting. I think this knowledge is helpful  

Yes for flash memory. Now data memory (SRAM) is byte based as it's separate memory and char type variables are just 8 bits wide. It's a function of the Harvard architecture (separate program and data memories) and the more standard Von neumann architecture where instructions and data share the same physical memory.



Lefty
« Last Edit: December 06, 2009, 11:47:17 am by retrolefty » Logged

Pages: [1]   Go Up
Jump to: