Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an ARM assembly language routine that accepts two C strings, concatenates

ID: 3680171 • Letter: W

Question

Write an ARM assembly language routine that accepts two C strings, concatenates them and returns a pointer to the concatenated version to the calling C driver program to be printed.

The assembly routine should have the definition:

            extern char * jstring( char * buf1, char * buf2 )

where buf1 is a pointer to the first string and buf2 is a pointer to the second string.

A string in C is a byte array with the last byte equal to zero.

The return to the C program will be the pointer to the newly allocated space where the strings are concatenated.

The driver program in C is:

/* C driver program for joining strings */

#include <stdlib.h>

#include <stdio.h>

extern char * jstring( char * buf1, char * buf2 ) ;

int main( int argc, char * argv[] )

{

            char buffer1[] = "First part of string" ;

            char buffer2[] = " join this to buffer1 " ;

            char * result ;

            result = jstring( buffer1, buffer2 ) ;

            printf( "Concatenated strings: %s ", result ) ;

           

            exit( 0 ) ;

}

You will need to determine the lengths of the two strings passed in. This can be done using the library routine strlen for each string. To find the length of the string the register a1 should have the pointer to the string. bl to the library routine ( bl strlen ). Upon return a1 will have the length of the string in bytes not counting the null byte at the end.

To obtain memory for the concatenated strings you use the library function malloc.

This subroutine has as its input in a1 the number of bytes requested. The return is in a1 and is a pointer to the space. For example, to obtain 30 bytes:

            mov      a1, #30

            bl         malloc

            ;a1 now has a pointer to a 30 byte space

You must calculate the space needed in the ARM subroutine.

Explanation / Answer

Program to compare strings, concatenate strings, copy string and display part of a string in C Programming

# include<stdio.h>

       void main()

       {

           int c;

           void func1();

           void func2();

           void func3();

           void func4();

           void func5();

           clrscr();

           printf(" - : User Defined String Functions : -");

           printf(" -------------------------------------");

           printf(" Find String Length      : 1");

           printf(" Concatenate two Strings : 2");

           printf(" String Comparison       : 3");

           printf(" String Copy             : 4");

           printf(" Extract SubString       : 5");

           printf(" Enter Your Choice       : ");

           scanf("%d",&c);

           switch(c)

           {

               case 1:

                   func1();

                   break;

               case 2:

                   func2();

                   break;

               case 3:

                   func3();

                   break;

               case 4:

                   func4();

                   break;

               case 5:

                   func5();

                   break;

               default:

                   printf(" Invalid Choice");

           }

           getch();

       }

       void func1()

       {

           char str[50];

           int slength(char []);

           int l;

           clrscr();

           fflush(stdin);

           printf(" Enter String : ");

           scanf("%[^ ]",str);

           l=slength(str);

           printf(" Length = %d",l);

       }

       int slength(char x[])

       {

           int i=0;

           while(x[i]!='')

               i++;

           return i;

       }

       void func2()

       {

           char str1[50],str2[50];

           void sconcate(char [],char []);

           clrscr();

           fflush(stdin);

           fflush(stdin);

           printf(" Enter String 1 : ");

           scanf("%[^ ]",str1);

           fflush(stdin);

           printf(" Enter String 2 : ");

           scanf("%[^ ]",str2);

           sconcate(str1,str2);

           printf("%s",str1);

       }

       void sconcate(char s1[],char s2[])

       {

           int i=0,j;

           j=slength(s1);

           while(s2[i]!='')

           {

               s1[j]=s2[i];

               j++;

               i++;

           }

           s1[j]='';

       }

       void func3()

       {

           char str1[50],str2[50];

           int l;

           int scomp(char [],char []);

           clrscr();

           fflush(stdin);

           printf(" Enter String 1 : ");

           scanf("%[^ ]",str1);

           fflush(stdin);

           printf(" Enter String 2 : ");

           scanf("%[^ ]",str2);

           l=scomp(str1,str2);

           if(l==0)

               printf(" Both Strings are Equal");

           elseif(l>0)

               printf(" String 1 is greater than String 2");

           else

               printf(" String 2 is greater than String 1");

       }

       int scomp(char s1[],char s2[])

       {

           int i,j,z;

           int l;

           for(i=0,j=0;(s1[i]!='' || s2[i]!='');i++,j++)

           {

               if(s1[i]!=s2[i])

               {

                   if(s1[i]>s2[i])

                       l=1;

                   else

                       l=-1;

                   break;

               }

           }

           if(s1[i]=='' && s2[i]=='')

               l=0;

           return l;

       }

       void func4()

       {

           char str1[50],str2[50];

           void scopy(char [],char []);

           clrscr();

           fflush(stdin);

           printf(" Enter String : ");

           scanf("%[^ ]",str1);

           scopy(str2,str1);

           printf(" Copied String : %s",str2);

       }

       void scopy(char s2[],char s1[])

       {

           int i;

           i=0;

           while(s1[i]!='')

           {

               s2[i]=s1[i];

               i++;

           }

           s2[i]='';

       }

       void func5()

       {

           char str[50],sub[50];

           int s,n;

           void substr(char [],int,int,char []);

           clrscr();

           fflush(stdin);

           printf(" Enter string : ");

           scanf("%[^ ]",str);

           fflush(stdin);

           printf(" Enter substring starting character index : ");

           scanf("%d",&s);

           fflush(stdin);

           printf(" Enter total no. of characters : ");

           scanf("%d",&n);

           substr(str,s,n,sub);

           printf(" Substring = %s",sub);

       }

       void substr(char str[],int s,int n,char ds[])

       {

           int i,j;

           j=0;

           i=s;

           while( (i<(s+n)) && (str[i]!=''))

           {

           ds[j]=str[i];

           i++;

           j++;

           }

           ds[j]='';

       }

Program which will accept string from the user . Pass this string to the function. Calculate the length of the string using pointer

#include<stdio.h>

#include<conio.h>

int string_ln(char*);

void main() {

   char str[20];

   int length;

   clrscr();

  

   printf(" Enter any string : ");

   gets(str);

  

   length = string_ln(str);

   printf("The length of the given string %s is : %d", str, length);

   getch();

}

int string_ln(char*p) /* p=&str[0] */

{

   int count = 0;

   while (*p != '') {

      count++;

      p++;

   }

   return count;

}

note-the code in above two programs may be useful for the given query .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote