-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEuler4WithInlineX86Assembly.cpp
More file actions
140 lines (98 loc) · 1.8 KB
/
Euler4WithInlineX86Assembly.cpp
File metadata and controls
140 lines (98 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// AsmTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int IsPal(int n){
int digits[100];
int i = 0;
int isPal = 1;
_asm {
start:
cmp n,0
je check_palindrome
//edx:eax/ebx --> eax = result, edx = remainder
mov eax,n
xor edx,edx
mov ebx,10
div ebx // eax has result, edx has the next digit
mov n,eax
//digits[i] = edx
mov esi,i
lea eax, digits
mov [eax+4*esi],edx
inc esi
mov i,esi
jmp start
check_palindrome :
//squeeze equality
mov eax,i
dec eax
mov ebx,0
loop_palindrome:
cmp eax, ebx
jle end
mov ecx, digits[eax*4]
mov edx, digits[ebx*4]
//if ecx!=edx return false
cmp ecx,edx
jne falsify
dec eax
inc ebx
jmp loop_palindrome
falsify:
mov isPal,0
end:
}
return isPal;
}
int Euler4(){
int largest = 0;
_asm{
//for eax in 100..999, for ebx in 100..999
mov eax, 100
mov ebx, 100
loop_eax:
cmp eax,1000
jge end
loop_ebx:
cmp ebx,1000
jge end_loop_ebx
//ecx = eax; eax=eax*ebx
mov ecx,eax
mul ebx
xchg eax,ecx //now eax is as before, ecx = eax*ebx
//first save registers, then use ecx as arg
push eax
push ebx
push ecx
push ecx
call IsPal
//restore registers, set result to edx
mov edx,eax
//pop once to pop pushed argument (due to CDECL), once to start popping stored arguments
pop ecx
pop ecx
pop ebx
pop eax
//not pal, continue
cmp edx,1
jne not_largest_pal
//not largest, continue
cmp ecx,largest
jle not_largest_pal
mov largest,ecx
not_largest_pal:
inc ebx
jmp loop_ebx
end_loop_ebx:
mov ebx,100
inc eax
jmp loop_eax
end:
}
return largest;
}
int _tmain(int argc, _TCHAR* argv[])
{
printf("%d", Euler4());
return 0;
}