title: Bubble Sort Assembly (MSP430)
body: <p>So I'm having trouble dictating where I should place my swap statements in my sort function. It works here and there but Maybe I've cmp.b/mov.b in the wrong memory address or in my line? I am using a TI MSP430 which is a 16 bit micro controller, so it doesn't have the same capabilities as some higher tier devices, so the code has to be somewhat simple.</p>
  
  <pre><code>;---------------------------------------------------------------------------
  ; MSP430 Assembler Code Template for use with TI Code Composer Studio
  ;
  ;
  ;---------------------------------------------------------------------------
          .cdecls C,LIST,"msp430.h"       ; Include device header file
  
  ;---------------------------------------------------------------------------
          .def    RESET                   ; Export program entry-point to
                                          ; make it known to linker.
  ;---------------------------------------------------------------------------
          .text                           ; Assemble into program memory.
          .retain                         ; Override ELF conditional linking
                                          ; and retain current section.
          .retainrefs                     ; And retain any sections that have
                                          ; references to current section.
  
  ;---------------------------------------------------------------------------
  ARY1        .set    0x0200          ;Memory allocation  ARY1
  ARY1S       .set    0x0210          ;Memory allocation  ARYS
  ARY2        .set    0x0220          ;Memory allocation  ARY2
  ARY2S       .set    0x0230          ;Memory allocation  AR2S
  RESET       mov.w   #__STACK_END,SP         ; Initialize stackpointer
  StopWDT     mov.w   #WDTPW|WDTHOLD,&amp;WDTCTL  ; Stop watchdog timer
  
  
  ;---------------------------------------------------------------------------
  ; Main loop here
  ;---------------------------------------------------------------------------
  ;Memory allocation of arrays must be done prior to the RESET &amp; StopWDT
  
          clr R4                  ;Clear Register
          clr R5                  ;Clear Register
          clr R6                  ;Clear Register
  
  SORT1       mov.w   #ARY1,  R4      ;Initialize R4 to point to ARY1 in the  
                                       memory
              mov.w   #ARY1S, R6      ;Initialize R6 to point to ARY1S in the 
                                       memory where the sorted ARY1 will be 
                                       stored
              call    #ArraySetup1    ;Create elements are store them in ARY1
              call    #COPY           ;Copy the elements from the ARY1 space 
                                       to ARY1S space
              call    #SORT           ;Calling Subroutine Sort with parameter 
                                       passing in R4 abd R6
  
  SORT2       mov.w   #ARY2,  R4      ;Initialize R4 to point to ARY2  in the 
                                       memory
              mov.w   #ARY2S, R6      ;Initialize R6 to point to ARY2S in the 
                                       memory where the sorted ARY2 will be 
                                       stored
          call    #ArraySetup2    ;Create elements are store them in ARY2
          call    #COPY           ;Copy the elements from the ARY2 space to 
                                   ARY1S space
          call    #SORT           ;Calling Subroutine Sort with parameter 
                                    passing in R4 abd R6
  
      Mainloop    jmp Mainloop            ;loop in place for ever
  
  ;Array element initialization Subroutine
  ArraySetup1 mov.b   #10,    0(R4)   ;Define the number of elements in the 
                                       array
          mov.b   #17,    1(R4)   ;store an element
          mov.b   #75,    2(R4)   ;store an element
          mov.b   #-67,   3(R4)   ;store an element
          mov.b   #23,    4(R4)   ;store an element
          mov.b   #36,    5(R4)   ;store an element
          mov.b   #-7,    6(R4)   ;store an element
          mov.b   #44,    7(R4)   ;store an element
          mov.b   #8,     8(R4)   ;store an element
          mov.b   #-74,   9(R4)   ;store an element
          mov.b   #18,    10(R4)  ;store an element
          ;store the rest of the 10 elements
          ret
  
  ;Array element initialization Subroutine
   ArraySetup2    mov.b   #10,    0(R4)   ;Define the number of elements in 
                                            the array
          mov.b   #54,    1(R4)   ;store an element
          mov.b   #-4,    2(R4)   ;store an element
          mov.b   #-23,   3(R4)   ;store an element
          mov.b   #-19,   4(R4)   ;store an element
          mov.b   #-72,   5(R4)   ;store an element
          mov.b   #-7,    6(R4)   ;store an element
          mov.b   #36,    7(R4)   ;store an element
          mov.b   #62,    8(R4)   ;store an element
          mov.b   #0,     9(R4)   ;store an element
          mov.b   #39,    10(R4)  ;store an element
  
          ;store the rest of the 10 elements
          ret
  
   ;Copy original Array to allocated Array-Sorted space
   COPY       mov.b   0(R4), R10      ;save n (number of elements) in R10
          inc.b   R10             ;increment by 1 to account for the byte n to 
                                    be copied as well
          mov.w   R4, R5          ;copy R4 to R5 so we keep R4 unchanged for 
                                   later use
          mov.w   R6, R7          ;copy R6 to R7 so we keep R6 unchanged for 
                                   later use
  LP          mov.b   @R5+, 0(R7)     ;copy elements using R5/R7 as pointers
          inc.w   R7
          dec     R10
          jnz LP
          ret
  
  ;Sort the copy of Array saved in the allocated Array-Sorted space, while 
  keeping original Array unchanged
  ;replace the following two lines with your actual sorting algorithm.
  ;Sort the copy of Array saved in the allocated Array-Sorted sapce, while 
  keeping original Array unchanged
  SORT
          clr R7
          clr R8
          clr R9
          clr R10
          clr R11
          clr R12
          clr R13
          clr R14
          clr R15
          mov.b R5, R11     ;counter
          dec R11
          mov.b @R4+, R7
          mov.b @R4, R8
          cmp.b R7,R8
    *         jz    noswap
    *         jge   No_Swap
          mov.b R7, R9
          mov.b R8, R7
          mov.b R9, R8
          inc R10
          mov.b r8, 0(r4)
          mov.b r7, -1(r4)
          dec R11
          tst R11
      *   jnz Swap
          cmp.w #0, R10 ; swap counter
       *   jnz Swap
          ret
  ;To bubble sort, you need to scan the array n-1 times,
  ;In every scan, you compare from top down each two consecutive elements, and 
  you swap them if they are not in ascending order.
  ;Notice that in the first scan you get the largest element (no matter where 
  it is in the array) pushed all the way to the bottom.
  ;So your next scan should be n-1 iterations, and then n-2 and so on.
  ;So every time you come back to the top of the array for a new scan, your n 
   (the number of comparisons) must be decremented by 1.
  ;In the last scan, you need only one comparison.
  ;Hints:
  ;Your sorting algorithm starts with R6 as a pointer to the array
  ;you need to save n (number of elements) in R8, then decrement it by 1 (n-1) 
  to become the number of comparisons.
  ;Copy R6 to R7 so you keep R6 unchanged as it points to the top of the array 
  for every new scan.
  ;Copy n-1 to R9 and use R9 as a loop counter, while keeping the current n-1 
  value in R8 for the next scan.
  ;In the scan loop get an element and auto increment pointer R7, then get 
  next element without changing R7.
  ;Compare the two elements, if not in ascending order, swap them.
  ;Repeat the scan from the top as pointed to by (R6), and every time 
   decrement the number of comparisons (R8).
  
   ;--------------------------------------------------------------------------
   ; Stack Pointer definition
   ;--------------------------------------------------------------------------
          .global __STACK_END
          .sect   .stack
  
   ;--------------------------------------------------------------------------
   ; Interrupt Vectors
   ;--------------------------------------------------------------------------
          .sect   ".reset"                ; MSP430 RESET Vector
          .short  RESET
  </code></pre>