从汇编分析程序返回值错误的原因
cat a.c main.c;gcc main.c;./a.out;gcc -S main.c;nl main.s
int num(int i){
int ret;
// ret=0;
while (i)
{
/* code */
if (i&1==1)
{
/* code */
ret++;
}
i=i>>1;
}
return ret;
}
# include <stdio.h>
# include "a.c"
int main(){
for (int i = 0; i < 16; i++)
{
/* code */
printf("%d %d\n",i,num(i));
}
}
0 0
1 1
2 2
3 4
4 5
5 7
6 9
7 12
8 13
9 15
10 17
11 20
12 22
13 25
14 28
15 32
1 .file "main.c"
2 .text
3 .globl num
4 .type num, @function
5 num:
6 .LFB0:
7 .cfi_startproc
8 endbr64
9 pushq %rbp
10 .cfi_def_cfa_offset 16
11 .cfi_offset 6, -16
12 movq %rsp, %rbp
13 .cfi_def_cfa_register 6
14 movl %edi, -20(%rbp)
15 jmp .L2
16 .L4:
17 movl -20(%rbp), %eax
18 andl $1, %eax
19 testl %eax, %eax
20 je .L3
21 addl $1, -4(%rbp)
22 .L3:
23 sarl -20(%rbp)
24 .L2:
25 cmpl $0, -20(%rbp)
26 jne .L4
27 movl -4(%rbp), %eax
28 popq %rbp
29 .cfi_def_cfa 7, 8
30 ret
31 .cfi_endproc
32 .LFE0:
33 .size num, .-num
34 .section .rodata
35 .LC0:
36 .string "%d %d\n"
37 .text
38 .globl main
39 .type main, @function
40 main:
41 .LFB1:
42 .cfi_startproc
43 endbr64
44 pushq %rbp
45 .cfi_def_cfa_offset 16
46 .cfi_offset 6, -16
47 movq %rsp, %rbp
48 .cfi_def_cfa_register 6
49 subq $16, %rsp
50 movl $0, -4(%rbp)
51 jmp .L7
52 .L8:
53 movl -4(%rbp), %eax
54 movl %eax, %edi
55 call num
56 movl %eax, %edx
57 movl -4(%rbp), %eax
58 movl %eax, %esi
59 leaq .LC0(%rip), %rax
60 movq %rax, %rdi
61 movl $0, %eax
62 call printf@PLT
63 addl $1, -4(%rbp)
64 .L7:
65 cmpl $15, -4(%rbp)
66 jle .L8
67 movl $0, %eax
68 leave
69 .cfi_def_cfa 7, 8
70 ret
71 .cfi_endproc
72 .LFE1:
73 .size main, .-main
74 .ident "GCC: (Ubuntu 11.2.0-19ubuntu1) 11.2.0"
75 .section .note.GNU-stack,"",@progbits
76 .section .note.gnu.property,"a"
77 .align 8
78 .long 1f - 0f
79 .long 4f - 1f
80 .long 5
81 0:
82 .string "GNU"
83 1:
84 .align 8
85 .long 0xc0000002
86 .long 3f - 2f
87 2:
88 .long 0x3
89 3:
90 .align 8
91 4:
int num(int i){
int ret;
ret=0;
while (i)
{
/* code */
if (i&1==1)
{
/* code */
ret++;
}
i=i>>1;
}
return ret;
}
# include <stdio.h>
# include "a.c"
int main(){
for (int i = 0; i < 16; i++)
{
/* code */
printf("%d %d\n",i,num(i));
}
}
0 0
1 1
2 1
3 2
4 1
5 2
6 2
7 3
8 1
9 2
10 2
11 3
12 2
13 3
14 3
15 4
1 .file "main.c"
2 .text
3 .globl num
4 .type num, @function
5 num:
6 .LFB0:
7 .cfi_startproc
8 endbr64
9 pushq %rbp
10 .cfi_def_cfa_offset 16
11 .cfi_offset 6, -16
12 movq %rsp, %rbp
13 .cfi_def_cfa_register 6
14 movl %edi, -20(%rbp)
15 movl $0, -4(%rbp)
16 jmp .L2
17 .L4:
18 movl -20(%rbp), %eax
19 andl $1, %eax
20 testl %eax, %eax
21 je .L3
22 addl $1, -4(%rbp)
23 .L3:
24 sarl -20(%rbp)
25 .L2:
26 cmpl $0, -20(%rbp)
27 jne .L4
28 movl -4(%rbp), %eax
29 popq %rbp
30 .cfi_def_cfa 7, 8
31 ret
32 .cfi_endproc
33 .LFE0:
34 .size num, .-num
35 .section .rodata
36 .LC0:
37 .string "%d %d\n"
38 .text
39 .globl main
40 .type main, @function
41 main:
42 .LFB1:
43 .cfi_startproc
44 endbr64
45 pushq %rbp
46 .cfi_def_cfa_offset 16
47 .cfi_offset 6, -16
48 movq %rsp, %rbp
49 .cfi_def_cfa_register 6
50 subq $16, %rsp
51 movl $0, -4(%rbp)
52 jmp .L7
53 .L8:
54 movl -4(%rbp), %eax
55 movl %eax, %edi
56 call num
57 movl %eax, %edx
58 movl -4(%rbp), %eax
59 movl %eax, %esi
60 leaq .LC0(%rip), %rax
61 movq %rax, %rdi
62 movl $0, %eax
63 call printf@PLT
64 addl $1, -4(%rbp)
65 .L7:
66 cmpl $15, -4(%rbp)
67 jle .L8
68 movl $0, %eax
69 leave
70 .cfi_def_cfa 7, 8
71 ret
72 .cfi_endproc
73 .LFE1:
74 .size main, .-main
75 .ident "GCC: (Ubuntu 11.2.0-19ubuntu1) 11.2.0"
76 .section .note.GNU-stack,"",@progbits
77 .section .note.gnu.property,"a"
78 .align 8
79 .long 1f - 0f
80 .long 4f - 1f
81 .long 5
82 0:
83 .string "GNU"
84 1:
85 .align 8
86 .long 0xc0000002
87 .long 3f - 2f
88 2:
89 .long 0x3
90 3:
91 .align 8
92 4: