【发布时间】:2021-08-20 01:55:54
【问题描述】:
我想在使用堆栈时对数组进行排序:
所以首先我将它全部推入堆栈。
然后我在堆栈中找到最小元素,将其与弹出的顶部元素交换并将其移回数组。
但有些地方不对劲,大约一半就搞砸了(DI 比实际应该低一个索引)
data segment
a db 5h,12h,6h,4h,9h
len equ $-a
loop_counter db len
min_index dw 0
min db ?
ends
stack segment
dw 128 dup(0)
ends
code segment ;TODO: Locate min in stack xchg it with top element copy to array and pop it :|
start:
mov ax, data
mov ds, ax
mov bx,0
mov cx,0
mov cl,loop_counter
push_arr_to_stack:
mov al,a[bx]
push ax
inc bx
loop push_arr_to_stack
mov bx,len-1
mov ax,0
mov cl,loop_counter
mov bp,sp
mov ax,[bp]
mov bx,[bp]
mov di,0
mov dx,0
mov si,2
mov di_check,len-loop_counter
find_min_to_arr:
cmp bx,ax
jb new_min
mov bx,[bp+si]
add si,2
loop find_min_to_arr
mov min,al
xchg a[di],al
jmp pop_min
new_min:
mov ax,bx
mov bx,[bp+si]
mov min_index,si
sub min_index,2
add si,2
dec cx
jmp find_min_to_arr
pop_min:
mov dx,[bp]
xchg di,min_index
mov [bp+di],dx
xchg di,min_index
pop dx
mov dx,0
inc di
mov si,0
cmp di,len
je b
dec loop_counter
mov cl,loop_counter
jmp find_min_to_arr
b:
mov ax, 4c00h ; exit to operating system.
int 21h
ends
end start
【问题讨论】:
-
是时候学习如何调试您的代码,以及如何在监控寄存器及其值(以及内存中的数组本身)的同时逐条指令执行它。
-
这就是我所做的——我就是这样注意到错误的:|
标签: arrays sorting assembly stack tasm