當前位置:編程學習大全網 - 編程語言 - 在c語言(C++或G++)中如何嵌入匯編

在c語言(C++或G++)中如何嵌入匯編

今天有點時間,重新改下了下,為避免因編譯器和平臺實現而出現的問題,我寫了三個版本,分別是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;

}

  • 上一篇:怎麽解開金山毒霸正在保護妳的電腦?
  • 下一篇:為什麽1G光模塊顯示為1.25G?
  • copyright 2024編程學習大全網