>If I understand correctly, char s1[]= "Hello"; puts that array of characters
>(plus the NULL) on the stack. Could I then execute something like
>printf("%s", s1) and have it print Hello? If true, when that printf runs,
>how does it know which bytes to pull off the stack. I might have added
>additional things to the stack, so how does it know where to find it if
>there is no pointer to it? If I could understand that, I'd really be getting
>somewhere. Thanks.
With CW, I think that static strings are in one of 2 places:
- along with the globals for the app (which are just in a dynamic heap
chunk which the StartupCode sets up for you when your app launches)
- Embedded in the code as 'PC Relative Strings' which basically means that
they are in the CODE segment of your app right along with the instructions,
and thus on Palm don't take up extra space during runtime.
The best way to understand what's going on is to look at the assembly.
Here's a little test I ran:
static void g()
{
char ell[] = "....";
WinDrawChars(ell, 4, 15, 15);
}
Hunk: Kind=HUNK_LOCAL_CODE Name="g__Fv"(305) Size=54
00000000: 4E56 FFFA link a6,#-6
00000004: 41FA 002A lea *+44,a0 ; 0x00000030
00000008: 2D50 FFFA move.l (a0),-6(a6)
0000000C: 1D68 0004 FFFE move.b 4(a0),-2(a6)
00000012: 3F3C 000F move.w #15,-(a7)
00000016: 2F3C 0004 000F move.l #262159,-(a7)
0000001C: 486E FFFA pea -6(a6)
00000020: 4E4F trap #15
00000022: A220 sysTrapWinDrawChars
00000024: 4E5E unlk a6
00000026: 4E75 rts
00000028: 8567 5F5F 4676 dc.b 0x85,'g__Fv'
0000002E: 0006 2E2E 2E2E
00000034: 0000
You can see by the 1st assembly line that there is only space made for 6
bytes on the stack by link. Since I have PC relative strings on by compiler
option, the string "...." is located at offset 0x30 in this function. You
can see at that spot 0x2E2E2E2E00 which is "....". Then the address of this
string constant is loaded into a0. Then what's pointed to by a0 is moved
onto the stack, via a move.l (4bytes) and a move.b (1 byte). That's 4 dots
and a NULL.
As for your question about how it knows WHICH bytes to pull off the
stack... the pea -6(a6) will push onto the stack the ADDRESS of the
beginning of the "...." string which is on the stack. I am not that good at
asm. so I'm not exactly sure what's happening, but I can tell that the
compiler loads char ell[] onto the stack and then passes it around by
passing the beginning address of the string.
Alan Pinstein
Synergy Solutions, Inc.
http://www.synsolutions.com
1-800-210-5293