(asm is in AT&T syntax, operands are reversed from intel and (%eax) means [eax], the rest is hopefully easyish to pick up). No optimizations are on.
int main(int argc,char** argv){ const char* str1 = "Hello, world"; char str2[] = "Hello again, world";}
gives
gcc2_compiled.:___gnu_compiled_c: .def ___main; .scl 2; .type 32; .endef.textLC0: .ascii "Hello, world\0"LC1: .ascii "Hello again, world\0" .align 4.globl _main .def _main; .scl 2; .type 32; .endef_main: pushl %ebp movl %esp,%ebp subl $64,%esp pushl %edi pushl %esi call ___main movl $LC0,-4(%ebp) leal -48(%ebp),%eax leal -48(%ebp),%edi movl $LC1,%esi cld movl $4,%ecx rep movsl movsw movsbL2: leal -72(%ebp),%esp popl %esi popl %edi leave ret
Note the two strings are defined in the code (text) segment, does exactly the same without the const qualifier.
Then, trying to modify these:
int main(int argc,char** argv){ char* str1 = "Hello, world"; char str2[] = "Hello again, world"; str1[0] = 'J';}
Crashes, asm source
.file "str1.c"gcc2_compiled.:___gnu_compiled_c: .def ___main; .scl 2; .type 32; .endef.textLC0: .ascii "Hello, world\0"LC1: .ascii "Hello again, world\0" .align 4.globl _main .def _main; .scl 2; .type 32; .endef_main: pushl %ebp movl %esp,%ebp subl $64,%esp pushl %edi pushl %esi call ___main movl $LC0,-4(%ebp) leal -48(%ebp),%eax leal -48(%ebp),%edi movl $LC1,%esi cld movl $4,%ecx rep movsl movsw movsb movl -4(%ebp),%eax movb $74,(%eax) # eax points to string (pointer is stored on stack at [ebp - 4] earlier)L2: leal -72(%ebp),%esp popl %esi popl %edi leave ret
And attempting to modify str2:
int main(int argc,char** argv){ char* str1 = "Hello, world"; char str2[] = "Hello again, world"; str2[0] = 'J';}
Asm:
.file "str1.c"gcc2_compiled.:___gnu_compiled_c: .def ___main; .scl 2; .type 32; .endef.textLC0: .ascii "Hello, world\0"LC1: .ascii "Hello again, world\0" .align 4.globl _main .def _main; .scl 2; .type 32; .endef_main: pushl %ebp movl %esp,%ebp subl $64,%esp pushl %edi pushl %esi call ___main movl $LC0,-4(%ebp) leal -48(%ebp),%eax leal -48(%ebp),%edi movl $LC1,%esi cld movl $4,%ecx rep movsl movsw movsb movb $74,-48(%ebp)L2: leal -72(%ebp),%esp popl %esi popl %edi leave ret
EDIT: tags
[edited by - JuNC on June 9, 2002 9:53:47 AM]