assembly x86程序集-需要使用冒泡排序对两个给定数组进行排序的程序的帮助

hc8w905p  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(166)

我正在编写一个x86汇编程序,该程序利用bubble-sort algorithm detailed in the Irvine Assembly textbook (for x86 systems)对两个给定的数组按升序排序。我的第二个过程搜索每个数组并输出每个数组中的最大值。我知道我忘记了一个跳转命令(sortAscending过程),但我不确定将其放置在何处才能完成所需的工作。当我在VS 2022中构建ASM文件时,收到以下错误:
1〉C:\程序文件(x86)\MSBuild\Microsoft.Cpp\v4.0\V140\构建自定义\masm. targets(50,5):错误MSB 3721:命令“ml. exe/c /nologo /Sg /Zi /Fo“调试\模板.obj”/Fl“项目.lst”/I“C:\irvine”/W3 /错误报告:提示符/TaTemplate. asm”已退出,代码为1。
我不确定是否还有其他错误,但是如果有人注意到了,请指出它们。我的代码如下所示。

TITLE Sort Arrays, Version 1  (SortArrays.asm)

; This program sifts through and 
; sorts arrays (in place) in ascending
; order and outputs the greatest value 
; in each array.
; Name: Kasizah
; Date: 10-19-2022

INCLUDE Irvine32.inc

.data
Array1  DWORD 0C0D12AFh, 00030256h, 0FFAABBCCh, 0F700F70h, 00000000h, 0E222111Fh, 0ABCDEF01h, 01234567h
Array2  DWORD 61A80000h, 024F4A37h, 0EC010203h, 0FAEEDDCCh, 2C030175h, 84728371h, 63AA5678h, 0CD454443h, 22222222h, 61B1C2D3h, 7A4E96C2h, 81002346h, 0FDB2726Eh, 65432100h, 0FFFFFFFFh

; message strings
largestUnsignedS BYTE "The largest unsigned value in the array is: ",0
largestUnsignedF BYTE ".",0

.code
main PROC
    mov  esi,OFFSET Array1   ; point to start of Array1
    mov  ecx,LENGTHOF Array1 ; get length of Array1
    call sortAscending       ; sort Array1
    ; display sorted array using DumpMem
    mov  esi,OFFSET Array1   ; starting OFFSET
    mov  ecx,LENGTHOF Array1 ; number of units
    mov  ebx,TYPE Array1     ; doubleword format
    call DumpMem
    ; get greatest value in Array1
    mov  esi,OFFSET Array1   ; point to start of Array1
    mov  ecx,LENGTHOF Array1 ; get length of Array1
    call Crlf                ; skip line
    call getLargest          ; display largest value in Array1
    call Crlf                ; skip line
    
    mov  esi,OFFSET Array2   ; point to start of Array2
    mov  ecx,LENGTHOF Array2 ; get length of Array2
    call sortAscending       ; sort Array2
    ; display sorted array using DumpMem
    mov  esi,OFFSET Array2   ; starting OFFSET
    mov  ecx,LENGTHOF Array2 ; number of units
    mov  ebx,TYPE Array2     ; doubleword format
    call DumpMem
    ; get greatest value in Array2
    mov  esi,OFFSET Array2   ; point to start of Array2
    mov  ecx,LENGTHOF Array2 ; get length of Array2
    call Crlf                ; skip line
    call getLargest          ; display largest value in Array2
    call Crlf                ; skip line
    
    exit                     ; exit the program
main ENDP
;-------------------------------------------------------
sortAscending PROC
;
; Sorts an array of 32-bit signed integers in ascending
;   order using the bubble sort algorithm.
; Receives: pointer to array, array size
; Returns: nothing
;-------------------------------------------------------
    mov  edi,esi            ; duplicate "point to first value" 
                            ; (used to reset ESI)
    dec  ecx                ; decrement ECX (count) by 1
    outer_loop:
        push ecx                ; save outer loop count
        mov  esi,edi            ; point to first value
        sort:
            mov  eax,esi            ; get current array value
            cmp  [esi+4],eax        ; compare [ESI] and [ESI+4]
            jg   next               ; if [ESI] <= [ESI+4], no swap
            xchg eax,[esi+4]        ; else swap pair
            mov  [esi],eax
        next:
            add  esi,4              ; move both pointers forward
            loop sort               ; inner loop
            
            pop  ecx                ; retrieve outer loop count
            loop outer_loop         ; else repeat outer loop
    quit:
        ret
sortAscending ENDP
;-------------------------------------------------------
getLargest PROC
;
; Searches an array for its largest 
;   value.
; Receives: ESI - pointer
; Returns: statement of largest value in array
;-------------------------------------------------------
    sift:
        mov  eax,[esi]              ; move [ESI] into EAX 
        cmp  [esi+4],eax            ; compare EAX with [ESI+4]
        jg   next                   ; if EAX >= [ESI+4] don't replace
        mov  eax,[esi+4]            ; mov [ESI+4] into EAX
    next:
        add  esi,4                  ; move pointer forward
        cmp  ecx,esi                ; make sure that esi isn't at the end of array
        je   quit                   ; jump to quit if at the end of array
        loop sift                   ; else return to sift loop
    quit:
        mov  ebx,eax                ; move EAX into EBX temporarily
        mov  eax,largestUnsignedF   ; move largestUnsignedF string into EAX
        call WriteString            ; display largestUnsignedF string
        mov  eax,ebx                ; move EBX into EAX
        call WriteInt               ; display EAX
        mov  eax,largestUnsignedS   ; move largestUnsignedS string into EAX
        call WriteString            ; display largestUnsignedS string
        ret
getLargest ENDP
END main
pftdvrlh

pftdvrlh1#

mov  eax,esi            ; get current array value

您忘记了加载存储在地址ESI中的值的方括号:

mov  eax, [esi]
  • getLargest* 代码可以简单地获取最后一个数组元素,因为数组是按升序排序的。
mov  eax, [esi + ecx * 4 - 4]

另外,您的消息谈到了unsigned结果,但是排序算法将元素视为signeddwords!对于unsigned,请使用ja(JumpIfAbove)。
您的 sift 代码有错误:

cmp  ecx,esi                ; make sure that esi isn't at the end of array
    je   quit                   ; jump to quit if at the end of array
    loop sift                   ; else return to sift loop
quit:

cmp ecx, esi比较一个计数和一个地址!不能工作!只删除这两行。
如果在开始循环之前预先递减ECX,并且通过mov ebx, [esi]获取结果,则loop指令就足够了.
查找最大元素的代码如下所示:

mov  eax, 80000000h ; Smallest signed dword
More:
 cmp  [esi], eax
 jng  Skip
 mov  eax, [esi]
Skip:
 add  esi, 4
 dec  ecx
 jnz  More
 mov  ebx, eax        ; The signed maximum

相关问题