首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >x86程序集-需要对两个给定数组进行冒泡排序的程序的帮助

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

Stack Overflow用户
提问于 2022-10-19 21:22:22
回答 1查看 131关注 0票数 1

我正在制作一个x86汇编程序,它利用气泡排序算法详细在欧文汇编教科书(为x86系统)按升序对两个给定的数组进行排序。我的第二个过程搜索每个数组并输出每个数组中最大的值。我知道我忘记了一个跳转命令(sortAscending过程),虽然我不知道该把它放在哪里来完成我需要做的事情。当我用VS2022构建我的ASM文件时,我会收到以下错误:

(x86)\MSBuild\Microsoft.Cpp\v4.0\V140\BuildCustomizations\masm.targets(50,5):1>C:\Program MSB3721:命令"ml.exe /c /nologo /Sg /Zi /Fo"Debug\Template.obj“/Fl”Project.lst /I "C:\irvine“/W3 /errorReport:prompt /TaTemplate.asm”与代码1一起退出。

我不确定是否有其他错误,但如果有人注意到了,请指出。我的代码如下所示。

代码语言:javascript
运行
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-19 21:56:33

mov eax,esi;获取当前数组值

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

代码语言:javascript
运行
复制
mov  eax, [esi]

getLargest代码可以简单地获取最后一个数组元素,因为数组得到了优先排序。

代码语言:javascript
运行
复制
mov  eax, [esi + ecx * 4 - 4]

您的消息还谈到了无符号结果,但是排序算法将这些元素视为签名的dword!对于未签名的,请使用ja (JumpIfAbove)。

您的sift代码有错误:

代码语言:javascript
运行
复制
    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进行减量,并通过loop获取结果,那么loop指令就足够了。

找到最大元素的代码如下所示:

代码语言:javascript
运行
复制
 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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74132050

复制
相关文章

相似问题

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