![]() |
C++ to masm conversion
Dear all
What could be the easiest source in masm for the below c++ code? For i=222222 to 999999 C1=i[1] C2=i[2] C3=i[3] C4=i[4] C5=i[5] C6=i[6] Calcul=C1^6+C2^6+C3^6+C3¨6+C4^6+C5^6+C6^6 if Calcul=serial then Msgbox("sérial" & i) Exit end if Next i |
Lol that is Visual Basic code not C++. So the carrot operator ^ represents exponentiation.
Exponentiation by a positive integer constant is best done by exponentuation by squaring most efficient by finding an addition chain which minimizes the number of multiplications and additions or subtractions. 6=4+2 or 6=8-2 will both work but 4+2 is more efficient So you use IMUL to multiply by itself once, store the square the multiply the result by itself again, and multiply those together to get the 6th power. Accumulate with ADD and use a counter register initialized to 6 which decreases with DEC each time though the loop and check loop exit condition with jump if zero JZ. This is a very basic assembly task if you've learned it. Addition chains are a more advanced topic if your exponents are huge e.g. in cryptography. There is no known efficient way to find them interestingly enough |
Thank you. But the issue is we need a six digit number for which the equals to the sum of each 6th square. There seems more math.
|
The below C++ code can solve the problem. But need to convert it in simple masm.
#include <stdio.h> int main(void) { int a,b,c,d,e,f; int n; for (a=2; a<=9; a++) for (b=2; b<=9; b++) for (c=2; c<=9; c++) for (d=2; d<=9; d++) for (e=2; e<=9; e++) for (f=2; f<=9; f++) { n = 100000*a + 10000*b + 1000*c + 100*d + 10*e +f; if (a*a*a*a*a*a + b*b*b*b*b*b + c*c*c*c*c*c + d*d*d*d*d*d + e*e*e*e*e*e + f*f*f*f*f*f == n) { printf("se! %i\n", n); return 0; } } } |
Might I suggest godbolt.org. Most here could handcraft this, but there us no need. How you can do it is using that website which shows the generated assembly code fir many C++ compilers, take your choice.
|
You may try gcc -S in order to get asm code
|
1 Attachment(s)
Hi,
Using MSVC to compile "C snippet" and generate masm listing code: Code:
.686pAttached source and (binary size 1KB) clean asm code easy to debug... External link: https://www.sendspace.com/file/olce5h |
Quote:
Hi insid3code Thanks. Can you explain a little more so that I can try my own, Thanks |
MSVC compiler can generate from your "c" snippet masm listing..
to generate this masm file: cl.exe /I"include" /D "WIN32" /X /c /O1 /Zl /Os /GF /EHsc /GS- /W4 /Gd /FAs /Fa"asmX86" "main.c" You can use the generated masm listing directly under MASM32. |
Anyone can support me compiling the below code in vb? I tried but not working.
For i=222222 to 999999 C1=i[1] C2=i[2] C3=i[3] C4=i[4] C5=i[5] C6=i[6] Calcul=C1^6+C2^6+C3^6+C3¨6+C4^6+C5^6+C6^6 if Calcul=serial then Msgbox("sérial" & i) Exit end if Next i |
try yourself https://www.onlinegdb.com/online_vb_compiler
|
Code:
.dataYou have to write your own logic for digit extraction and calling msgbox |
Zeocrack, your earlier example is wrong. Only the outer loop can go from 2 to 9. The inner loops must go from 0to 9. You've lost semantic equivalence
|
Quote:
The above code is not going to work. |
Quote:
|
| All times are GMT +8. The time now is 07:52. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX