汇编语言数组上的冒泡排序

时间:2015-05-24 19:22:02

标签: arrays assembly x86 bubble-sort

我需要Bubblesort一个无组织的数组,其中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

4 个答案:

答案 0 :(得分:2)

对于第一个错误,您忘记在寄存器和立即数之间键入逗号。

对于第2和第3错误,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

答案 1 :(得分: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               ; outer loop iteration count

nextscan:                ; do {    // outer loop
    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       ; } while(--cx);



;;; 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 

答案 2 :(得分: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 

答案 3 :(得分:-1)

.model small

。数据

arr1 db 6,5,8,3,9

len1 equ $ -arr1

.CODE

mov ax,@ data

mov ds,ax

mov ch,len1-1

A1:

mov cl,ch

lea si,arr1

REPT1:

mov al,[si]

inc si

cmp al,[si]

jbe next1

xchg al,[si]

mov [si-1],al

NEXT1:

dec cl

jnz rept1

dec ch

jnz a1

mov啊,4ch

int 21h