我正在制作一個 x86 匯編程式,該程式利用Irvine Assembly 教科書(適用于 x86 系統)中詳述的冒泡排序演算法按升序對兩個給定陣列進行排序。我的第二個程式搜索每個陣列并輸出每個陣列中的最大值。我知道我忘記了一個跳轉命令(sortAscending程序),盡管我不確定將它放在哪里來完成我需要做的事情。當我在 VS2022 中構建我的 ASM 檔案時,我收到以下錯誤:
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\BuildCustomizations\masm.targets(50,5):錯誤MSB3721:命令“ml.exe /c /nologo /Sg / Zi /Fo"Debug\Template.obj" /Fl"Project.lst" /I "C:\irvine" /W3 /errorReport:prompt /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
uj5u.com熱心網友回復:
mov eax,esi ; get current array value
您忘記了加載存盤在地址 ESI 中的值的方括號:
mov eax, [esi]
假設陣列升序排序,getLargest 代碼可以簡單地獲取最后一個陣列元素。
mov eax, [esi ecx * 4 - 4]
您的訊息還談論未簽名的結果,但排序演算法將元素視為已簽名的雙字!對于無符號,使用ja(JumpIfAbove)。
您的篩選代碼有錯誤:
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計數與地址進行比較!不能作業!只需洗掉這 2 行。
如果loop您在開始回圈之前預先遞減 ECX 并通過mov ebx, [esi].
找到最大元素的代碼可能如下所示:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/519517.html
標籤:Intel Collective 数组排序部件x86冒泡排序
上一篇:程式集將文本存盤到外部檔案中
下一篇:交換陣列內容時的效率
