2021-02-11

水仙花数

例题:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个“水仙花数”,因为153等于1的三次方+5的三次方+3的三次方。

#include<stdio.h>
#include<stdlib.h>

int main()
{
	int i, j, k, n;
	printf("打印出的所有水仙花数分别为:");
	for (n = 100; n<1000; n++)
	{
		i = n / 100;/*分解出百位*/
		j = n / 10 % 10;/*分解出十位*/
		k = n % 10;/*分解出个位*/
		if (i * 100 + j * 10 + k == i*i*i + j*j*j + k*k*k)
		{
			printf("%d\n", n);
		}
	}
	printf("\n");
	system("pause");
	return 0;
}

输出:
2021-02-11
汇编
int main()
{
00401010 push ebp
00401011 mov ebp,esp
00401013 sub esp,50h
00401016 push ebx
00401017 push esi
00401018 push edi
00401019 lea edi,[ebp-50h]
0040101C mov ecx,14h
00401021 mov eax,0CCCCCCCCh
00401026 rep stos dword ptr [edi]
int i, j, k, n;
printf(“打印出的所有水仙花数分别为:”);
00401028 push offset string “\xb4\xf2\xd3\xa1\xb3\xf6\xb5\xc4\xcb\xf9\xd3\xd0\xcb\xae\xcf\xc9\xbb\xa8
0040102D call printf (00401260)
00401032 add esp,4
for (n = 100; n<1000; n++)
00401035 mov dword ptr [ebp-10h],64h
0040103C jmp main+37h (00401047)
0040103E mov eax,dword ptr [ebp-10h]
00401041 add eax,1
00401044 mov dword ptr [ebp-10h],eax
00401047 cmp dword ptr [ebp-10h],3E8h
0040104E jge main+0C8h (004010d8)
{
i = n / 100;/分解出百位/
00401054 mov eax,dword ptr [ebp-10h]
00401057 cdq
00401058 mov ecx,64h
0040105D idiv eax,ecx
0040105F mov dword ptr [ebp-4],eax
j = n / 10 % 10;/分解出十位/
00401062 mov eax,dword ptr [ebp-10h]
00401065 cdq
00401066 mov ecx,0Ah
0040106B idiv eax,ecx
0040106D cdq
0040106E mov ecx,0Ah
00401073 idiv eax,ecx
00401075 mov dword ptr [ebp-8],edx
k = n % 10;/分解出个位/
00401078 mov eax,dword ptr [ebp-10h]
0040107B cdq
0040107C mov ecx,0Ah
00401081 idiv eax,ecx
00401083 mov dword ptr [ebp-0Ch],edx
if (i * 100 + j * 10 + k == iii + jjj + kkk)
00401086 mov edx,dword ptr [ebp-4]
00401089 imul edx,edx,64h
0040108C mov eax,dword ptr [ebp-8]
0040108F imul eax,eax,0Ah
00401092 mov ecx,dword ptr [ebp-0Ch]
00401095 add ecx,edx
00401097 add eax,ecx
00401099 mov edx,dword ptr [ebp-4]
0040109C imul edx,dword ptr [ebp-4]
004010A0 imul edx,dword ptr [ebp-4]
004010A4 mov ecx,dword ptr [ebp-8]
004010A7 imul ecx,dword ptr [ebp-8]
004010AB imul ecx,dword ptr [ebp-8]
004010AF add edx,ecx
004010B1 mov ecx,dword ptr [ebp-0Ch]
004010B4 imul ecx,dword ptr [ebp-0Ch]
004010B8 imul ecx,dword ptr [ebp-0Ch]
004010BC add edx,ecx
004010BE cmp eax,edx
004010C0 jne main+0C3h (004010d3)
{
printf(”%d\n", n);
004010C2 mov edx,dword ptr [ebp-10h]
004010C5 push edx
004010C6 push offset string “%d\n” (00424028)
004010CB call printf (00401260)
004010D0 add esp,8
}
}
004010D3 jmp main+2Eh (0040103e)
printf("\n");
004010D8 push offset string “\n” (00424024)
004010DD call printf (00401260)
004010E2 add esp,4
system(“pause”);
004010E5 push offset string “pause” (0042401c)
004010EA call system (00401150)
004010EF add esp,4
return 0;
004010F2 xor eax,eax
}
004010F4 pop edi
004010F5 pop esi
004010F6 pop ebx
004010F7 add esp,50h
004010FA cmp ebp,esp
004010FC call __chkesp (004012e0)
00401101 mov esp,ebp
00401103 pop ebp
00401104 ret

2021/2/11

上一篇:内核项目内存管理-页表创建布局


下一篇:硬编码01