本文发表在 rolia.net 枫下论坛I supposed the sum should be 2, and aa[] should be in the stack, aa[20] should be equal to the value of variable defined just above it(not below it because of stack), which is pad=2 here. I am pretty sure it happened in my work before.
But unfortunately! I got the different result this morning when I ran it on my new notebook
which is ppc CPU and linux runing on it. the thing become complicated. basically, the value should
be consistant with the compiler, but in this case , the aa[] is separated from other variables which make me confessed now!
thanks for you answer! it's pretty good!
the following is the runing result and disassembly code
display how the compiler allocate the memory for automatic variables.
debian:~/src/tmp$ gcc -g -o tmp tmp.c
debian:~/src/tmp$ ./tmp
sum=0X7ffffb30
debian:~/src/tmp$ gcc -v
Reading specs from /usr/lib/gcc-lib/powerpc-linux/2.95.4/specs
gcc version 2.95.4 20011002 (Debian prerelease)
debian:~/src/tmp$ !objdu
objdump -D -S ./tmp > disa
00004a0: 4e 80 00 20 blr
100004a4 <main>:
#include "stdio.h"
#include "string.h"
main()
{
100004a4: 94 21 ff d0 stwu r1,-48(r1)
100004a8: 7c 08 02 a6 mflr r0
100004ac: 93 41 00 18 stw r26,24(r1)
100004b0: 93 61 00 1c stw r27,28(r1)
100004b4: 93 81 00 20 stw r28,32(r1)
100004b8: 93 a1 00 24 stw r29,36(r1)
100004bc: 93 c1 00 28 stw r30,40(r1)
100004c0: 93 e1 00 2c stw r31,44(r1)
100004c4: 90 01 00 34 stw r0,52(r1)
100004c8: 7c 3f 0b 78 mr r31,r1
int MAX_LEN = 20;
100004cc: 38 00 00 14 li r0,20
100004d0: 90 1f 00 08 stw r0,8(r31)
int pad = 2 ;
100004d4: 38 00 00 02 li r0,2
100004d8: 90 1f 00 0c stw r0,12(r31)
100004dc: 81 3f 00 08 lwz r9,8(r31)
100004e0: 3b a9 ff ff addi r29,r9,-1
100004e4: 3b 9d 00 01 addi r28,r29,1
100004e8: 7f 80 e3 78 mr r0,r28
100004ec: 54 09 28 34 rlwinm r9,r0,5,0,26
100004f0: 7d 3c 4b 78 mr r28,r9
100004f4: 3b 7d 00 01 addi r27,r29,1
100004f8: 7f 60 db 78 mr r0,r27
100004fc: 54 09 10 3a rlwinm r9,r0,2,0,29
10000500: 7d 3b 4b 78 mr r27,r9
10000504: 57 89 e8 fe rlwinm r9,r28,29,3,31
10000508: 39 69 00 0f addi r11,r9,15
1000050c: 38 0b 00 07 addi r0,r11,7
10000510: 54 09 e8 fe rlwinm r9,r0,29,3,31
10000514: 7d 20 4b 78 mr r0,r9
10000518: 54 09 18 38 rlwinm r9,r0,3,0,28
1000051c: 80 01 00 00 lwz r0,0(r1)
10000520: 7d 29 00 d0 neg r9,r9
10000524: 7c 01 49 6e stwux r0,r1,r9
10000528: 39 21 00 08 addi r9,r1,8
1000052c: 38 09 00 0f addi r0,r9,15
10000530: 54 09 e1 3e rlwinm r9,r0,28,4,31
10000534: 7d 20 4b 78 mr r0,r9
10000538: 54 1a 20 36 rlwinm r26,r0,4,0,27
int aa[MAX_LEN] ;
int sum = 0;
1000053c: 38 00 00 00 li r0,0
10000540: 90 1f 00 10 stw r0,16(r31)
int i=20;
10000544: 38 00 00 14 li r0,20
10000548: 90 1f 00 14 stw r0,20(r31)
/*
* just set aa[] to 0 ;
*/
bzero(aa, sizeof(int)*MAX_LEN);
1000054c: 80 1f 00 08 lwz r0,8(r31)
10000550: 7c 09 03 78 mr r9,r0
10000554: 55 20 10 3a rlwinm r0,r9,2,0,29
10000558: 7f 43 d3 78 mr r3,r26
1000055c: 7c 04 03 78 mr r4,r0
10000560: 48 01 02 9d bl 100107fc <__bss_start+0x68>
aa[0] = 0;
10000564: 38 00 00 00 li r0,0
10000568: 90 1a 00 00 stw r0,0(r26)
while (i)
1000056c: 80 1f 00 14 lwz r0,20(r31)
10000570: 2c 00 00 00 cmpwi r0,0
10000574: 40 82 00 08 bne- 1000057c <main+0xd8>
10000578: 48 00 00 34 b 100005ac <main+0x108>
sum +=aa[i--];
1000057c: 39 3f 00 14 addi r9,r31,20
10000580: 81 69 00 00 lwz r11,0(r9)
10000584: 7d 60 5b 78 mr r0,r11
10000588: 7c 0a 03 78 mr r10,r0
1000058c: 55 40 10 3a rlwinm r0,r10,2,0,29
10000590: 81 5f 00 10 lwz r10,16(r31)
10000594: 7c 1a 00 2e lwzx r0,r26,r0
10000598: 7d 4a 02 14 add r10,r10,r0
1000059c: 91 5f 00 10 stw r10,16(r31)
100005a0: 39 6b ff ff addi r11,r11,-1
100005a4: 91 69 00 00 stw r11,0(r9)
100005a8: 4b ff ff c4 b 1000056c <main+0xc8>
printf("sum=0X%x\n",sum);
100005ac: 3d 20 10 00 lis r9,4096
100005b0: 38 69 06 8c addi r3,r9,1676
100005b4: 80 9f 00 10 lwz r4,16(r31)
100005b8: 4c c6 31 82 crclr 4*cr1+eq
100005bc: 48 01 02 39 bl 100107f4 <__bss_start+0x60>
}
100005c0: 81 61 00 00 lwz r11,0(r1)
100005c4: 80 0b 00 04 lwz r0,4(r11)
100005c8: 7c 08 03 a6 mtlr r0
100005cc: 83 4b ff e8 lwz r26,-24(r11)
100005d0: 83 6b ff ec lwz r27,-20(r11)
100005d4: 83 8b ff f0 lwz r28,-16(r11)
100005d8: 83 ab ff f4 lwz r29,-12(r11)
100005dc: 83 cb ff f8 lwz r30,-8(r11)
100005e0: 83 eb ff fc lwz r31,-4(r11)
100005e4: 7d 61 5b 78 mr r1,r11
100005e8: 4e 80 00 20 blr
100005ec <__do_global_ctors_aux>:
100005ec: 94 21 ff f0 stwu r1,-16(r1)
100005f0: 7c 08 02 a6 mflr r0
100005f4: 93 e1 00 0c stw r31,12(r1)
100005f8: 90 01 00 14 stw r0,20(r1)
100005fc: 3d 20 10 01 lis r9,4097
10000600: 80 09 07 70 lwz r0,1904(r9)
10000604: 3b e9 07 70 addi r31,r9,1904
10000608: 2c 00 ff ff cmpwi r0,-1
1000060c: 41 82 00 1c beq- 10000628 <__do_global_ctors_aux+0x3c>
10000610: 81 3f 00 00 lwz r9,0(r31)
10000614: 7d 28 03 a6 mtlr r9
10000618: 4e 80 00 21 blrl
1000061c: 84 1f ff fc lwzu r0,-4(r31)
10000620: 2c 00 ff ff cmpwi r0,-1
10000624: 40 82 ff ec bne+ 10000610 <__do_global_ctors_aux+0x24>
10000628: 80 01 00 14 lwz r0,20(r1)
1000062c: 7c 08 03 a6 mtlr r0
10000630: 83 e1 00 0c lwz r31,12(r1)
10000634: 38 21 00 10 addi r1,r1,16
10000638: 4e 80 00 20 blr更多精彩文章及讨论,请光临枫下论坛 rolia.net
But unfortunately! I got the different result this morning when I ran it on my new notebook
which is ppc CPU and linux runing on it. the thing become complicated. basically, the value should
be consistant with the compiler, but in this case , the aa[] is separated from other variables which make me confessed now!
thanks for you answer! it's pretty good!
the following is the runing result and disassembly code
display how the compiler allocate the memory for automatic variables.
debian:~/src/tmp$ gcc -g -o tmp tmp.c
debian:~/src/tmp$ ./tmp
sum=0X7ffffb30
debian:~/src/tmp$ gcc -v
Reading specs from /usr/lib/gcc-lib/powerpc-linux/2.95.4/specs
gcc version 2.95.4 20011002 (Debian prerelease)
debian:~/src/tmp$ !objdu
objdump -D -S ./tmp > disa
00004a0: 4e 80 00 20 blr
100004a4 <main>:
#include "stdio.h"
#include "string.h"
main()
{
100004a4: 94 21 ff d0 stwu r1,-48(r1)
100004a8: 7c 08 02 a6 mflr r0
100004ac: 93 41 00 18 stw r26,24(r1)
100004b0: 93 61 00 1c stw r27,28(r1)
100004b4: 93 81 00 20 stw r28,32(r1)
100004b8: 93 a1 00 24 stw r29,36(r1)
100004bc: 93 c1 00 28 stw r30,40(r1)
100004c0: 93 e1 00 2c stw r31,44(r1)
100004c4: 90 01 00 34 stw r0,52(r1)
100004c8: 7c 3f 0b 78 mr r31,r1
int MAX_LEN = 20;
100004cc: 38 00 00 14 li r0,20
100004d0: 90 1f 00 08 stw r0,8(r31)
int pad = 2 ;
100004d4: 38 00 00 02 li r0,2
100004d8: 90 1f 00 0c stw r0,12(r31)
100004dc: 81 3f 00 08 lwz r9,8(r31)
100004e0: 3b a9 ff ff addi r29,r9,-1
100004e4: 3b 9d 00 01 addi r28,r29,1
100004e8: 7f 80 e3 78 mr r0,r28
100004ec: 54 09 28 34 rlwinm r9,r0,5,0,26
100004f0: 7d 3c 4b 78 mr r28,r9
100004f4: 3b 7d 00 01 addi r27,r29,1
100004f8: 7f 60 db 78 mr r0,r27
100004fc: 54 09 10 3a rlwinm r9,r0,2,0,29
10000500: 7d 3b 4b 78 mr r27,r9
10000504: 57 89 e8 fe rlwinm r9,r28,29,3,31
10000508: 39 69 00 0f addi r11,r9,15
1000050c: 38 0b 00 07 addi r0,r11,7
10000510: 54 09 e8 fe rlwinm r9,r0,29,3,31
10000514: 7d 20 4b 78 mr r0,r9
10000518: 54 09 18 38 rlwinm r9,r0,3,0,28
1000051c: 80 01 00 00 lwz r0,0(r1)
10000520: 7d 29 00 d0 neg r9,r9
10000524: 7c 01 49 6e stwux r0,r1,r9
10000528: 39 21 00 08 addi r9,r1,8
1000052c: 38 09 00 0f addi r0,r9,15
10000530: 54 09 e1 3e rlwinm r9,r0,28,4,31
10000534: 7d 20 4b 78 mr r0,r9
10000538: 54 1a 20 36 rlwinm r26,r0,4,0,27
int aa[MAX_LEN] ;
int sum = 0;
1000053c: 38 00 00 00 li r0,0
10000540: 90 1f 00 10 stw r0,16(r31)
int i=20;
10000544: 38 00 00 14 li r0,20
10000548: 90 1f 00 14 stw r0,20(r31)
/*
* just set aa[] to 0 ;
*/
bzero(aa, sizeof(int)*MAX_LEN);
1000054c: 80 1f 00 08 lwz r0,8(r31)
10000550: 7c 09 03 78 mr r9,r0
10000554: 55 20 10 3a rlwinm r0,r9,2,0,29
10000558: 7f 43 d3 78 mr r3,r26
1000055c: 7c 04 03 78 mr r4,r0
10000560: 48 01 02 9d bl 100107fc <__bss_start+0x68>
aa[0] = 0;
10000564: 38 00 00 00 li r0,0
10000568: 90 1a 00 00 stw r0,0(r26)
while (i)
1000056c: 80 1f 00 14 lwz r0,20(r31)
10000570: 2c 00 00 00 cmpwi r0,0
10000574: 40 82 00 08 bne- 1000057c <main+0xd8>
10000578: 48 00 00 34 b 100005ac <main+0x108>
sum +=aa[i--];
1000057c: 39 3f 00 14 addi r9,r31,20
10000580: 81 69 00 00 lwz r11,0(r9)
10000584: 7d 60 5b 78 mr r0,r11
10000588: 7c 0a 03 78 mr r10,r0
1000058c: 55 40 10 3a rlwinm r0,r10,2,0,29
10000590: 81 5f 00 10 lwz r10,16(r31)
10000594: 7c 1a 00 2e lwzx r0,r26,r0
10000598: 7d 4a 02 14 add r10,r10,r0
1000059c: 91 5f 00 10 stw r10,16(r31)
100005a0: 39 6b ff ff addi r11,r11,-1
100005a4: 91 69 00 00 stw r11,0(r9)
100005a8: 4b ff ff c4 b 1000056c <main+0xc8>
printf("sum=0X%x\n",sum);
100005ac: 3d 20 10 00 lis r9,4096
100005b0: 38 69 06 8c addi r3,r9,1676
100005b4: 80 9f 00 10 lwz r4,16(r31)
100005b8: 4c c6 31 82 crclr 4*cr1+eq
100005bc: 48 01 02 39 bl 100107f4 <__bss_start+0x60>
}
100005c0: 81 61 00 00 lwz r11,0(r1)
100005c4: 80 0b 00 04 lwz r0,4(r11)
100005c8: 7c 08 03 a6 mtlr r0
100005cc: 83 4b ff e8 lwz r26,-24(r11)
100005d0: 83 6b ff ec lwz r27,-20(r11)
100005d4: 83 8b ff f0 lwz r28,-16(r11)
100005d8: 83 ab ff f4 lwz r29,-12(r11)
100005dc: 83 cb ff f8 lwz r30,-8(r11)
100005e0: 83 eb ff fc lwz r31,-4(r11)
100005e4: 7d 61 5b 78 mr r1,r11
100005e8: 4e 80 00 20 blr
100005ec <__do_global_ctors_aux>:
100005ec: 94 21 ff f0 stwu r1,-16(r1)
100005f0: 7c 08 02 a6 mflr r0
100005f4: 93 e1 00 0c stw r31,12(r1)
100005f8: 90 01 00 14 stw r0,20(r1)
100005fc: 3d 20 10 01 lis r9,4097
10000600: 80 09 07 70 lwz r0,1904(r9)
10000604: 3b e9 07 70 addi r31,r9,1904
10000608: 2c 00 ff ff cmpwi r0,-1
1000060c: 41 82 00 1c beq- 10000628 <__do_global_ctors_aux+0x3c>
10000610: 81 3f 00 00 lwz r9,0(r31)
10000614: 7d 28 03 a6 mtlr r9
10000618: 4e 80 00 21 blrl
1000061c: 84 1f ff fc lwzu r0,-4(r31)
10000620: 2c 00 ff ff cmpwi r0,-1
10000624: 40 82 ff ec bne+ 10000610 <__do_global_ctors_aux+0x24>
10000628: 80 01 00 14 lwz r0,20(r1)
1000062c: 7c 08 03 a6 mtlr r0
10000630: 83 e1 00 0c lwz r31,12(r1)
10000634: 38 21 00 10 addi r1,r1,16
10000638: 4e 80 00 20 blr更多精彩文章及讨论,请光临枫下论坛 rolia.net