汇编语言选择排序遇阻:如何查找数组中的最小有符号值?
Hey there! Let's fix your signed integer selection sort code step by step. I see you've got the basics down (jumps, subroutines), so we'll build on that to implement proper selection sorting for signed numbers.
Fixing Your Signed Integer Selection Sort in 8086 Assembly
What's Wrong with Your Current Code
Your code attempts to find a minimum value but misses core parts of the selection sort algorithm:
- No outer loop: Selection sort needs
n-1passes (for an array ofnelements) to place each element in its correct sorted position. Your code only runs once. - No tracking of the minimum's position: You update
axwith smaller values but don't remember where that minimum value lives in the array. Without this, you can't swap it into the correct spot. - Missing swap step: Even if you found the minimum, you don't swap it with the starting element of the current pass.
- Confused inner loop logic: Your
bxincrements and jump conditions don't properly traverse the rest of the array to find the true minimum.
Corrected Code with Detailed Explanations
Here's a working version tailored to your skill level, with comments to clarify each step:
[org 0x0100] jmp start data: dw -1, -7, 0, -4 ; Our signed integer array (4 elements) temp: dw 0 ; Temporary storage for swap operations start: mov cx, 3 ; Outer loop runs 3 times (n-1 for 4 elements) mov si, 0 ; SI tracks the start offset of the current unsorted portion outer_loop: mov bx, si ; BX stores the offset of the current minimum value mov ax, [data + si] ; AX holds the current minimum value (starts with first element of the pass) mov di, si ; DI starts at the current pass start to traverse the rest of the array inner_loop: add di, 2 ; Move to next element (each word = 2 bytes) cmp di, 8 ; Check if we've reached the end of the array (4*2 = 8 bytes total) je swap_min ; If end of array, jump to swap the found minimum cmp ax, [data + di] ; Compare current minimum with the next element (signed comparison) jle inner_loop ; If current minimum is <= next element, keep checking (no update needed) ; If we reach here, the next element is smaller than current minimum mov ax, [data + di] ; Update AX to the new smaller value mov bx, di ; Update BX to the offset of this new minimum jmp inner_loop ; Continue checking the rest of the array swap_min: ; Swap the minimum element (at BX) with the start of the unsorted portion (at SI) mov dx, [data + si] ; Store the start element in DX mov [temp], dx ; Save it to temporary storage mov dx, [data + bx] ; Grab the minimum element mov [data + si], dx ; Place the minimum at the start of the unsorted portion mov dx, [temp] ; Retrieve the original start element mov [data + bx], dx ; Put it in the position where the minimum was add si, 2 ; Move the start of the unsorted portion to the next element loop outer_loop ; Decrement CX, repeat outer loop if CX isn't 0 mov ax, 0x4c00 ; Exit the program int 0x21
Key Fixes to Note
- Outer Loop: Uses
cxas a counter to runn-1passes, ensuring each element gets placed in the right spot. - Inner Loop: Properly traverses the unsorted portion of the array, using
bxto track where the smallest value is located (not just its value). - Signed Comparisons: We use
jle(jump if less than or equal, for signed numbers) to correctly handle negative values—this is crucial for sorting signed integers properly. - Swap Step: The core of selection sort: once we find the minimum in the unsorted section, we swap it with the first element of that section to build our sorted array.
After running this code, your data array will be sorted in ascending order: -7, -4, -1, 0.
内容的提问来源于stack exchange,提问作者saad shan
相关产品推荐
相关产品推荐

