I need to sort the array in one proc and than the smallest number should swap its place with the first number . than the second smaller number with the second number and so on... My just wrote 00 00 00 00 in ds:0000
proc min
push bx
push ax
mov si,bx
inc bx
checkminnum: ; the outer loop for checking all the numbers
mov al, [byte ptr si]
cmp [byte ptr bx] , al
jb smaller
mov si, bx; the offset of the smaller num is saved in si
smaller:
inc bx
cmp [byte ptr bx], '$' ; check when the loop should end
jne checkminnum
pop ax
pop bx
ret
endp min
proc swap
push bx
mov bx, offset array
repos:
call min
mov al, [byte ptr bx]
push ax
mov al, [byte ptr si]
mov [byte ptr bx], al
pop ax
mov [byte ptr si], al
inc bx
cmp [byte ptr bx], '$'
jne repos
pop bx
ret
endp swap
CodePudding user response:
The min proc exhibits these two errors:
- You are using the opposite of the condition that you would need in order to find the minimum.
- Your code will, in its final iteration, treat the $-terminator as if it were an array element. You must check for the terminator before comparing with a following array element because said element might not exist.
What the code does is InsertionSort in the ascending order.
; IN (bx) OUT (al='$',si) MOD ()
proc min
push bx
SetMin:
mov si, bx ; Position of minimum
Cont:
inc bx
mov al, [bx]
cmp al, '$'
je Done
cmp al, [si] ; Compare with current minimum
jb SetMin ; Smaller, so record position
jmp Cont ; Not smaller, so just continue
Done:
pop bx
ret
endp min
In the swap proc, if the position of the minimum is equal to the current front of the array, it's better to not swap elements at all.
And instead of using the stack and work with a single register AL
, use extra registers like CL
and DL
for efficiency.
; IN () OUT () MOD (al,cl,dl)
proc swap
push bx
push si
mov bx, offset array
More:
call min ; -> AL='$' SI
cmp si, bx
je Skip
mov cl, [bx]
mov dl, [si]
mov [bx], dl
mov [si], cl
Skip:
inc bx
cmp [bx], al ; AL='$'
jne More
pop si
pop bx
ret
endp swap
A drawback of using a terminator for working with your array is that the value 36 (ASCII code of the $-character) can not appear in the array itself.