Search code examples
assemblybinaryarmbubble-sortarmv7

RAM store binary numbers and bubble sort in assembly language


I have to do a routine using ARM v7 that store in RAM memory 10 binary numbers and then sort these numbers from high to low using bubble sort, how should I start?


Solution

  • .func
    bubbleSortAscendingU32:
        ldr     r3, [r0], #4
        mov     r1, #9*4
        mov     r12, #9*4
    1:
        ldr     r2, [r0], #4
        cmp     r2, r3
        strdlo  r2, r3, [r0, #-8]
        movhi   r3, r2
        subs    r12, r12, #4
        bgt     1b
    
        sub     r0, r0, r1
        subs    r1, r1, #4
        ldrgt   r3, [r0, #-4]
        movgt   r12, r1
        bgt     1b
    
        bx      lr
    .endfunc
    

    Assuming we are dealing with an array of uint32_t, that above might work.

    both strdlo and movhi have to be replaced for other types:

    Unsigned Descending: strdhi and movlo

    Signed Ascending: strdlt and movgt

    Signed Descending: strdgt and movlt

    The function prototype:

    void bubbleSortAscendingU32(uint32_t *pSrc);

    void bubbleSortDescendingU32(uint32_t *pSrc);

    void bubbleSortAscendingS32(int32_t *pSrc);

    void bubbleSortDescendingS32(int32_t *pSrc);


    • Do not ask me for details. I'd have to write half a book for the explanations. Follow the instructions step by step until you finally understand the flow.
    • Your professor will most probably need quite some time until he understands the routine above.
    • I doubt he will believe that you wrote this by yourself. Be honest.