我的老师给我们布置了一项作业,要求我们编写对数字进行排序的源代码。
该代码应该由 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
过程 1(最小值)接收列表中需要在按引用传递中排序的第一个元素的地址。它还在按值传递中接收元素数量,并应返回最小元素的位置。
add sp, 2
将其删除,要么让被调用者使用 ret 2
将其删除。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
。4,6
。与已经就位的 2
一起,您将得到排序后的数组 2,4,6
。