RosettaCodeData/Task/Tokenize-a-string/ARM-Assembly/tokenize-a-string.arm

162 lines
7.3 KiB
Plaintext

/* ARM assembly Raspberry PI */
/* program strTokenize.s */
/* Constantes */
.equ STDOUT, 1 @ Linux output console
.equ EXIT, 1 @ Linux syscall
.equ WRITE, 4 @ Linux syscall
.equ NBPOSTESECLAT, 20
/* Initialized data */
.data
szMessFinal: .asciz "Words are : \n"
szString: .asciz "Hello,How,Are,You,Today"
szMessError: .asciz "Error tokenize !!\n"
szCarriageReturn: .asciz "\n"
/* UnInitialized data */
.bss
/* code section */
.text
.global main
main:
ldr r0,iAdrszString @ string address
mov r1,#',' @ separator
bl stTokenize
cmp r0,#-1 @ error ?
beq 99f
mov r2,r0 @ table address
ldr r0,iAdrszMessFinal @ display message
bl affichageMess
ldr r4,[r2] @ number of areas
add r2,#4 @ first area
mov r3,#0 @ loop counter
1: @ display loop
ldr r0,[r2,r3, lsl #2] @ address area
bl affichageMess
ldr r0,iAdrszCarriageReturn @ display carriage return
bl affichageMess
add r3,#1 @ counter + 1
cmp r3,r4 @ end ?
blt 1b @ no -> loop
b 100f
99: @ display error message
ldr r0,iAdrszMessError
bl affichageMess
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc 0 @ perform the system call
iAdrszString: .int szString
iAdrszFinalString: .int szFinalString
iAdrszMessFinal: .int szMessFinal
iAdrszMessError: .int szMessError
iAdrszCarriageReturn: .int szCarriageReturn
/******************************************************************/
/* 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 systeme
pop {r0,r1,r2,r7,lr} @ restaur des 2 registres
bx lr @ return
/*******************************************************************/
/* Separate string by separator into an array */
/* areas are store on the heap Linux */
/*******************************************************************/
/* r0 contains string address */
/* r1 contains separator character (, or . or : ) */
/* r0 returns table address with first item = number areas */
/* and other items contains pointer of each string */
stTokenize:
push {r1-r8,lr} @ save des registres
mov r6,r0
mov r8,r1 @ save separator
bl strLength @ length string for place reservation on the heap
mov r4,r0
ldr r5,iTailleTable
add r5,r0
and r5,#0xFFFFFFFC
add r5,#4 @ align word on the heap
@ place reservation on the heap
mov r0,#0 @ heap address
mov r7, #0x2D @ call system linux 'brk'
svc #0 @ call system
cmp r0,#-1 @ error call system
beq 100f
mov r3,r0 @ save address heap begin
add r0,r5 @ reserve r5 byte on the heap
mov r7, #0x2D @ call system linux 'brk'
svc #0
cmp r0,#-1
beq 100f
@ string copy on the heap
mov r0,r6
mov r1,r3
1: @ loop copy string
ldrb r2,[r0],#1 @ read one byte and increment pointer one byte
strb r2,[r1],#1 @ store one byte and increment pointer one byte
cmp r2,#0 @ end of string ?
bne 1b @ no -> loop
add r4,r3 @ r4 contains address table begin
mov r0,#0
str r0,[r4]
str r3,[r4,#4]
mov r2,#1 @ areas counter
2: @ loop load string character
ldrb r0,[r3]
cmp r0,#0
beq 3f @ end string
cmp r0,r8 @ separator ?
addne r3,#1 @ no -> next location
bne 2b @ and loop
mov r0,#0 @ store zero final of string
strb r0,[r3]
add r3,#1 @ next character
add r2,#1 @ areas counter + 1
str r3,[r4,r2, lsl #2] @ store address area in the table at index r2
b 2b @ and loop
3:
str r2,[r4] @ returns number areas
mov r0,r4
100:
pop {r1-r8,lr}
bx lr
iTailleTable: .int 4 * NBPOSTESECLAT
/***************************************************/
/* calcul size string */
/***************************************************/
/* r0 string address */
/* r0 returns size string */
strLength:
push {r1,r2,lr}
mov r1,#0 @ init counter
1:
ldrb r2,[r0,r1] @ load byte of string index r1
cmp r2,#0 @ end string ?
addne r1,#1 @ no -> +1 counter
bne 1b @ and loop
100:
mov r0,r1
pop {r1,r2,lr}
bx lr