Find square difference: Difference between revisions
Content added Content deleted
No edit summary |
VincentARM (talk | contribs) (add task to asm assembly raspberry pi) |
||
Line 76: | Line 76: | ||
<pre> |
<pre> |
||
Smallest n where n^2 - (n-1)^2 is > 1000 is: 501 |
Smallest n where n^2 - (n-1)^2 is > 1000 is: 501 |
||
</pre> |
|||
=={{header|ARM Assembly}}== |
|||
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}} |
|||
<syntaxhighlight lang ARM Assembly> |
|||
/* ARM assembly Raspberry PI */ |
|||
/* program diffsquare.s */ |
|||
/* REMARK 1 : this program use routines in a include file |
|||
see task Include a file language arm assembly |
|||
for the routine affichageMess conversion10 |
|||
see at end of this program the instruction include */ |
|||
/* for constantes see task include a file in arm assembly */ |
|||
/************************************/ |
|||
/* Constantes */ |
|||
/************************************/ |
|||
.include "../constantes.inc" |
|||
.equ MAXI, 10000 |
|||
/*********************************/ |
|||
/* Initialized data */ |
|||
/*********************************/ |
|||
.data |
|||
szMessNotFind: .asciz "No soluce !! " |
|||
szMessResult: .asciz "Nombre = " |
|||
szMessStart: .asciz "Program 32 bits start.\n" |
|||
szCarriageReturn: .asciz "\n" |
|||
/*********************************/ |
|||
/* UnInitialized data */ |
|||
/*********************************/ |
|||
.bss |
|||
sZoneConv: .skip 24 |
|||
/*********************************/ |
|||
/* code section */ |
|||
/*********************************/ |
|||
.text |
|||
.global main |
|||
main: @ entry of program |
|||
ldr r0,iAdrszMessStart |
|||
bl affichageMess |
|||
mov r5,#2 @ init start number |
|||
ldr r3,iMAXI |
|||
1: @ begin loop |
|||
mul r2,r5,r5 @ n * n |
|||
sub r4,r5,#1 @ n - 1 |
|||
mul r6,r4,r4 @ (n - 1) * (n - 1) |
|||
sub r0,r2,r6 @ difference |
|||
cmp r0,#1000 @ > 1000 |
|||
bgt 2f |
|||
add r5,r5,#1 @ increment number |
|||
cmp r5,r3 @ maxi limit ? |
|||
blt 1b |
|||
ldr r0,iAdrszMessNotFind @ display not find |
|||
bl affichageMess |
|||
b 100f |
|||
2: @ display result |
|||
mov r0,r5 |
|||
ldr r1,iAdrsZoneConv |
|||
bl conversion10 @ decimal conversion |
|||
mov r3,#0 @ zero final |
|||
strb r3,[r1,r0] |
|||
mov r0,#3 @ number string to display |
|||
ldr r1,iAdrszMessResult |
|||
ldr r2,iAdrsZoneConv @ insert conversion in message |
|||
ldr r3,iAdrszCarriageReturn |
|||
bl displayStrings @ display message |
|||
100: @ standard end of the program |
|||
mov r0, #0 @ return code |
|||
mov r7, #EXIT @ request to exit program |
|||
svc #0 @ perform the system call |
|||
iAdrszCarriageReturn: .int szCarriageReturn |
|||
iAdrsZoneConv: .int sZoneConv |
|||
iAdrszMessResult: .int szMessResult |
|||
iAdrszMessNotFind: .int szMessNotFind |
|||
iAdrszMessStart: .int szMessStart |
|||
iMAXI: .int MAXI |
|||
/***************************************************/ |
|||
/* display multi strings */ |
|||
/***************************************************/ |
|||
/* r0 contains number strings address */ |
|||
/* r1 address string1 */ |
|||
/* r2 address string2 */ |
|||
/* r3 address string3 */ |
|||
/* other address on the stack */ |
|||
/* thinck to add number other address * 4 to add to the stack */ |
|||
displayStrings: @ INFO: displayStrings |
|||
push {r1-r4,fp,lr} @ save des registres |
|||
add fp,sp,#24 @ save paraméters address (6 registers saved * 4 bytes) |
|||
mov r4,r0 @ save strings number |
|||
cmp r4,#0 @ 0 string -> end |
|||
ble 100f |
|||
mov r0,r1 @ string 1 |
|||
bl affichageMess |
|||
cmp r4,#1 @ number > 1 |
|||
ble 100f |
|||
mov r0,r2 |
|||
bl affichageMess |
|||
cmp r4,#2 |
|||
ble 100f |
|||
mov r0,r3 |
|||
bl affichageMess |
|||
cmp r4,#3 |
|||
ble 100f |
|||
mov r3,#3 |
|||
sub r2,r4,#4 |
|||
1: @ loop extract address string on stack |
|||
ldr r0,[fp,r2,lsl #2] |
|||
bl affichageMess |
|||
subs r2,#1 |
|||
bge 1b |
|||
100: |
|||
pop {r1-r4,fp,pc} |
|||
/***************************************************/ |
|||
/* ROUTINES INCLUDE */ |
|||
/***************************************************/ |
|||
.include "../affichage.inc" |
|||
</syntaxhighlight> |
|||
{{Out}} |
|||
<pre> |
|||
Program 32 bits start. |
|||
Nombre = 501 |
|||
</pre> |
</pre> |
||