首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >程序集8086 16位中的数组排序

程序集8086 16位中的数组排序
EN

Stack Overflow用户
提问于 2022-11-24 20:32:36
回答 1查看 20关注 0票数 1

我试图通过使用在数组中找到最小数字的函数对数组进行排序,而其他函数则交换两个变量。但由于某种原因,数组没有改变,保持不变。我想我有问题,但我找不到它。

这是我的代码:对不起,它太长了,没有组织起来。我刚开始集会。`

代码语言:javascript
复制
org 100h
    
     jmp start
        array db 1,9,3,6,3 **;should get 1,2,3,6,9**
        min_in_array dw ?
        
    start: 
       
       lea si, array         
       push 5 
       push si
       call sortArray 
       pop bx
       pop bx
       mov ah, 0
       int 16h
       ret
        

;doesn't work
PROC sortArray
    push bp
    mov bp, sp
    
    mov dx, 0 ;the index
    mov cx, [bp+6];size
    mov di, [bp+4];array  
    
    loop_arr: 
        add di, dx
        push di
        call findMin
        pop di
        
        
        sub di, dx
         
        add di, min_in_array 
        push dx        
        push di
        call swap
        pop di
        pop dx
        sub di, min_in_array
        inc dx
        
        mov ax, [di]
        loop loop_arr
        
   
     mov sp, bp
     pop bp
   
     ret
ENDP sortArray    


;works
PROC findMin
    push bp
    mov bp, sp
    sub sp, 4
    
    mov cx, 0 ;init the counter
    mov di, [bp+4]
    mov al, [bp-2] ;initialising the min save var
    mov al, [di] 
    
    mov bx, [bp-4] ;the index to save
    mov bx, 0
    
    run:
        cmp al, [di] 
        ja change_min 
        cmp cx, 4 ;check if cx is lower than the size of the array
        inc cx ;+1
        inc di ;move forward in the array
        jb run ;check again
        jmp fin ;finished - cx = size
        
    change_min:        
        mov al, [di] ;change the min
        mov bx, cx  ;assign the index
        inc di 
        cmp cx, 4  
        je fin
        inc cx
        jmp run
    
         
    fin: 
        mov sp, bp
        pop bp
        mov cx, 0
        
        mov min_in_array, bx
        ret 
ENDP findMin                               


;function works
PROC swap       
    ;creates fram  
    push    bp
    mov     bp,sp  
    
    sub sp,2 ;make space for local temp 
    mov bx, [bp+6]
    mov cx, [bp+4]
    ;swaping using the temp varaiable
    mov [bp-2], bx
    mov bx, cx
    mov cx, [bp-2]
         
    ;close frame
    mov sp, bp
    pop bp
    ret
ENDP swap  

`

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-24 22:45:47

,我对堆栈有问题,但我找不到。

您对堆栈的使用实际上是很好的:序言、结尾、清理。

但是还有一件事情是堆栈所擅长的,那就是保存寄存器值。您的sortArray过程依赖于CX寄存器,但是您破坏了它在findMin和交换过程中的值!

您说findMin有效,但我向您保证它不起作用。除了不保存CX和包含大量未使用的代码之外,findMin总是处理5个元素,即使提供的地址与每次调用一起移动,这意味着您正在处理内存中原始数组后面的垃圾。此外,存储在min_in_array变量中的结果是数组未排序分区中的偏移量,但返回后,sortArray将使用与原始数组中的偏移量相同的值。不能那样工作..。

你说交换有效,但我向你保证它不起作用。你提供给这个过程的是原始数组中的一个偏移量和原始数组中的一个(错误计算的)地址。您从堆栈中获取这些参数,并且只在寄存器中交换这些参数,仅此而已。您从不在数组中读取/写入,因此不会发生交换。

看看下一段代码是否指向正确的方向:

代码语言:javascript
复制
    add  di, dx
    push cx         ; Number of elements in the unsorted partition
    push di         ; Address of the start of the unsorted partition
    call findMin    ; -> AX (BL CX SI)
    pop  di
    pop  cx

    push ax         ; Address of the Min from the unsorted partition
    push di         ; Address of the start of the unsorted partition
    call swap
    ...

; IN () OUT (ax) MOD (bl,cx,si)
PROC findMin
    push bp
    mov  bp, sp
    mov  cx, [bp + 6]
    mov  si, [bp + 4]
  min:
    mov  bl, [si] 
    mov  ax, si
  run:
    dec  cx
    jz   done
    inc  si
    cmp  bl, [si] 
    jb   run
    jmp  min                 ; Go change min
  done:
    pop  bp
    ret 
ENDP findMin
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74565968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档