2015-05-24 120 views
1

我需要冒泡從最大的7個整數最小的一個無組織的陣列,它看起來像9,6,5,4,3,2,1。
我跑我的代碼經過編譯器和它說冒泡排序的彙編語言

Compiler Error

我不明白這有什麼代碼的問題:你忘了輸入一個

code segment 
assume ds:code,cs:code 
start: 
mov ax,code 
mov ds,ax ;code start 
ARR: dw 1,2,4,3,6,5,9 
mov ch,0h 
mov cl,1h 
mov bh 7h 
jmp assign_nums 
restart: 
mov ch,0h 
mov cl,1h 
dec bh 
jmp assign_nums 
swap: 
mov ch,dl 
mov cl,dh 
jmp next 
next: 
cmp bh,cl 
je restart 
add ch,1h 
add cl,1h 
jmp assign_nums 
assign_nums: 
cmp bh,0h 
je done 
mov dh,[ARR+ch] 
mov dl,[ARR+cl] 
cmp dh,dl 
jl swap 
jnl next 
done: 
nop 
code ends 
end start 

回答

2

對於第一個錯誤註冊和直接之間的逗號。

對於第二和第三誤差CH和CL寄存器不能用於尋址存儲器。改用SI,DI或BX。

由於你的數組定義爲話,你必須把它當作這樣的!
變化

mov dh,[ARR+ch] 
mov dl,[ARR+cl] 

成類似(取決於其他選擇,你做)

mov ax,[ARR+si] 
mov dx,[ARR+di] 

請注意,放置在陣列之中的說明。一旦你設法編譯它,這會使你的程序崩潰。將陣列放置在程序的單獨數據段中或跳過此行。

start: 
mov ax,code 
mov ds,ax 
jmp start2 
ARR: dw 1,2,4,3,6,5,9 
start2: 
mov ch,0h 
+0

所以它的一堆意大利麪代碼?有沒有更好的方法來編碼?我不知道我在這裏用匯編代碼 – Puloko

+1

來自user3144770的答案很好。我所做的唯一改變不是讓您選擇跳過ARRAY。僅將您的ARR放入.data段 '.DATA或.FARDATA DSEG' 'ARR:dw 1,2,4,3,6,5,9' – BKCOHEN

-1

有時固定代碼是不是進行一個新的更硬,所以我創建爲降序(從大至小的)的新冒泡排序。對於這個代碼我使用了EMU8086。我添加了註釋,並試圖使它像C++或Java,希望這將有助於你瞭解它,那就是:

.model small 
.stack 100h 
.data 

arr dw 7,99,1418,0,521,66,255 ;ARRAY OF 7 INTEGERS. 
i dw ? 
j dw ? 

;----------------------------------------- 

.code 
start: 

;INITIALIZE DATA SEGMENT. 
    mov ax, @data 
    mov ds, ax     

    call bubble_sort_descending 

;WAIT FOR ANY KEY.  
    mov ah, 7 
    int 21h 

;FINISH PROGRAM. 
    mov ax, 4c00h 
    int 21h 

;----------------------------------------- 
;for (i = 0; i < len-1; i++) 
; for (j = i+1; j < len; j++) 
; if (arr[i] < arr[j]) // '<' BECAUSE IT'S ASCENDING. 
;  exchange 

bubble_sort_descending proc 
    mov i, 0    ;I = 0. 
fori: 
    mov ax, i   ;AX = I. 
    inc ax    ;I++. 
    mov j, ax   ;J = I++. 
forj: 
;GET ARR[ I ]. 
    mov si, offset arr 
    mov ax, i 
    shl ax, 1   ;I * 2, BECAUSE EVERY COUNTER IS 2 BYTES. 
    add si, ax 
    mov ax, [ si ]  ;AX = ARR[ I ]. 
;GET ARR[ J ]. 
    mov di, offset arr 
    mov cx, j 
    shl cx, 1   ;J * 2, BECAUSE EVERY COUNTER IS 2 BYTES. 
    add di, cx 
    mov cx, [ di ]  ;CX = ARR[ J ]. 
;IF (ARR[ I ] < ARR[ J ]). 
    cmp ax, cx   ;CMP ARR[ I ], ARR[ J ]. 
    jae bigger   ;IF (ARR[I] >= ARR[J]) NO EXCHANGE. 
;EXCHANGE BECAUSE ARR[ I ] IS NOT BIGGER THAN ARR[ J ]. 
    ;EXCHANGE COUNTERS IN ARR. 
    mov [ si ], cx  ;ARR[ I ] = ARR[ J ]. 
    mov [ di ], ax  ;ARR[ J ] = ARR[ I ]. 
bigger: 
;NEXT J. 
    inc j    ;J++. 
    cmp j, 7 
    jbe forj    ;IF (J <= 7) REPEAT. 
;NEXT I. 
    inc i    ;I++. 
    cmp i, 7 
    jb fori    ;IF (I < 7) REPEAT. 

    ret 
bubble_sort_descending endp  

;----------------------------------------- 

end start 
-1

這是簡單的代碼,冒泡排序

iclude'emu8086.inc' 

org 100h 
.data 

array db 9,6,5,4,3,2,1 
count dw 7 

.code 

mov cx,count  
dec cx 

nextscan:   
mov bx,cx 
mov si,0 

nextcomp: 

mov al,array[si] 
mov dl,array[si+1] 
cmp al,dl 

jnc noswap 

mov array[si],dl 
mov array[si+1],al 

noswap: 
inc si 
dec bx 
jnz nextcomp 

loop nextscan 



; this loop to display elements on the screen 

mov cx,7 
mov si,0 

print: 

Mov al,array[si] 
Add al,30h 
Mov ah,0eh 
Int 10h 
MOV AH,2 
Mov DL , ' ' 
INT 21H 
inc si 
Loop print 


ret 

0

;使用泡泡排序算法排序陣列

.MODEL SMALL 
.STACK 100H 
.DATA 
    N DB 44H,22H,11H,55H,33H  ; N is an array  
    LEN DW 5 ; LENGTH OF ARRAY N 
.CODE 
MAIN PROC 
MOV AX,@DATA 
    MOV DS,AX 

MOV CX,LEN ;Cx is counter for OUTERLOOP CX=5  
DEC CX  ; CX = 4 

OUTERLOOP: 
    MOV SI,0   ; SI is the index of array N 
    MOV DX,CX ; Dx is counter for INNERLOOP 
INNERLOOP:  
    MOV AH,N[SI] ; assign the number N[SI] into reg.AH 
    MOV AL,N[SI+1] ; assign the next number N[SI+1] into reg.AL 
    CMP AH,AL  ; Compare between N[SI] and N[SI+1] <BR> 
    JC CARRY  ; if AL > AH => Carry Flag =1 ,THEN jump to carry 
    MOV N[SI] , AL ; else , Do Switching bteween N[SI] and N[SI+1] 
    MOV N[SI+1] ,AH 
CARRY: 
    INC SI 
    DEC DX 
    JNZ INNERLOOP 
    LOOP OUTERLOOP 
;exit 
MOV AH,4CH ;service number  
INT 21H  ; interrupt 
MAIN ENDP 
END 
+0

請在您的答案中包含一些解釋並使用正確的語法高亮。 – bastelflp

+0

這對商店可以通過'mov n [si],AX'來存儲AL和AH以及一個16位存儲。 (但是[在現代英特爾CPU上,你會得到部分寄存器合併速度減慢](https://stackoverflow.com/questions/45660139/how-exactly-do-partial-registers-on-haswell-skylake-perform-writing -al-seem-to)。但是,你正在使用[一個緩慢的循環指令](https://stackoverflow.com/questions/35742570/why-is-the-loop-instruction-slow-couldnt-intel - 有效地實現)顯然你不是在現代CPU上優化性能。) –