我们在开始写汇编代码的时候,一般是先写出C语言的实现,然后再翻译成汇编的实现。
在这个过程中,我们还可以利用IDE的反汇编功能查看编译器是如何帮我们生成汇编代码的,以及思考编译器为什么这样做,搞懂了这些,不论是对我们的C语言还是汇编语言都有很大的提升。
C语言代码实现如下:
#include <stdio.h>
int main()
{
int sum = 0;
int count = 100;
do
{
sum = sum + count;
count = count - 1;
} while (count != 0);
printf("%d\n", sum);
return 0;
}
汇编中寄存器对应C中变量关系如下:
eax==sum
ecx==count
汇编代码实现如下:
INCLUDELIB kernel32.lib
INCLUDELIB ucrt.lib
INCLUDELIB legacy_stdio_definitions.lib
.386
.model flat,stdcall
ExitProcess PROTO,
dwExitCode:DWORD
printf PROTO C : dword,:vararg
scanf PROTO C : dword,:vararg
.data
format byte 'the sum is : %d',10