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

Write an ARM assembly language which accepts an integer and returns a string whi

ID: 3680838 • Letter: W

Question

Write an ARM assembly language which accepts an integer and returns a string which is the hexadecimal representation of the integer.

The signature of the routine is:
char * int2hex( int conv ) ;

The input is a 32 bit integer. The output is a pointer to a character string of the format 0Xdddddddd. Do not suppress leading zeroes.

The C program is:

#include <stdlib.h>
#include <stdio.h>

extern char * int2hex( int convert ) ;

int main( int argc, char * argv[] )
{
   int convert = 1194684 ;
   char * result ;
  
   result = int2hex( convert ) ;
   printf( "Integer to hex string: %s ", result ) ;
}


A string in C is terminated by a null byte (a byte of zero).
You will need to use the shift instructions. You need to separate the integer into 4 bit sections each representing the individual hex digits. These need to be turned into printable characters.
You need to ‘adjust’ the resulting values for the digits A-F.
Use the strb instruction and an auto increment of 1 since you are storing bytes into memory.
Use the malloc library routine to obtain space for the character string (11 bytes: 2 for 0x, 8 for the hex digits, and one for the null byte).

Explanation / Answer

//c program for converting integer to hexa hex digits

#include <stdlib.h>
#include <stdio.h>
#include<string.h>
extern char * int2hex( int convert ) ;
int main( int argc, char * argv[] )
{
int convert = 1194684 ;
char * result ;
  
result = int2hex( convert ) ;
printf( "Integer %d to hex string: %s ",convert, result ) ;
}

extern char* int2hex(int convert)
{
char * res = malloc(11),str[20],bfr[20];//res string allocated to 11 bytes memory
int temp,i=0,j,ipc[10];
temp=convert;
printf("%d",temp/16);

while (temp>0)
{
ipc[i]=temp%16;//obtaining each hex bit
//str=(char)temp%16;
printf(" 4 bit integer character for print %d ",ipc[i]);
sprintf(bfr,"%d",ipc[i]);
//printf(" char is %s",bfr);
  
if(ipc[i]<9)
strcat(str,bfr);//concatenating bit to str string
//res[i]=ipc[i];
else
switch(ipc[i])
{
case 10 : strcat(str,"A");break;
case 11 : strcat(str,"B");break;
case 12 : strcat(str,"C");break;
case 13 : strcat(str,"D");break;
case 14 : strcat(str,"E");break;
case 15 : strcat(str,"F");break;
default : printf("wrong");
}
temp = temp/16;
//printf("temp is %d ",temp);
i++;
  
}
j=strlen(str);
/*for(i=1;i<strlen(str);i++,j--)
{
char *a=str[j-1];
strcat(res,a);
}
printf("str is%d %s",temp,str);
*/

j=8-j;
str>>j;//using left shift for 0's remaining bits
strcat(str,"0x");//appending 0x for start bits
printf("str is %s",str);//printing reverse of required bits
res= strrev(str);//required hex digit
return res;
}

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