谢谢大家对我的耐心。我只需要把头绕在我正在看的东西上。为缓慢而道歉,只是试图学习和理解。
在重新阅读了每个人的评论并查看了几次代码后,我意识到我的问题指向了错误的方向。我
我会用简单的组装说明替换这两行吗?还是我必须做类似的事情asm()
。我想这就是我感到困惑的地方。一旦我知道了这一点,我想我会从那里得到它。
编辑
我没有意识到我从帖子中省略了这个。显然是最重要的部分,感谢 Oli 指出。
目标是用内联汇编代码替换附加程序中 for 循环内的两行代码。您必须从内联汇编代码中获得相同的输出。(执行相同的结果,循环次数相同)
C :
#include "stdlib.h"
#include "time.h"
int main (int argc, char* argv[])
{
int num1 = 10;
int num2 = 27;
int sum = 0;
int cases = 0;
int loose = 0;
float time1 = 0;
float time2 = 0;
float time = 0;
int i = 0;
sum = num1 + num2;
asm("xor %%eax,%%eax;"
"mov %1,%%eax;"
"add %2,%%eax;"
"mov %%eax,%0;"
:"=r"(sum) /* outputs */
:"r"(num1),"r"(num2) /* input */
:"%eax"); /*clobber list*/
printf("The sum is %d \n",sum);
time1 = clock();
for (i = 0; i<1000000000; i++)
{
cases = i/num1;
loose = i%num1;
}
printf("The number of cases are %d \n",cases);
printf("The number of loose items are %d \n",loose);
time2 = clock();
time = (time2 - time1) / CLOCKS_PER_SEC;
printf("The elapsed time is %f seconds \n", time);
system("pause");
return 0;
}
集会 :
.file "inlineAsm.c"
.def ___main; .scl 2; .type 32; .endef
.section .rdata,"dr"
LC1:
.ascii "The sum is %d \12\0"
LC2:
.ascii "The number of cases are %d \12\0"
.align 4
LC3:
.ascii "The number of loose items are %d \12\0"
.align 4
LC5:
.ascii "The elapsed time is %f seconds \12\0"
LC6:
.ascii "pause\0"
.align 4
LC4:
.long 1148846080
.text
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl %esp, %ebp
subl $56, %esp
andl $-16, %esp
movl $0, %eax
addl $15, %eax
addl $15, %eax
shrl $4, %eax
sall $4, %eax
movl %eax, -40(%ebp)
movl -40(%ebp), %eax
call __alloca
call ___main
movl $10, -4(%ebp)
movl $27, -8(%ebp)
movl $0, -12(%ebp)
movl $0, -16(%ebp)
movl $0, -20(%ebp)
movl $0x00000000, %eax
movl %eax, -24(%ebp)
movl $0x00000000, %eax
movl %eax, -28(%ebp)
movl $0x00000000, %eax
movl %eax, -32(%ebp)
movl $0, -36(%ebp)
movl -8(%ebp), %eax
addl -4(%ebp), %eax
movl %eax, -12(%ebp)
movl -4(%ebp), %ecx
movl -8(%ebp), %edx
/APP
xor %eax,%eax;mov %ecx,%eax;add %edx,%eax;mov %eax,%edx;
/NO_APP
movl %edx, %eax
movl %eax, -12(%ebp)
movl -12(%ebp), %eax
movl %eax, 4(%esp)
movl $LC1, (%esp)
call _printf
call _clock
pushl %eax
fildl (%esp)
leal 4(%esp), %esp
fstps -24(%ebp)
movl $0, -36(%ebp)
L2:
cmpl $999999999, -36(%ebp)
jg L3
movl -36(%ebp), %edx
leal -4(%ebp), %ecx
movl %ecx, -40(%ebp)
movl %edx, %eax
movl -40(%ebp), %ecx
cltd
idivl (%ecx)
movl %eax, -40(%ebp)
movl -40(%ebp), %eax
movl %eax, -16(%ebp)
movl -36(%ebp), %edx
leal -4(%ebp), %ecx
movl %ecx, -40(%ebp)
movl %edx, %eax
movl -40(%ebp), %ecx
cltd
idivl (%ecx)
movl %edx, -20(%ebp)
leal -36(%ebp), %eax
incl (%eax)
jmp L2
L3:
movl -16(%ebp), %eax
movl %eax, 4(%esp)
movl $LC2, (%esp)
call _printf
movl -20(%ebp), %eax
movl %eax, 4(%esp)
movl $LC3, (%esp)
call _printf
call _clock
pushl %eax
fildl (%esp)
leal 4(%esp), %esp
fstps -28(%ebp)
flds -28(%ebp)
fsubs -24(%ebp)
flds LC4
fdivrp %st, %st(1)
fstps -32(%ebp)
flds -32(%ebp)
fstpl 4(%esp)
movl $LC5, (%esp)
call _printf
movl $LC6, (%esp)
call _system
movl $0, %eax
leave
ret
.def _system ;.scl 3; .type 32; .endef
.def _clock ;.scl 3; .type 32; .endef
.def _printf ;.scl 3; .type 32; .endef
输出 :
/*
The sum is 37
The number of cases are 99999999
The number of loose items are 9
The elapsed time is 9.359000 seconds
Press any key to continue . . .
*/