1. m8763

    Thread Starter New Member

    Dec 2, 2008
    I'm trying to write a quicksort algorithm in assembly for the 8051. I want to sort a list of 8-bit unsigned numbers stored in external RAM. The resulting list should be sorted in increasing order.
    Bellow is the initial code and comments about what should be there. Can anyone help me out with this?

    $include (c8051f120.inc) ; Include register definition file.

    ; Data Segment

    dseg AT 40H
    i: DS 1
    list: DS 8 ;creates a list of 8 bits

    ; Reset Vector
    cseg AT 0
    ljmp Main ; Locate a jump to the start of code at
    ; the reset vector.

    mov WDTCN, #0DEh
    mov WDTCN, #0ADh

    mov R0, #0h ; set as temp storage
    mov R1, #0h ; set
    mov R2, #0h ; set
    mov R3, #0h ; set
    mov R4, #0h ; set
    mov R5, #0h ; pivot value
    mov R6, #0h ; first register
    mov R7, #0h ; last register

    assign: ;sets the pointer and list
    mov DPTR,#list ; creates a starting address
    mov A,#3
    movx @DPTR,A
    inc DPTR
    mov A,#8

    mov A,#1
    movx @DPTR,A
    inc DPTR

    mov A,#6
    movx @DPTR,A
    inc DPTR

    mov A,#2
    movx @DPTR,A
    inc DPTR

    mov A,#8
    movx @DPTR,A
    inc DPTR

    mov A,#7
    movx @DPTR,A
    inc DPTR

    mov A,#4
    movx @DPTR,A
    inc DPTR

    mov A,#5
    movx @DPTR,A

    mov A,DPL
    add A,i
    mov DPL,A

    mov A,DPH
    addc A,#0
    mov DPH,A

    mov R6,#0 ;the first to R6
    mov R7,#7 ;the last to R7

    push i ;push i into direct memory location

    lcall subr ; call subr: "split subroutine"


    ;sort: ;sorts--set DPTR (pivot point) and recursively chop list up into smaller pieces

    ; value - pivot --> carry = 1 --> move towards the right
    ; jc ;something to move it right

    ;if carry 0, exchange

    ;;increments the pointer to compare the next thing

    ;partition: ;controls which section/piece of the list to sort from

    ;exchange: ;our exchange or "swap" values
    ; xch A, R#1
    ; mov A, R#2

    subr: mov R0,SP ; get stackpointer
    dec R0 ; top two bytes on stack are the return address
    dec R0
    ;mov i,#0 ; initialize i
    loop: inc @R6 ; inc value @R6--"first+1"
    push R6 ; push R6 to direct memory location
    ;mov A,i ; get i
    ;inc @R6 ; first+1
    cjne A,R6,endsplit ;;;;;ask for how to compare two elements in the stack
    ; go to endsplit if not equal
    ; go on to lp2 if statement satisfied

    lp2: mov A,#list ; move list to acc
    add A,i ; list
    mov R1,A ; list --> A
    mov A,@R1 ; list --> R1
    push R1 ; list --push--> stack

    add R1,#1

    ; cjne A,x,then ;;;;ask for how to compare two elements in the stack
    ; jmp loop

    ;then: mov A,splitpoint
    ; add A,#1 ; splitpoint+1
    ; mov splitpoint,A ; splitpoint = splitpoint+1
    ; xch goes here
    ; mov A,i
    ; cjne A,last,loop ; for-loop -- if i ~= last, go back to 'loop'

    endsplit ;xch ; swap list[first] and list[splitpoint]