Ok so I have my final assignment for an Assembly class and our objective is to find a Pythagorean triple using an algorithm. There's a typical MSB3721 error that prevents it from compiling but all the numbers I see on the output screen are weird ones like +4384199725.
Here's my code:
INCLUDE Irvine32.inc
.data
U DWORD ? ; variable U
V DWORD ? ; variable V
GCD DWORD
.code
main PROC
mov U, 2 ; moves 2 to location U
if FirstDo:
mov eax, U ; moves memory location U to eax
TEST eax, 1 ; checks for an ODD value
jnz oddp ; if not zero, jump
jmp evenp ; jumps to evenp
oddp: mov V, 2
mov ebx, V
jmp endofF1
evenp: mov V, 1
mov ebx, V
endofF1:
jb secondDO:
call GCD ; executes instruction GCD
cmp eax, 1 ; compares to 1
je Equal1
mov eax, U
mov ebx, V
jmp goDo2
Equal1:
mov eax, U
mov ebx, V
call TRIPLE ; executes instruction triple
mov eax, U
mov ebx, V
goDo2: add ebx, 2
mov V, ebx
cmp ebx, eax
add eax, 1
mov U, eax
cmp U, 10
jb FirstDo
exit
main ENDP
END main
;;;;;;;;;;;;;;;;;;
GCD PROC uses ebx
whileLoop: cmp ebx, 0
jbe out1
mov edx, 0
div ebx
mov eax, edx
xchg eax, edx
jmp whileLoop
out1:
ret
GCD ENDP
;;;;;;;;;;;;;;;;;
TRIPLE PROC
push eax
push ebx
imul eax, eax
imul ebx, ebx
sub eax, ebx
mov ecx, eax
pop ebx
pop eax
push eax
push ebx
imul eax, ebx
add eax, eax
mov edx, eax
pop ebx
pop eax
push eax
push ebx
imul eax, eax
imul ebx, ebx
add eax, ebx
mov esi, eax
pop ebx
pop eax
cmp ecx, edx
jb endIf2
xchg ecx, edx
endIf2:
mov eax, ecx
call WriteInt
mov eax, edx
call WriteInt
mov eax, esi
call WriteInt
call crlf
ret
exit
TRIPLE ENDP
END TRIPLE
GCD DWORD is not correct syntax I believe.
It should be GCD proto :dword
and I'm not sure if the "if FirstDo:" is correct syntax because of the space
.if ecx>5
dec ecx
.endif
there is a list of "expression operators" in the masm manual
Quoteobjective is to find a Pythagorean triple using an algorithm
This may seem harsh but take it in a positive manner.
Before you ever start programming anything scientific, you MUST fully understand the principles behind the objective. In your case, a Pythagorean triple is a set of three integer numbers (a,b,c) being the sides of a right angle triangle such that a
2 + b
2 = c
2. I don't know the origin of the 'algorithm' you are using but either it is totally wrong or you grossly misunderstand it (according to the coding of your TRIPLE PROC).
The same applies to the code for the GCD algo. Its purpose should be to find the Greater Common Denominator between two numbers. If it is 1, the two numbers are then considered co-prime. And using co-prime numbers as the source, you then ensure that the generated Pythagorean triples are only the primitives (which I don't know if it was one of the stated objectives).
For your info, the basic example of a primitive Pythagorean triple is (3,4,5). A non-primitive one would be (6,8,10).
Quote from: raymond on December 07, 2014, 05:37:36 AM
For your info, the basic example of a primitive Pythagorean triple is (3,4,5). A non-primitive one would be (6,8,10).
5, 12 and 13 is a solution, too. These are so called Indian triples.
Gunther
sounds like a nice Euler Project problem :biggrin: