RosettaCodeData/Task/String-append/ARM-Assembly/string-append.arm

96 lines
4.1 KiB
Plaintext

/* ARM assembly Raspberry PI */
/* program appendstr.s */
/* Constantes */
.equ STDOUT, 1 @ Linux output console
.equ EXIT, 1 @ Linux syscall
.equ WRITE, 4 @ Linux syscall
.equ BUFFERSIZE, 100
/* Initialized data */
.data
szMessString: .asciz "String :\n"
szString1: .asciz "Alphabet : "
sComplement: .fill BUFFERSIZE,1,0
szString2: .asciz "abcdefghijklmnopqrstuvwxyz"
szCarriageReturn: .asciz "\n"
/* UnInitialized data */
.bss
/* code section */
.text
.global main
main:
ldr r0,iAdrszMessString @ display message
bl affichageMess
ldr r0,iAdrszString1 @ display begin string
bl affichageMess
ldr r0,iAdrszCarriageReturn @ display line return
bl affichageMess
ldr r0,iAdrszString1
ldr r1,iAdrszString2
bl append @ append sting2 to string1
ldr r0,iAdrszMessString
bl affichageMess
ldr r0,iAdrszString1 @ display string
bl affichageMess
ldr r0,iAdrszCarriageReturn
bl affichageMess
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc 0 @ perform system call
iAdrszMessString: .int szMessString
iAdrszString1: .int szString1
iAdrszString2: .int szString2
iAdrszCarriageReturn: .int szCarriageReturn
/******************************************************************/
/* append two strings */
/******************************************************************/
/* r0 contains the address of the string1 */
/* r1 contains the address of the string2 */
append:
push {r0,r1,r2,r7,lr} @ save registers
mov r2,#0 @ counter byte string 1
1:
ldrb r3,[r0,r2] @ load byte string 1
cmp r3,#0 @ zero final ?
addne r2,#1
bne 1b @ no -> loop
mov r4,#0 @ counter byte string 2
2:
ldrb r3,[r1,r4] @ load byte string 2
strb r3,[r0,r2] @ store byte string 1
cmp r3,#0 @ zero final ?
addne r2,#1 @ no -> increment counter 1
addne r4,#1 @ no -> increment counter 2
bne 2b @ no -> loop
100:
pop {r0,r1,r2,r7,lr} @ restaur registers
bx lr @ return
/******************************************************************/
/* display text with size calculation */
/******************************************************************/
/* r0 contains the address of the message */
affichageMess:
push {r0,r1,r2,r7,lr} @ save registers
mov r2,#0 @ counter length */
1: @ loop length calculation
ldrb r1,[r0,r2] @ read octet start position + index
cmp r1,#0 @ if 0 its over
addne r2,r2,#1 @ else add 1 in the length
bne 1b @ and loop
@ so here r2 contains the length of the message
mov r1,r0 @ address message in r1
mov r0,#STDOUT @ code to write to the standard output Linux
mov r7, #WRITE @ code call system "write"
svc #0 @ call system
pop {r0,r1,r2,r7,lr} @ restaur registers
bx lr @ return