Today in order to review the textbook above the procedure, on-site test the next procedure, the same year, but learned the computer principle of bubble sort, the results of transplant platform is so troublesome, hehe, online bar, forum, know, library above please your program can be reliable point!!!
The first common mistake is the program to write a mess, unrestrained, get a few bits of address and intermediate variables, plus two loops, the program flew early, it really makes us programmers ashamed AH
The second mistake is because most of the reference to the textbook title: will be the 40H first address of the N data to sort, and then dozens of lines of code, how can I quickly know the program is not
.................
In view of this, special in our green space under the two methods to achieve the bubble sort full version, the experimental program has all been debugged under the Keil. ( in honor of today's college entrance examination, crying, good refueling it )
Idea: an orderly sequence is more useful for finding. This program uses the " bubble Sort " method, the algorithm is to compare a number with the number of the following, if the number is larger than the following, then the exchange, so that all the number is compared once again, the largest number will be in the last side of the sequence. Then the next round of comparisons, find the second big data until all the data is in order.
The first method: (disadvantage is not conducive to the transplant, the advantage is the end of the number as a reference, logic simple)
///////////////////////////////
; bubble sort
; 10 numbers from small to large sort
ORG 0000H
MOV 60H, #02H To send the initial value to the 60-69H storage unit
MOV 61H, #05H
MOV 62H, #03H
MOV 63H, #07H
MOV 64H, #09H
MOV 65H, #14H
MOV 66H, #13H
MOV 67H, #08H
MOV 68H, #01H
MOV 69H, #12H
MOV R0, #60H
MOV R1, #61H
L2: MOV A, @R0
MOV 50H, @R1
Cjne a,50h,neq; Compare the size of the values in 60H and 61H, and do subtraction. If the content in 60 is large then the C bit (carry is 0) if the content in 60 is 1 than the small C bit in 61
NEQ: JNC EXCHANGE; Yes 0 jump, left operand < right operand
LJMP L3
EXCHANGE: XCH A, @R1; Exchange the contents of 60 and 61
XCH A, @R0
L3: INC R1
Cjne R1, #6AH, L2The contents of 62 to 69 are compared to 60, and the last 60 is the minimum value.
INC R0
MOV 51h,r0
MOV r1,51h
INC R1
Cjne R0, #69H, L2, the content in 61 is compared with the content from 62 to 69, and the content in 61 is the second small value, and so on, until the last two digits are compared.
SJMP $
END
///////////////////////////////
The second method (advocated by this method, only two loops, logic is strong, not easy to understand, the advantage is conducive to transplant)
///////////////////////////////
; bubble sort
; 10 numbers from small to large sort
Size equ 10; Number of data
Array equ 60h; Data Start Address
Change equ 0; Swap flags
ORG 0000H
MOV 60H, #02H To send the initial value to the 60-69H storage unit
MOV 61H, #05H
MOV 62H, #03H
MOV 63H, #07H
MOV 64H, #09H
MOV 65H, #14H
MOV 66H, #13H
MOV 67H, #08H
MOV 68H, #01H
MOV 69H, #12H
SORT:
MOV R0, #Array
MOV R7, #Size-1
CLR Change
GOON:
MOV A, @R0
MOV R2, A
INC R0
MOV B, @R0
Cjne A, B, notequal
SJMP Next
NotEqual:
JC Next; Before small after large, not exchanged
SETB change; Front large rear small, place swap flag
XCH A, @R0; Exchange
DEC R0
XCH A, @R0
INC R0
Next:
DJNZ R7, GOON
JB Change, SORT
LJMP $
End
///////////////////////////////
51 single-chip microcomputer assembly for bubbling Sorting