assembly 组件中的指标

w6lpcovy  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(168)

在swap函数中,我传递了两个要交换的元素的地址。初始数组是5,1,4,2,8,代码应该执行冒泡排序

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;
}

这是我目前的汇编代码:

elementswap proc
push ebp
mov ebp,esp

     push eax
     push ebx
        
        ;do swap
    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

    outerloop:
         mov edx,[EBP+12]
         mov ebx,[EBP+8]
         sub ebx,2
         mov ecx,ebx
         cmp esi,ebx; 0 1 2 3 4,4
         JE exit_loop
         sub ecx,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
         JE L3
         JL L1
    
    L1:
        mov eax,[edx]
        add edx,4
        cmp eax,[edx]; 5,1   5,4   5,2 ;  1 4 2 5 8; 1,4 4,2 4,5 8; 1 2 4 5 8
        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
         call elementswap
         JMP L2
    
    L2:
        inc edi ;increment index innerloop
        JMP innerloop
     
    L3:
       inc esi
       JMP outerloop
       
    
    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

在呼叫bubblesort后,我期待1、2、4、5、8。

fiei3ece

fiei3ece1#

在swap函数中,我传递了两个应该交换的元素的地址。
真的吗?调用 elementswap 的代码如下所示:

push eax
mov eax,edx
push eax
call elementswap

推入的第一个EAX是数组元素的value,推入的第二个EAX是另一个数组元素的address
C代码在swap(&arr[j], &arr[j + 1]);中传递2个地址

push eax
push ebx
...
pop eax
pop ebx

elementswap 过程中,您试图保留堆栈上的EAX和EBX寄存器。要恢复这些寄存器,您需要反转pop的:
第一次

  • elementswap* proc只在堆栈上交换它的参数(不管它们是什么),它们会留在堆栈上,因为你从来没有删除过它们!堆栈会以这种方式迅速填满,这可能会导致堆栈/分段错误。

因为这是BubbleSort,交换只会发生在相邻的数组元素之间。因此,*elementswap proc的单一参数(位址)就足够了。而且,因为交换完成后,不再需要参数,所以请写入ret 4来平衡堆栈。ret指令的这个变体会在传回呼叫端时,将堆栈指标加4。

elementswap proc
  push ebp
  mov  ebp, esp
  push eax
  push ebx
  mov  ebp, [ebp+8]    ; Stacked argument is address in between 2 array elements
  mov  eax, [ebp-4]    ; Element on the left
  mov  ebx, [ebp]      ; Element on the right
  mov  [ebp-4], ebx
  mov  [ebp], eax
  pop  ebx
  pop  eax
  pop  ebp
  ret  4               ; Removes stacked argument
elementswap endp

你的BubbleSort似乎坏了。例如,如果数组包含2个元素,你会立即退出,但如果数组为空或只有1个元素,你就会开始排序。为了正确,首先研究我提供的这个SO答案中的例子,然后根据你的需要修改它。
我知道,出于学习的目的,您在一个单独的过程中进行交换,但是在BubbleSort的内部循环中进行交换的解决方案更有效。

相关问题