Babbage problem: Difference between revisions
Content added Content deleted
VincentARM (talk | contribs) (Add task to aarch64 assembly raspberry pi) |
|||
Line 69: | Line 69: | ||
<pre> |
<pre> |
||
Solution is: i= 25264 (i*i= 638269696) |
Solution is: i= 25264 (i*i= 638269696) |
||
</pre> |
|||
=={{header|AArch64 Assembly}}== |
|||
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }} |
|||
<syntaxhighlight lang AArch64 Assembly> |
|||
/* ARM assembly AARCH64 Raspberry PI 3B */ |
|||
/* program babbage64.s */ |
|||
/************************************/ |
|||
/* Constantes */ |
|||
/************************************/ |
|||
/* for this file see task include a file in language AArch64 assembly*/ |
|||
.include "../includeConstantesARM64.inc" |
|||
/*********************************/ |
|||
/* Initialized data */ |
|||
/*********************************/ |
|||
.data |
|||
szMessResult: .asciz "Result = " |
|||
szMessStart: .asciz "Program 64 bits start.\n" |
|||
szCarriageReturn: .asciz "\n" |
|||
/*********************************/ |
|||
/* UnInitialized data */ |
|||
/*********************************/ |
|||
.bss |
|||
sZoneConv: .skip 24 // conversion buffer |
|||
/*********************************/ |
|||
/* code section */ |
|||
/*********************************/ |
|||
.text |
|||
.global main |
|||
main: // entry of program |
|||
ldr x0,qAdrszMessStart |
|||
bl affichageMess |
|||
ldr x4,qNbStart // start number = 269696 |
|||
mov x5,#0 // counter multiply |
|||
ldr x2,qNbMult // value multiply = 1 000 000 |
|||
mov x6,x4 |
|||
1: |
|||
mov x0,x6 |
|||
bl squareRoot // compute square root |
|||
mul x1,x0,x0 // compute square |
|||
umulh x3,x0,x0 |
|||
cmp x3,#0 // overflow ? |
|||
bne 100f // yes -> end |
|||
cmp x1,x6 // perfect square |
|||
bne 2f // no -> loop |
|||
ldr x1,qAdrsZoneConv |
|||
bl conversion10 |
|||
mov x0,#3 // string number to display |
|||
ldr x1,qAdrszMessResult |
|||
ldr x2,qAdrsZoneConv // insert conversion in message |
|||
ldr x3,qAdrszCarriageReturn |
|||
bl displayStrings // display message |
|||
b 100f // end |
|||
2: |
|||
add x5,x5,#1 // increment counter |
|||
mul x3,x5,x2 // multiply by 1 000 000 |
|||
add x6,x3,x4 // add start number |
|||
b 1b |
|||
100: // standard end of the program |
|||
mov x0, #0 // return code |
|||
mov x8,EXIT |
|||
svc #0 // perform the system call |
|||
qAdrszCarriageReturn: .quad szCarriageReturn |
|||
qNbStart: .quad 269696 |
|||
qNbMult: .quad 1000000 |
|||
qAdrsZoneConv: .quad sZoneConv |
|||
qAdrszMessResult: .quad szMessResult |
|||
qAdrszMessStart: .quad szMessStart |
|||
/***************************************************/ |
|||
/* Compute integer square root by Héron méthode */ |
|||
/***************************************************/ |
|||
/* r0 number */ |
|||
/* r0 return root */ |
|||
squareRoot: |
|||
stp x1,lr,[sp,-16]! // save registres |
|||
stp x2,x3,[sp,-16]! // save registres |
|||
cmp x0,#0 // |
|||
beq 100f |
|||
cmp x0,#4 // if < 4 return 1 |
|||
mov x1,1 |
|||
csel x0,x1,x0,lo |
|||
blo 100f |
|||
lsr x1,x0,#1 // division by 2 -> divisor |
|||
1: |
|||
mov x3,x1 // save previous result |
|||
udiv x2,x0,x1 // divide number by previous result |
|||
add x1,x1,x2 // add quotient to previous result |
|||
lsr x1,x1,#1 // division by 2 |
|||
cmp x1,x3 // compare result and previous result |
|||
blo 1b // loop if result is smaller then previous result |
|||
mov x0,x3 // else return previous result |
|||
100: |
|||
ldp x2,x3,[sp],16 // restaur registres |
|||
ldp x1,lr,[sp],16 // restaur registres |
|||
ret |
|||
/***************************************************/ |
|||
/* display multi strings */ |
|||
/* new version 24/05/2023 */ |
|||
/***************************************************/ |
|||
/* x0 contains number strings address */ |
|||
/* x1 address string1 */ |
|||
/* x2 address string2 */ |
|||
/* x3 address string3 */ |
|||
/* x4 address string4 */ |
|||
/* x5 address string5 */ |
|||
/* x6 address string5 */ |
|||
/* x7 address string6 */ |
|||
displayStrings: // INFO: displayStrings |
|||
stp x8,lr,[sp,-16]! // save registers |
|||
stp x2,fp,[sp,-16]! // save registers |
|||
add fp,sp,#32 // save paraméters address (4 registers saved * 8 bytes) |
|||
mov x8,x0 // save strings number |
|||
cmp x8,#0 // 0 string -> end |
|||
ble 100f |
|||
mov x0,x1 // string 1 |
|||
bl affichageMess |
|||
cmp x8,#1 // number > 1 |
|||
ble 100f |
|||
mov x0,x2 |
|||
bl affichageMess |
|||
cmp x8,#2 |
|||
ble 100f |
|||
mov x0,x3 |
|||
bl affichageMess |
|||
cmp x8,#3 |
|||
ble 100f |
|||
mov x0,x4 |
|||
bl affichageMess |
|||
cmp x8,#4 |
|||
ble 100f |
|||
mov x0,x5 |
|||
bl affichageMess |
|||
cmp x8,#5 |
|||
ble 100f |
|||
mov x0,x6 |
|||
bl affichageMess |
|||
cmp x8,#6 |
|||
ble 100f |
|||
mov x0,x7 |
|||
bl affichageMess |
|||
100: |
|||
ldp x2,fp,[sp],16 // restaur registers |
|||
ldp x8,lr,[sp],16 // restaur registers |
|||
ret |
|||
/***************************************************/ |
|||
/* ROUTINES INCLUDE */ |
|||
/***************************************************/ |
|||
/* for this file see task include a file in language AArch64 assembly*/ |
|||
.include "../includeARM64.inc" |
|||
</syntaxhighlight> |
|||
{{Out}} |
|||
<pre> |
|||
Program 64 bits start. |
|||
Result = 25264 |
|||
</pre> |
</pre> |
||