0

I tried to do bubble sort based on this C code. Implement Bubble sort in assembly. Follow the reference C code and translate it to assembly. The implementation requires you exact translation of the reference code, which means you need to implement using local variables, function calls, and parameter passing following the given c code. Submit the source code, screenshot of intermediate steps, and the sorted array in memory window.

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm Links to an external site. that repeatedly steps through the input list element by element, comparing the current element with the one after it, swapping Links to an external site.their values if needed. These passes through the list are repeated until no swaps have to be performed during a pass, meaning that the list has become fully sorted. The algorithm, which is a comparison sort Links to an external site., is named for the way the larger elements "bubble" up to the top of the list.

// C program for implementation of Bubble sort
#include <stdio.h>

int arr[] = { 5, 1, 4, 2, 8 };

void swap(int* xp, int* yp)
{
    int temp = *xp;
    *xp = *yp;
    *yp = temp;
}

// A function to implement bubble sort
void bubbleSort(int arr[], int n)
{
    int i, j;
    for (i = 0; i < n - 1; i++)

        // Last i elements are already in place
        for (j = 0; j < n - i - 1; j++)
            if (arr[j] > arr[j + 1])
                swap(&arr[j], &arr[j + 1]);
}

// Driver program to test above functions
int main()
{
    int n = sizeof(arr) / sizeof(arr[0]);
    bubbleSort(arr, n);
    return 0;
}

the code is

myarr DWORD 5,1,4,2,8 .code elementswap proc push ebp mov ebp,esp

 push eax
 push ebx

mov eax,[EBP+8]
mov ebx,[EBP+12]
mov [EBP+8],ebx
mov [EBP+12],eax

pop eax
pop ebx

mov esp,ebp
pop ebp
ret

elementswap endp

bubblesort proc push ebp mov ebp,esp mov esi,0 mov ebx,[EBP+8] sub ebx,1 mov ecx,ebx mov edx,[EBP+12]

outerloop:
     cmp esi,ebx; 0 1 2 3 4,4
     JE exit_loop
     sub ecx,esi
     inc esi
     mov edi,0
     JL innerloop

innerloop:
     cmp edi,ecx ;0 1 2 3 4,4 ;0 1 2 3,3;0 1 2,2;0 1,1;0,0
     JGE outerloop
     JL L1

L1:
    mov eax,[edx]
    add edx,4
    cmp eax,[edx]; 0404000,0404004  0404004,0404008  0404008,040400C  040400C,0404011
    JG eleswap ;if eax content is bigger than element of the address stored edx
    JLE L2 ;if eax is less or equal than element of the address stored edx

eleswap:
     push eax
     mov eax,edx
     push eax
     mov eax,[edx]
     call elementswap
     JMP L2

L2:
    inc edi ;increment index innerloop
    JMP innerloop
   

exit_loop:


mov esp,ebp
pop ebp
ret

bubblesort endp

main proc push ebp mov ebp,esp

mov eax,OFFSET myarr
push eax

mov eax,LENGTHOF myarr 
push eax

call bubblesort

mov esp,ebp
pop ebp
ret

invoke ExitProcess,0

main endp end main




It suppose to sort the array and the elementswap supposed to go back inside the loop but it goes to the bubblesort function
Amalia
  • 11
  • 2
  • 1
    The function doesn't have a `ret` instruction. "Functions" in assembly don't return automatically. – sj95126 Dec 09 '22 at 00:46
  • 2
    Your `elementswap` sets up a stack frame but it does not destroy it. Also you seem to be passing argument pushed on the stack but the function simply uses `eax`. Additionally, you are missing the `ret` at the end as well (I don't think `endp` will emit that for you automatically.) – Jester Dec 09 '22 at 00:47
  • 1
    Now your `elementswap` is just swapping the arguments, not the values pointed to. You are missing a level of indirection. It also has the `pop`s in the wrong order (swapped, as it were, LOL). The `mov eax,[edx]` is useless before the `call elementswap`. The first `push eax` at `eleswap` puts the current value not the address on the stack so that will crash `elementswap` You do not reload `edx` after for each outerloop iteration so that goes out of bounds. – Jester Dec 09 '22 at 12:25

0 Answers0