Question

In: Computer Science

Write the below C program using ARM assembly language and compile for Cortex A53. #include<stdio.h> void...

Write the below C program using ARM assembly language and compile for Cortex A53.

#include<stdio.h>

void quicksort(int number[25],int first,int last){

int i, j, pivot, temp;

if(first<last)

{

pivot=first;

i=first;

j=last;

while(i<j)

{

while(number[i]<=number[pivot]&&i<last)

i++;

while(number[j]>number[pivot])

j--;

if(i<j){

temp=number[i];

number[i]=number[j];

number[j]=temp;

}

}

temp=number[pivot];

number[pivot]=number[j];

number[j]=temp;

quicksort(number,first,j-1);

quicksort(number,j+1,last);

}

}

int main()

{

int i, count, number[25];

printf("Enter some elements (Maximum 25): ");

scanf("%d",&count);

printf("Enter %d elements: ", count);

for(i=0;i<count;i++)

scanf("%d",&number[i]);

quicksort(number,0,count-1);

printf("The Sorted Order is: ");

for(i=0;i<count;i++)

printf(" %d",number[i]);

return 0;

}

Solutions

Expert Solution

.386
.model flat, c
.code

;Code by Miguel Casillas.
;This code can be used and reproduced, please give credit

;void QuickSort(void *pArray, int nItems);
QuickSort PROC

;These registers must be restored at the end
push EBP
mov EBP, ESP
push EBX
push ESI
push EDI

;EBP + 8 is the array
;EBP + 12 is the number of items in the array

mov ESI, [EBP+8] ;ESI is the array

;setting ECX to the number of items
;we multiply by 4 (size of the element) in order to put ECX
;at the last address of the array
mov EAX, [EBP+12]
mov ECX, 4
mul ECX
mov ECX, EAX

;EAX will be our 'low index', we initially set it to 0
xor EAX, EAX

;EBX will be our 'high index', we initially set it to
;the last element of the array (currently stored in ECX)
mov EBX, ECX

;We now call our recursive function that will sort the array
call QuickRecursive

;Restoring the registers
pop EDI
pop ESI
pop EBX
pop EBP

RET
QuickSort ENDP


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Recursive QuickSort function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
QuickRecursive:

;if lowIndex >= highIndex, we exit the function
cmp EAX, EBX
jge PostIf

push EAX ;saving our low index, now EAX is 'i'
push EBX ;saving our high index, now EBX is 'j'
add EBX, 4 ;j = high + 1

;EDI is our pivot
;pivot = array[lowIndex];
mov EDI, [ESI+EAX]

MainLoop:

iIncreaseLoop:

;i++
add EAX, 4

;If i >= j, exit this loop
cmp EAX, EBX
jge End_iIncreaseLoop

;If array[i] >= pivot, exit this loop
cmp [ESI+EAX], EDI
jge End_iIncreaseLoop

;Go back to the top of this loop
jmp iIncreaseLoop

End_iIncreaseLoop:

jDecreaseLoop:

;j--
sub EBX, 4

;If array[j] <= pivot, exit this loop
cmp [ESI+EBX], EDI
jle End_jDecreaseLoop

;Go back to the top of this loop
jmp jDecreaseLoop

End_jDecreaseLoop:

;If i >= j, then don't swap and end the main loop
cmp EAX, EBX
jge EndMainLoop

;Else, swap array[i] with array [j]
push [ESI+EAX]
push [ESI+EBX]

pop [ESI+EAX]
pop [ESI+EBX]

;Go back to the top of the main loop
jmp MainLoop

EndMainLoop:   

;Restore the high index into EDI
pop EDI

;Restore the low index into ECX
pop ECX

;If low index == j, don't swap
cmp ECX, EBX
je EndSwap

;Else, swap array[low index] with array[j]
push [ESI+ECX]
push [ESI+EBX]

pop [ESI+ECX]
pop [ESI+EBX]

EndSwap:

;Setting EAX back to the low index
mov EAX, ECX

push EDI ;Saving the high Index
push EBX ;Saving j

sub EBX, 4 ;Setting EBX to j-1

;QuickSort(array, low index, j-1)
call QuickRecursive

;Restore 'j' into EAX
pop EAX
add EAX, 4 ;setting EAX to j+1

;Restore the high index into EBX
pop EBX

;QuickSort(array, j+1, high index)
call QuickRecursive

PostIf:

RET

END


Related Solutions

*Answer in C program* Given a program as shown below: #include <stdio.h> void function1(void); void function2...
*Answer in C program* Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37;...
In MPLAB write and compile (using the simulator) an assembly language program with the following functionality:...
In MPLAB write and compile (using the simulator) an assembly language program with the following functionality: Configures pin RA2 of the PIC24to be an output to control an attached LED. Configures pin RB13 of the PIC24 to be an input to read the value on an attached switch (this switch will connect to ground when pressed). Configures pin RB13 to use the internal pull-up resistor. After configuration, the LED will be "off" when the push-button is pressed, and "on" when...
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {     ...
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {      int count =2; // COUNT TAKEN 2 AS TO PRINT 2 TIMES           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                                 return 0; }
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
Translate the following C program to PEP/9 assembly language. #include <stdio.h> Int main (){ int number;...
Translate the following C program to PEP/9 assembly language. #include <stdio.h> Int main (){ int number; Scanf (“%d”, & number); if (number % 2 ==0) { printf (“Even\n”); } else { printf(“Odd\n”); } Return 0; }
LANGUAGE: C Only using <stdio.h> & <stdlib.h> Write a program that gives the user a menu...
LANGUAGE: C Only using <stdio.h> & <stdlib.h> Write a program that gives the user a menu to choose from – 1. Convert temperature input from the user in degrees Fahrenheit to degrees Celsius 2. Convert temperature input from the user in degrees Celsius to degrees Fahrenheit 3. Quit. Formulae you will need: C = (5 / 9) * (F-32) and F = (9/5) * C + 32 1. Use functions to accomplish 1 and 2 above. 2. Use at least...
REWRITE THE FOLLOWING CODES USING FOR LOOP. PLS USE C LANGUAGE FORMAT #include <stdio.h> int main(void)...
REWRITE THE FOLLOWING CODES USING FOR LOOP. PLS USE C LANGUAGE FORMAT #include <stdio.h> int main(void) {      int count ; scanf("%d",&count);           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                            return 0; }
Take the following C++ program and translate it into assembly language( pep9 ) #include using namespace...
Take the following C++ program and translate it into assembly language( pep9 ) #include using namespace std; char ch; int main() {    cin >> ch;    cout << "You inputted " << ch << endl;    ch++;    cout << "Next character is " << ch << endl; if (ch <= ‘Z’)         cout << “Could be luppercase\n”;    return 0; }
Translate the following C program to Pep/9 assembly language. #include <stdio.h.> int main() { int numitms,j,data,sum;...
Translate the following C program to Pep/9 assembly language. #include <stdio.h.> int main() { int numitms,j,data,sum; scanf("%d", &numitms); sum=0; for (j=1;j<=numitms;j++) { scanf("%d", &data); sum+=data; } printf("sum: %d\n",sum); return0; } I got an answer with an error. Please debug the answer or have a new correct answer (don't copy and paste others' answer) main: SUBSP 2,i DECI numItms,i DECI j,j DECI data,d DECI sum,s LDWA numItms,i sum: .EQUATE 0 LDWA 1,i STWA j,j for: CPWA numItms, j BRGE endFor STRO...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT