Question

Translate pseudocode (bubble sort) into MIPS A[0] holds the smallest value and A[size -1] holds the...

 
        Translate pseudocode (bubble sort) into MIPS
        A[0] holds the smallest value and 
                A[size -1] holds the largest value.
                
                int t = 0;
                
                for ( int i=0; i<size-1; i++ ) {
                        for ( int j=0; j<size-1-i; j++ ) {
                                if ( A[j] > A[j+1] ) {
                                        t = A[j+1];
                                        A[j+1] = A[j];
                                        A[j] = t;
                                }
                        }
                }
                                
         ----------------------------------------------------------------------------------

         PART 2
                Translate pseudocode (binary search) into MIPS
                The array A has already been sorted due to part 1. A[0] 
                holds the smallest value and A[size -1] holds the largest value, and v holds 
                the search value by user input
                
                int left = 0;
                int right = size - 1;
                int middle = 0;
                int element_index = -1;
        
                while ( left <= right ) { 
      
        middle = left + (right - left) / 2; 
        
                if ( A[middle] == v) {
                                element_index = middle;
                                break;
                }
      
                if ( A[middle] < v ) 
                                left = middle + 1; 
                else
                                right = middle - 1; 
        
                } 
        
                if ( element_index < 0 ) 
                        printf( "%d not in sorted A\n", v );
                else 
                        printf( "Sorted A[%d] = %d\n", element_index, v );

______________________________________________________________________________

Homework Answers

Answer #1

    Translation of bubble sort psuedo code

    size:

            .zero   4

    A:

            .zero   4

    t:

            .zero   4

    main:

            push    rbp

            mov     rbp, rsp

            mov     DWORD PTR [rbp-4], 0

            jmp     .L2

    .L6:

            mov     DWORD PTR [rbp-8], 0

            jmp     .L3

    .L5:

            mov     eax, DWORD PTR [rbp-8]

            cdqe

            mov     edx, DWORD PTR A[0+rax*4]

            mov     eax, DWORD PTR [rbp-8]

            add     eax, 1

            cdqe

            mov     eax, DWORD PTR A[0+rax*4]

            cmp     edx, eax

            jle     .L4

            mov     eax, DWORD PTR [rbp-8]

            add     eax, 1

            cdqe

            mov     eax, DWORD PTR A[0+rax*4]

            mov     DWORD PTR t[rip], eax

            mov     eax, DWORD PTR [rbp-8]

            lea     ecx, [rax+1]

            mov     eax, DWORD PTR [rbp-8]

            cdqe

            mov     edx, DWORD PTR A[0+rax*4]

            movsx   rax, ecx

            mov     DWORD PTR A[0+rax*4], edx

            mov     edx, DWORD PTR t[rip]

            mov     eax, DWORD PTR [rbp-8]

            cdqe

            mov     DWORD PTR A[0+rax*4], edx

    .L4:

            add     DWORD PTR [rbp-8], 1

    .L3:

            mov     eax, DWORD PTR size[rip]

            sub     eax, 1

            sub     eax, DWORD PTR [rbp-4]

            cmp     DWORD PTR [rbp-8], eax

            jl      .L5

            add     DWORD PTR [rbp-4], 1

    .L2:

            mov     eax, DWORD PTR size[rip]

            sub     eax, 1

            cmp     DWORD PTR [rbp-4], eax

            jl      .L6

            nop

            nop

            pop     rbp

            ret

2) Translation of binary search

binarySearch:
        push    rbp
        mov     rbp, rsp
        sub     rsp, 48
        mov     QWORD PTR [rbp-24], rdi
        mov     DWORD PTR [rbp-28], esi
        mov     DWORD PTR [rbp-32], edx
        mov     DWORD PTR [rbp-36], ecx
        mov     eax, DWORD PTR [rbp-32]
        cmp     eax, DWORD PTR [rbp-28]
        jl      .L2
        mov     eax, DWORD PTR [rbp-32]
        sub     eax, DWORD PTR [rbp-28]
        mov     edx, eax
        shr     edx, 31
        add     eax, edx
        sar     eax
        mov     edx, eax
        mov     eax, DWORD PTR [rbp-28]
        add     eax, edx
        mov     DWORD PTR [rbp-4], eax
        mov     eax, DWORD PTR [rbp-4]
        cdqe
        lea     rdx, [0+rax*4]
        mov     rax, QWORD PTR [rbp-24]
        add     rax, rdx
        mov     eax, DWORD PTR [rax]
        cmp     DWORD PTR [rbp-36], eax
        jne     .L3
        mov     eax, DWORD PTR [rbp-4]
        jmp     .L4
.L3:
        mov     eax, DWORD PTR [rbp-4]
        cdqe
        lea     rdx, [0+rax*4]
        mov     rax, QWORD PTR [rbp-24]
        add     rax, rdx
        mov     eax, DWORD PTR [rax]
        cmp     DWORD PTR [rbp-36], eax
        jge     .L5
        mov     eax, DWORD PTR [rbp-4]
        lea     edi, [rax-1]
        mov     edx, DWORD PTR [rbp-36]
        mov     esi, DWORD PTR [rbp-28]
        mov     rax, QWORD PTR [rbp-24]
        mov     ecx, edx
        mov     edx, edi
        mov     rdi, rax
        call    binarySearch
        jmp     .L4
.L5:
        mov     eax, DWORD PTR [rbp-4]
        lea     esi, [rax+1]
        mov     ecx, DWORD PTR [rbp-36]
        mov     edx, DWORD PTR [rbp-32]
        mov     rax, QWORD PTR [rbp-24]
        mov     rdi, rax
        call    binarySearch
        jmp     .L4
.L2:
        mov     eax, -1
.L4:
        leave
        ret
.LC0:
        .string "Element is not present in array"
.LC1:
        .string "Element is present at index %d"
main:
        push    rbp
        mov     rbp, rsp
        sub     rsp, 32
        mov     DWORD PTR [rbp-32], 2
        mov     DWORD PTR [rbp-28], 3
        mov     DWORD PTR [rbp-24], 4
        mov     DWORD PTR [rbp-20], 10
        mov     DWORD PTR [rbp-16], 40
        mov     DWORD PTR [rbp-4], 5
        mov     DWORD PTR [rbp-8], 10
        mov     eax, DWORD PTR [rbp-4]
        lea     esi, [rax-1]
        mov     edx, DWORD PTR [rbp-8]
        lea     rax, [rbp-32]
        mov     ecx, edx
        mov     edx, esi
        mov     esi, 0
        mov     rdi, rax
        call    binarySearch
        mov     DWORD PTR [rbp-12], eax
        cmp     DWORD PTR [rbp-12], -1
        jne     .L7
        mov     edi, OFFSET FLAT:.LC0
        mov     eax, 0
        call    printf
        jmp     .L8
.L7:
        mov     eax, DWORD PTR [rbp-12]
        mov     esi, eax
        mov     edi, OFFSET FLAT:.LC1
        mov     eax, 0
        call    printf
.L8:
        mov     eax, 0
        leave
        ret

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a MIPS assembly program that sorts an array using bubble sort translating the C code...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code int main(void) { int array[] = {10, 2, 7, 5, 15, 30, 8, 6}; // input array int arraySize = sizeof(array)/sizeof(array[0]); bool swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; //Note : "j" , "arraySize - j" are optimizations to the bubble sort algorithm j++; // j= sorted elements int i=0; /* "arraySize - j" is used...
Translate C code into MIPS. Do not include an exit syscall int proc1( int a, int...
Translate C code into MIPS. Do not include an exit syscall int proc1( int a, int b ) { if ( proc2( a, b ) >= 0 ) return 1; else return -1; } int proc2( int a, int b ) { return (a*10) & (b*10); } int main() { int a = 9; int b = -10; int c = a + b + proc1( a, b ); printf("%d\n", c ); return 0; }
Can you translate this C code into MIPS assembly with comment? #include <stdio.h> #include <math.h> #include...
Can you translate this C code into MIPS assembly with comment? #include <stdio.h> #include <math.h> #include <stdlib.h> double fact (double); void main () { int angle_in_D; double term, angle_in_R; float sine = 0; unsigned int i = 1; double sign = 1; int n = 1000; printf ("Please enter an angle (Unit: Degree): "); scanf ("%d", &angle_in_D); angle_in_R = angle_in_D * M_PI / 180.0; do { term = pow(-1,(i-1)) * pow (angle_in_R, (2*i - 1)) / fact (2*i - 1);...
What is wrong with my recursive Binary Search code? For some reason my 'middle' value is...
What is wrong with my recursive Binary Search code? For some reason my 'middle' value is always zero? Please fix and explain! #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int BinarySearch(const int A[],const int L,const int R,const int key) {              if (R >= 1){           int middle = L + (R-1)/2;                              if (A[middle] == key)        return A[middle];               if (A[middle] > key){...
#include #include #define MAX_SIZE 1300 // Maximum string size int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE];...
#include #include #define MAX_SIZE 1300 // Maximum string size int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE]; printf("Enter any string: "); gets(str); printf("Enter word to search occurrences: "); gets(tosearch); int cursor = 0; int i = 0; int stringLen = 0; int searchLen = 0; int count1; int count2; stringLen = strlen(str); searchLen = strlen(tosearch); count1 = 0; count2 = 0; for (cursor = 0; cursor <= stringLen; ) { if (str[cursor] == tosearch[i]) { count1++; if (count1 == searchLen) {...
Question asks to use if statements to test if the value entered is an A. If...
Question asks to use if statements to test if the value entered is an A. If true print "The value of an A is 4.0" I'm struggling to get it to give the right results using char variable which the questions says to use. C programming. int main() { char Grade = 'X'; // Declares a character type variable named Grade printf("Enter a grade\t"); // Prompts for Grade scanf("%c", &Grade); // Inputs Grade printf("Grade is: \t%c\n", Grade); // Prints the...
Write the following program in MIPS: a) declare an array A of the following numbers: 3,...
Write the following program in MIPS: a) declare an array A of the following numbers: 3, 5, 8, 10, 12, 2, 76, 43, 90, 44 b) declare a variable called size which stores the number of element in array A, that is 10. c) write a subroutine to search for a number stored in an array and return true or false. In C++ the subroutine is as follows: search(array, size, number_To_Search) e.g. search(A, 10, 12) The subroutine should return 0...
Using the C programming language implement Heapsort in the manner described in class. Here is some...
Using the C programming language implement Heapsort in the manner described in class. Here is some example code to use as a guideline. Remember, you need only implement the sort algorithm, both the comparison and main functions have been provided. /* * * after splitting this file into the five source files: * * srt.h, main.c, srtbubb.c, srtinsr.c, srtmerg.c * * compile using the command: * * gcc -std=c99 -DRAND -DPRNT -DTYPE=(float | double) -D(BUBB | HEAP | INSR |...
I have written working code for this problem however it is not producing an output and...
I have written working code for this problem however it is not producing an output and it states is a wrong answer. Please just fix my code below and make it work for the sample test cases and all test cases. You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the...
[15 pts.] Consider a version of a binary search tree (BST) for sorted maps with integer...
[15 pts.] Consider a version of a binary search tree (BST) for sorted maps with integer keys that stores additional information as data members of each node w of the tree to account for the smallest and largest integer keys in the subtree rooted at w. Suppose that we can access this information using w.min, for the smallest integer key, and w.max, for the largest. Algorithm 4 provides the pseudocode for the constructor of a node in a binary tree...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT