RosettaCodeData/Task/Repeat-a-string/ARM-Assembly/repeat-a-string.arm

120 lines
3.6 KiB
Plaintext

/* ARM assembly Raspberry PI */
/* program repeatstring.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 */
/*******************************************/
/* Constantes */
/*******************************************/
.include "../constantes.inc"
.equ BUFFERSIZE, 2000
/*******************************************/
/* Macros */
/*******************************************/
//.include "../../ficmacros32.inc" @ for developer debugging
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessDebutPgm: .asciz "Program 32 bits start. \n"
szCarriageReturn: .asciz "\n"
szMessFinOK: .asciz "Program normal end. \n"
szMessErreur: .asciz "Error Buffer too small!!!\n"
szString1: .asciz "ha"
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
.align 4
sZoneConv: .skip 24
sBuffer: .skip BUFFERSIZE
/*********************************/
/* code section */
/*********************************/
.text
.global main
main:
ldr r0,iAdrszMessDebutPgm
bl affichageMess @ start message
ldr r0,iAdrszString1 @ load phrase adress
ldr r1,iAdrsBuffer
mov r2,#5
bl repeatString
cmp r0,#0
ble 99f
ldr r0,iAdrsBuffer @ buffer display
bl affichageMess
ldr r0,iAdrszCarriageReturn
bl affichageMess
ldr r0,iAdrszMessFinOK
bl affichageMess
b 100f
99:
ldr r0,iAdrszMessErreur @ error
bl affichageMess
mov r0, #1 @ return code error
b 100f
100:
mov r7,#EXIT @ program end
svc #0 @ system call
iAdrszMessDebutPgm: .int szMessDebutPgm
iAdrszMessFinOK: .int szMessFinOK
iAdrszMessErreur: .int szMessErreur
iAdrszString1: .int szString1
iAdrsBuffer: .int sBuffer
iAdrsZoneConv: .int sZoneConv
iAdrszCarriageReturn: .int szCarriageReturn
/******************************************************************/
/* test if number is aritmetic number */
/******************************************************************/
/* r0 contains string address */
/* r1 contains buffer address */
/* r2 number repeat */
/* r0 return buffer writed length or -1 if error*/
repeatString:
push {r3-r6,lr} @ save registers
mov r3,#0 @ indice repeat
mov r4,#0 @ indice buffer
1:
mov r5,#0 @ indice string
2:
ldrb r6,[r0,r5] @ load string characters
cmp r6,#0 @ end string ?
beq 3f
strb r6,[r1,r4] @ store char in buffer
add r4,r4,#1 @ increment indice
add r5,r5,#1
b 2b @ and loop
3:
mul r6,r5,r2 @ compute repeat length string
cmp r6,#BUFFERSIZE @ compare to buffer length
movge r0,#-1 @ error ?
bge 100f
add r3,r3,#1 @ increment repeat counter
cmp r3,r2 @ end ?
blt 1b
mov r6,#0
strb r6,[r1,r4] @ final zero
mov r0,r4 @ return length
100:
pop {r3-r6,pc} @ restaur registers
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../affichage.inc"