不确定这是否是为您消耗周期的实际分配我相信这是消耗周期的分配
例如在这个 t1.c
#include <stdio.h>
int main(void)
{
int i;
size_t u;
for (i = 0; i < 10; i++) {
printf("i = %d, u = %zu\n", i, u);
}
return 0;
}
以及 t1.c 的组装
.file "t1.c"
.section .rodata
.LC0:
.string "i = %d, u = %zu\n"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $32, %esp
movl $0, 24(%esp)
jmp .L2
.L3:
movl $.LC0, %eax
movl 28(%esp), %edx
movl %edx, 8(%esp)
movl 24(%esp), %edx
movl %edx, 4(%esp)
movl %eax, (%esp)
call printf
addl $1, 24(%esp)
.L2:
cmpl $9, 24(%esp)
jle .L3
movl $0, %eax
leave
ret
.size main, .-main
.ident "GCC: (GNU) 4.4.6 20110731 (Red Hat 4.4.6-3)"
.section .note.GNU-stack,"",@progbits
在上述情况下,暂时没有任何任务
第二种情况 t2.c
#include <stdio.h>
int main(void)
{
int i;
size_t u;
for (i = 0; i < 10; i++) {
i = (size_t) u;
printf("i = %d, u = %zu\n", i, u);
}
return 0;
}
以及随后的组装
.file "t2.c"
.section .rodata
.LC0:
.string "i = %d, u = %zu\n"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $32, %esp
movl $0, 24(%esp)
jmp .L2
.L3:
movl 28(%esp), %eax
movl %eax, 24(%esp)
movl $.LC0, %eax
movl 28(%esp), %edx
movl %edx, 8(%esp)
movl 24(%esp), %edx
movl %edx, 4(%esp)
movl %eax, (%esp)
call printf
addl $1, 24(%esp)
.L2:
cmpl $9, 24(%esp)
jle .L3
movl $0, %eax
leave
ret
.size main, .-main
.ident "GCC: (GNU) 4.4.6 20110731 (Red Hat 4.4.6-3)"
.section .note.GNU-stack,"",@progbits
检查上面的陈述
movl 28(%esp), %eax
movl %eax, 24(%esp)
现在是最后一个例子 t3.c
#include <stdio.h>
int main(void)
{
int i;
int u;
for (i = 0; i < 10; i++) {
i = u;
printf("i = %d, u = %zu\n", i, u);
}
return 0;
}
以及随后的组装
.file "t3.c"
.section .rodata
.LC0:
.string "i = %d, u = %zu\n"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $32, %esp
movl $0, 24(%esp)
jmp .L2
.L3:
movl 28(%esp), %eax
movl %eax, 24(%esp)
movl $.LC0, %eax
movl 28(%esp), %edx
movl %edx, 8(%esp)
movl 24(%esp), %edx
movl %edx, 4(%esp)
movl %eax, (%esp)
call printf
addl $1, 24(%esp)
.L2:
cmpl $9, 24(%esp)
jle .L3
movl $0, %eax
leave
ret
.size main, .-main
.ident "GCC: (GNU) 4.4.6 20110731 (Red Hat 4.4.6-3)"
.section .note.GNU-stack,"",@progbits
现在您可以观察 t2 和 t3 并在此处看到差异,但确实因拱门而异