汇编语言:使用有符号(负数)和正数按升序排序。

时间:2015-11-30 23:01:02

标签: assembly

鉴于存储在外部存储器中的4个数字,其中一些数字带有符号。进行排序,首先是负数,然后是正数。按升序排列。在外部存储器中按顺序存储它们。

程序是按升序编写的,但结果是负数跟随正数。

汇编程序8051(mcs-51)

代码:

        mov DPTR, #00h             ;Initial address of data pointer.

        mov A, #35d                                          
        movx @DPTR, A              ;Moves a value from accumulator into DPTR.
        inc DPTR                   ;The increment of address pointer.

        mov A, #-12d                                       
        movx @DPTR, A
        inc DPTR

        mov A, #-23d
        movx @DPTR, A
        inc DPTR

        mov A, #15d  
        movx @DPTR, A       

 ;Sorting of data will be in internal memory, after finishing, soting data will be removing to external memory again.

  sort:                            ;Start a loop of sorting by sort.
        mov R0,#28h                ;Stores address of value in proccess on DATA(28h...22h).
   lp1:   
        mov 4Ah, #00h              ;Stores a largest number.
        mov R2, DPL                ;Stores a values from DPL to remember the address of largest element.
        jmp search                 ;Jump to search.
   rt1:
        mov DPL, R2                ;Sets DPTR a lower byte to R2 (DPTR now is equal R2)
        mov A, #00h
        dec DPL                    ;The decrement of DPL.
        movx @DPTR, A 
        mov @R0, 4Ah
        dec R0
        cjne R0, #24h, lp1         ;Compare, and if not equals, jump to lp1.
        jmp data_to_xdata  
search:
        mov DPTR, #00h           
   lp2:                           
        movx A, @DPTR           
        inc DPTR
        mov R1, DPL                ;R1 as a local loop counter.
        cjne A, 4Ah, lp3                   
   rt2:
        cjne R1, #0Ah, lp2
        jmp rt1 
   lp3:
        jc  rt2                    ;Jump to rt2 if carry set equals 1.
        mov 4Ah, A
        mov R2, DPL
        jmp rt2

;Sorting fihished, so data are removing to external memory.
 xdata:                          

        mov DPTR, #00h
        mov R0,#22h
   lp4:  
        mov A, @R0                 
        movx @DPTR, A
        inc DPTR
        inc R0
        cjne R0, #4Ah, lp4      
   end

1 个答案:

答案 0 :(得分:2)

如果使用无符号比较,则症状与签名比较时的症状完全一致。

我对8051已经30多年了,但据我所知,所有的比较都是管理数据索引。如果其中一个是数据比较,则可以是从无符号更改为已签名的数据。

相关问题