In Linux, I want to make assembly program that find how many Pythagorean triples(k^2= i^2+j^2).
Assembly program meet following condition.
(An appropriate k-value determination with an execution speed of 1-2 minutes)
(add Loop, count accumulation, and scoping)
Please modify mm.s and make a program.
The results of this program are as follows:
(25,45) : 12
(1,8000): 9705
(25,45) : 12 <- When the range of k is 25 to 45, it means that the number of i, j, and k pairs that complete k^2 = i^2 + j^2 within the given range.
And I wrote some code.
#include <stdio.h>
#include <time.h>
extern int com(long int a,long int b);
int main(){
int i;
time_t start,end;
time(&start);
i=com(25,45);
time(&end);
printf("%5d: %d secn",i, (int) (end-start));
}
com(int k1,int k2){ // p*p = i*i + j*j
int i,ii,j,p,pp,count;
count=0;
for (p=k2;p<k1;p--){
pp=p*p;
for (i=p;i>0;i--){
ii=i*i;
for (j=p;j>0;j--)
if(pp==ii+j*j) count++;
}
}
return count;
}
mm.s code meet following conditons.
I find pythagorean triples that meet the following conditions.
condition:
k^2=i^2+j^2
i,j<=45
2<=k<=45
so i make assembly code like this:
main.c
# include <stdio.h>
extern long int com(long int);
int main(){
long int i,k;
for (k=2;k<=45;k++)
printf("%3ld : %25ldn",k,com(k));
}
mm.s
.text # section declaration
.global com
com:
movq %rdi,%r11 # k
imulq %rdi,%r11 # k^2
movq $0, %rax # init count
movq %rdi, %r9 # arg2
movq %rdi, %r10 # arg
com_loop1:
cmpq $0, %r10 # arg1 index
jle com_end1
movq %r10, %r8
imulq %r8, %r8 # i^2
com_loop2:
cmpq $0, %r9 # arg2 index
jle com_end2
movq %r9, %rbx
imulq %rbx, %rbx # j^2
addq %r8,%rbx
subq %r11,%rbx # (i^2+j^2) - k^2
cmpq $0, %rbx
jnz com_z
addq $1, %rax # count++
com_z:
subq $1, %r9 # j--
jmp com_loop2
com_end2:
subq $1, %rdi # k-- (j<i)
movq %rdi, %r9 # restore arg2 index
subq $1, %r10 # i--
jmp com_loop1
com_end1:
ret
$cc main.c mm.s
$./a.out
result
2 : 0
3 : 04 : 0
5 : 1
6 : 0
.
.
.
45 : 1
2:0 <-When k is 2, it means that the number of k,i, and j pairs that satisfy k^2=i^2+j^2 is zero.
Source: Windows Questions C++