今天有点时间,重新改下了下,为避免因编译器和平台实现而出现的问题,我写了三个版本,分别是windows下vc6.0,windows下mingw和cygwin和linux下的gcc/g++。
vc6.0:
#include <stdio.h>
const char* input = "%d";
const char* output = "%d\n";
int n;
int main()
{
__asm
{
lea eax, n
push eax
push input
loopx:
call scanf
cmp eax, 1
jne end
mov ecx, n
jecxz end
dec ecx
push ecx
push output
call printf
add esp, 8
jmp loopx
end:
add esp, 8
}
return 0;
}
mingw/cygwin:
#include <stdio.h>
const char* input = "%d";
const char* output = "%d\n";
int n;
int main()
{
__asm__
(
"loop: \n"
"pushl $_n \n"
"pushl _input \n"
"call _scanf \n"
"addl $8, %esp \n"
"cmpl $1, %eax \n"
"jne end \n"
"movl _n, %ecx \n"
"jecxz end \n"
"decl %ecx \n"
"pushl %ecx \n"
"pushl _output \n"
"call _printf \n"
"addl $8, %esp \n"
"jmp loop \n"
"end:"
);
return 0;
}
linux gcc/g++:
#include <stdio.h>
const char* input = "%d";
const char* output = "%d\n";
int n;
int main()
{
__asm__
(
"pushl $n \n"
"pushl input \n"
"loop: \n"
"call scanf \n"
"cmp $1, %eax \n"
"jne end \n"
"movl n, %ecx \n"
"jecxz end \n"
"decl %ecx \n"
"pushl %ecx \n"
"pushl output \n"
"call printf \n"
"addl $8, %esp \n"
"jmp loop \n"
"end: \n"
"addl $8, %esp \n");
return 0;
}
温馨提示:内容为网友见解,仅供参考