如何进行排序算法

问题描述 投票:0回答:1

我的老师给我们布置了一项作业,要求我们编写对数字进行排序的源代码。 该代码应该由 2 个过程组成。
过程 1(最小值)接收列表中需要按引用排序的第一个元素的地址。它还通过值传递接收元素数量,并应返回最小元素的位置。
过程2称为“交换”。它通过引用接收列表头部的位置和最小元素的位置,交换它们的值。

这两个过程应该一个接一个地放置,并且一个不应该在另一个过程中调用。

在代码末尾,列表应按照从最小到最大的顺序排序。
我需要帮助进行交换程序。如果有人可以帮助我了解如何执行该程序的步骤,我将非常感激。 现在我的代码是:

IDEAL
MODEL small
STACK 100h
DATASEG
; --------------------------
; Your variables here
; --------------------------

arr    dw 4, 6, 2
amount dw 0

CODESEG

proc min   ;saves the offset of the smallest values in the DATASEG
    push bp
    mov bp, sp
    mov di, [bp+4]   ;di is now pointing to the first variables in the array
    mov ax, 0ffffh
    mov cx, [amount]  ;the loop will run the number of variables
lop:
    cmp ax, [di]    ;if the value in di is smaller than ax, it'll replace what's in ax
    jb lop1
    mov ax, [di]
    mov [bp+4], di  ;saves the offset of the smallest value in the array
lop1:
    add di, 2   ;incrementing the offset of the array
    loop lop
    
   pop bp
  ret   
endp min

proc swap
   push bp
   mov bp, sp
   
   pop bp
  ret
endp swap

start:
    mov ax, @data
    mov ds, ax
; --------------------------
; Your code here
; --------------------------
    xor ax, ax
    mov bx, offset arr   ;sets bx to point to the start of the array
check:
    cmp [word ptr bx], 0   ;if the value in bx is 0, it means the array is over
    jz exit_check
    add bx, 2
    inc ax
    jmp check
exit_check:
    mov [amount], ax    ;save the amount of variables in the array

    mov bx, offset arr
    push bx     ;save the offset of the array (pass by reference)
    
    call min
    call swap
    
exit:
    mov ax, 4c00h
    int 21h
END start
algorithm sorting assembly tasm
1个回答
0
投票

过程 1(最小值)接收列表中需要在按引用传递中排序的第一个元素的地址。它还在按值传递中接收元素数量,并应返回最小元素的位置。

  • 对于“按引用传递”,您使用堆栈上的参数,而对于“按值传递”,您只需使用一些全局变量。我希望您不要使用这种奇怪的混合调用约定并将元素的数量也放入堆栈中。
  • 当前需要 1 个堆叠参数的 min 过程会将该参数留在堆栈上。要么让调用者使用
    add sp, 2
    将其删除,要么让被调用者使用
    ret 2
    将其删除。
  • 现在,您将结果返回到(唯一)参数所在的堆栈上。一种更简洁的方法是返回 AX 寄存器中的地址并保持堆栈参数不变。这样做时,您可以为您的第二个过程重复使用该参数,该过程也需要相同的地址。

在下一个解决方案中,我重新使用两个参数槽。

proc min
  push bp
  mov  bp, sp
  push bx

  mov  cx, [bp + 6]   ; Number of elements to check
  mov  bx, [bp + 4]   ; Address of the array
  mov  ax, 0FFFFh
lop:
  cmp  ax, [bx]
  jb   lop1
  mov  ax, [bx]
  mov  [bp + 6], bx   ; Address of the smallest element
lop1:
  add  bx, 2
  loop lop

  pop  bx
  pop  bp
  ret   
endp min

呼叫者使用:

  push word ptr [amount]
  push offset arr
  call min

  ;;; push ...        ; Contains result from 'min'
  ;;; push offset arr ; Is still present
  call swap
  add  sp, 4          ; Finally discard both arguments

缺失的“交换”程序

proc swap
  push bp
  mov  bp, sp
  push bx

  mov  bx, [bp + 6]   ; Address of smallest element
  mov  bp, [bp + 4]   ; Address of the array
  mov  ax, [bx]       ; -> AX is smallest element
  mov  dx, [ds:bp]    ; -> DX is first element
  mov  [bx], dx       ; Former first element is in vacated position
  mov  [ds:bp], ax    ; Smallest element is in front

  pop  bx
  pop  bp
  ret   
endp swap

在代码末尾,列表应按照从最小到最大的顺序排序。

您需要重复上述操作。

此时,测试数据

4,6,2
将被修改为读取
2,6,4

进一步运行该代码将必须将第二个元素(即 6)视为具有 2 个元素的新数组的开始(因此比之前少 1 个)。该特定运行将产生
4,6
。与已经就位的
2
一起,您将得到排序后的数组
2,4,6

© www.soinside.com 2019 - 2024. All rights reserved.