我的氣泡代碼是彙編語言嗎? (is my code for bubble right at assembly language?)


問題描述

我的氣泡代碼是彙編語言嗎? (is my code for bubble right at assembly language?)

org 100h

.Model SMALL 
.Stack
.Data 

  Ar DB 06h,05h,04h,02h

  Len DW ($‑Ar)

.CODE  

  Mov Ax,@Data
  Mov Ds,Ax
  Mov Bx,Len   ;length of the array 
  Dec Bx       ;total number of passes ( n‑1) 

Nxtpass:

  Mov Cx,Bx    ;total number of comparison 
  Mov Si,0     ;index=0

Nxtcomp:Mov Al,Ar[Si] ;read 1st number    
       Inc Si        ;update pointer
       Cmp Al,Ar[Si] ;compare 1st number with 2nd number
       Jb  No_EXCG   ;if same , noexchange 


      XCHG Al,Ar[Si]
      Mov Ar[Si‑1],Al ; do exchange

No_EXCG:loop Nxtcomp  ;repeat until comparison
        Dec  Bx
        JNZ  Nxtpass  ;repeat untill pass = 0; 


ret

參考解法

方法 1:

Your BubbleSort is good, but you could make it more secure if it could deal with an array of just one element, or even zero elements!
The code that will test for this can be combined with the outer loop logic:

    jmp   Start
Nxtpass:
    ...
Start:
    sub   bx, 1
    jnbe  Nxtpass
    ret

If there are no elements, the sub bx, 1 instruction will make CF=1 aka "Below".
If there's but 1 element, the sub bx, 1 instruction will make ZF=1 aka "Equal".
The jnbe Nxtpass instruction will only jump on the condition "NeitherBelowNorEqual" also known as "Above".


Cmp Al,Ar[Si] ;compare 1st number with 2nd number
Jb  No_EXCG   ;if same , noexchange 

The comment is misleading! Exchanging elements can be skipped on "Below" but also on "Equal". Better use jbe No_EXCG.


Programming style is personal but writing the mnemonics and register names with a leading capital is very, very unusual.

.Model SMALL 
.Stack
.Data 

  Ar db 06h, 05h, 04h, 02h
  Len equ $‑Ar

.Code

    mov   ax, @Data
    mov   ds, ax
    mov   bx, Len
    jmp   Start

Nxtpass:
    mov   cx, bx
    xor   si, si
Nxtcomp:
    mov   al, Ar[si]
    inc   si
    cmp   al, Ar[si]
    jbe   No_EXCG
    xchg  al, Ar[si]
    mov   Ar[si‑1], al
No_EXCG:
    loop  Nxtcomp
Start:
    sub   bx, 1
    jnbe  Nxtpass
    ret

(by DurjoySep Roland)

參考文件

  1. is my code for bubble right at assembly language? (CC BY‑SA 2.5/3.0/4.0)

#emu8086 #assembly #x86-16






相關問題

使用 include 'emu8086.inc' 反轉字符串的大小寫和順序 (Reverse case and order of a String using include 'emu8086.inc')

emu8086 : ARR 不包含任何值 (emu8086 : ARR do not contain any value)

為什麼這個彙編代碼不刪除擴展名為“.lnk”的文件? (Why doesn't this assembly code delete the file with ".lnk" extension?)

將 C 代碼轉換為 8086 程序集 (Converting C code into 8086 assembly)

循環無法打印字母 (Loop does not work to print alphabets)

如何使用 call 和 ret 更改堆棧內容? (how to change stack content with call and ret?)

彙編語言中的putch函數通過堆棧 (putch function in assembly language through stack)

如何在不使用 HLT 的情況下對程序進行 HLT (How to HLT the program without using HLT)

使用 8086 彙編計算 10 的階乘 (Computing the factorial of 10 using 8086 assembly)

你好,我有一個問題,我需要從用戶那裡得到輸入,輸入是一個數字和一個數字後的數字,數字可以是雙字 (hello , i got a question that i need to get input from user, the input is a number and digit after digit ,the number can be a doubleWord)

在彙編語言中得到錯誤的結果 (Getting wrong result in Assembly language)

我的氣泡代碼是彙編語言嗎? (is my code for bubble right at assembly language?)







留言討論