How to resolve the algorithm String comparison step by step in the ARM Assembly programming language
How to resolve the algorithm String comparison step by step in the ARM Assembly programming language
Table of Contents
Problem Statement
Demonstrate how to compare two strings from within the language and how to achieve a lexical comparison.
The task should demonstrate:
For example, you might demonstrate the difference between generic/polymorphic comparison and coercive/allomorphic comparison if your language supports such a distinction.
Here "generic/polymorphic" comparison means that the function or operator you're using doesn't always do string comparison, but bends the actual semantics of the comparison depending on the types one or both arguments; with such an operator, you achieve string comparison only if the arguments are sufficiently string-like in type or appearance.
In contrast, a "coercive/allomorphic" comparison function or operator has fixed string-comparison semantics regardless of the argument type; instead of the operator bending, it's the arguments that are forced to bend instead and behave like strings if they can, and the operator simply fails if the arguments cannot be viewed somehow as strings. A language may have one or both of these kinds of operators; see the Raku entry for an example of a language with both kinds of operators.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm String comparison step by step in the ARM Assembly programming language
Source code in the arm programming language
/* ARM assembly Raspberry PI */
/* program comparString.s */
/* Constantes */
.equ STDOUT, 1 @ Linux output console
.equ EXIT, 1 @ Linux syscall
.equ WRITE, 4 @ Linux syscall
/* Initialized data */
.data
szMessStringEqu: .asciz "The strings are equals.\n"
szMessStringNotEqu: .asciz "The strings are not equals.\n"
szCarriageReturn: .asciz "\n"
szString1: .asciz "ABCDE"
szString2: .asciz "ABCDE"
szString3: .asciz "ABCFG"
szString4: .asciz "ABC"
szString5: .asciz "abcde"
/* UnInitialized data */
.bss
/* code section */
.text
.global main
main: /* entry of program */
push {fp,lr} /* saves 2 registers */
ldr r0,iAdrszString1
ldr r1,iAdrszString2
bl Comparaison
ldr r0,iAdrszString1
ldr r1,iAdrszString3
bl Comparaison
ldr r0,iAdrszString1
ldr r1,iAdrszString4
bl Comparaison
@ case sensitive comparisons ABCDE et abcde
ldr r0,iAdrszString1
ldr r1,iAdrszString5
bl Comparaison
@ case insensitive comparisons ABCDE et abcde
ldr r0,iAdrszString1
ldr r1,iAdrszString5
bl comparStringsInsensitive
cmp r0,#0
bne 1f
ldr r0,iAdrszMessStringEqu
bl affichageMess
b 2f
1:
ldr r0,iAdrszMessStringNotEqu
bl affichageMess
2:
100: /* standard end of the program */
mov r0, #0 @ return code
pop {fp,lr} @restaur 2 registers
mov r7, #EXIT @ request to exit program
swi 0 @ perform the system call
iAdrszString1: .int szString1
iAdrszString2: .int szString2
iAdrszString3: .int szString3
iAdrszString4: .int szString4
iAdrszString5: .int szString5
iAdrszMessStringEqu: .int szMessStringEqu
iAdrszMessStringNotEqu: .int szMessStringNotEqu
iAdrszCarriageReturn: .int szCarriageReturn
/*********************************************/
/* comparaison */
/*********************************************/
/* r0 contains address String 1 */
/* r1 contains address String 2 */
Comparaison:
push {fp,lr} /* save registres */
bl comparStrings
cmp r0,#0
bne 1f
ldr r0,iAdrszMessStringEqu
bl affichageMess
b 2f
1:
ldr r0,iAdrszMessStringNotEqu
bl affichageMess
2:
pop {fp,lr} /* restaur des 2 registres */
bx lr /* return */
/******************************************************************/
/* display text with size calculation */
/******************************************************************/
/* r0 contains the address of the message */
affichageMess:
push {fp,lr} /* save registres */
push {r0,r1,r2,r7} /* save others 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" */
swi #0 /* call systeme */
pop {r0,r1,r2,r7} /* restaur others registers */
pop {fp,lr} /* restaur des 2 registres */
bx lr /* return */
/************************************/
/* Strings case sensitive comparisons */
/************************************/
/* r0 et r1 contains the address of strings */
/* return 0 in r0 if equals */
/* return -1 if string r0 < string r1 */
/* return 1 if string r0 > string r1 */
comparStrings:
push {r1-r4} /* save des registres */
mov r2,#0 /* counter */
1:
ldrb r3,[r0,r2] /* byte string 1 */
ldrb r4,[r1,r2] /* byte string 2 */
cmp r3,r4
movlt r0,#-1 /* small */
movgt r0,#1 /* greather */
bne 100f /* not equals */
cmp r3,#0 /* 0 end string */
moveq r0,#0 /* equals */
beq 100f /* end string */
add r2,r2,#1 /* else add 1 in counter */
b 1b /* and loop */
100:
pop {r1-r4}
bx lr
/************************************/
/* Strings case insensitive comparisons */
/************************************/
/* r0 et r1 contains the address of strings */
/* return 0 in r0 if equals */
/* return -1 if string r0 < string r1 */
/* return 1 if string r0 > string r1 */
comparStringsInsensitive:
push {r1-r4} /* save des registres */
mov r2,#0 /* counter */
1:
ldrb r3,[r0,r2] /* byte string 1 */
ldrb r4,[r1,r2] /* byte string 2 */
@ majuscules --> minuscules byte 1
cmp r3,#65
blt 2f
cmp r3,#90
bgt 2f
add r3,#32
2: @ majuscules --> minuscules byte 2
cmp r4,#65
blt 3f
cmp r4,#90
bgt 3f
add r4,#32
3:
cmp r3,r4
movlt r0,#-1 /* small */
movgt r0,#1 /* greather */
bne 100f /* not equals */
cmp r3,#0 /* 0 end string */
moveq r0,#0 /* equal */
beq 100f /* end strings */
add r2,r2,#1 /* else add 1 in counter */
b 1b /* and loop */
100:
pop {r1-r4}
bx lr /* end procedure */
You may also check:How to resolve the algorithm Selectively replace multiple instances of a character within a string step by step in the Java programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the Avail programming language
You may also check:How to resolve the algorithm Ethiopian multiplication step by step in the МК-61/52 programming language
You may also check:How to resolve the algorithm Yellowstone sequence step by step in the Factor programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the HicEst programming language