We all know that local variables are stored in the stack during the C language operation, and the space is allocated from high to low.
However, recently I encountered a program that made me a little confused.
First look at a program.
Obviously, addresses are allocated from high to low, just as expected.
Modify it a little and then run it.
Obviously, from low to high !!!
Clarify the problem: the stack should change the memory allocation mode for the memory occupied by local variables.
Why? Why? Why?
Use-s to generate assembly language.
Assembly language in the first case
.file"main.c".section.rodata.LC0:.string"Address s = Ox%x\n".LC1:.string"Address d = Ox%x\n".text.globlmain.typemain, @functionmain:.LFB0:.cfi_startprocpushl%ebp.cfi_def_cfa_offset 8.cfi_offset 5, -8movl%esp, %ebp.cfi_def_cfa_register 5andl$-16, %espsubl$32, %espmovl%gs:20, %eaxmovl%eax, 28(%esp)xorl%eax, %eaxmovl$6513249, 24(%esp)movw$25185, 21(%esp)movb$0, 23(%esp)leal24(%esp), %eaxmovl%eax, 4(%esp)movl$.LC0, (%esp)callprintfleal21(%esp), %eaxmovl%eax, 4(%esp)movl$.LC1, (%esp)callprintfmovl$1, %eaxmovl28(%esp), %edxxorl%gs:20, %edxje.L3call__stack_chk_fail.L3:leave.cfi_restore 5.cfi_def_cfa 4, 4ret.cfi_endproc.LFE0:.sizemain, .-main.ident"GCC: (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3".section.note.GNU-stack,"",@progbits
Assembly Language in Case 2
.file"main.c".section.rodata.LC0:.string"Address s = Ox%x\n".LC1:.string"Address d = Ox%x\n".text.globlmain.typemain, @functionmain:.LFB0:.cfi_startprocpushl%ebp.cfi_def_cfa_offset 8.cfi_offset 5, -8movl%esp, %ebp.cfi_def_cfa_register 5andl$-16, %espsubl$32, %espmovl%gs:20, %eaxmovl%eax, 28(%esp)xorl%eax, %eaxmovl$6513249, 17(%esp)movl$1684234849, 21(%esp)movw$26213, 25(%esp)movb$0, 27(%esp)leal17(%esp), %eaxmovl%eax, 4(%esp)movl$.LC0, (%esp)callprintfleal21(%esp), %eaxmovl%eax, 4(%esp)movl$.LC1, (%esp)callprintfmovl$1, %eaxmovl28(%esp), %edxxorl%gs:20, %edxje.L3call__stack_chk_fail.L3:leave.cfi_restore 5.cfi_def_cfa 4, 4ret.cfi_endproc.LFE0:.sizemain, .-main.ident"GCC: (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3".section.note.GNU-stack,"",@progbits
There are obvious differences in the previous several mov statements. One is distribution from low to high, and the other is distribution from high to low.
Conjecture: the compiler optimizes the language to make the long string advanced stack.
But why?
Please answer.