This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

STR instruction fails in Assembly

Hello everyone,

I have written this piece of code that implements Bubble Sort in assembly. I declared an unsorted array with 10 elements with the DCD directive and then I have made register R0 indicate to the first element of this array. Then I implemented the Bubble Sort as below:

; Bubble Sort algorithm in Assembly ;)

			AREA 	BubbleSort, CODE, READONLY
			ENTRY
			EXPORT	__main
			GLOBAL	bsort 
				
__main

bsort
			   ;Bubble sort that works on 32bit integers
			   ;R0 = array location, R1 = array size
			   
			   ;ADRL  R0, Vals                        ;R0 - array location
			   LDR    R0, =ARRAY
			   MOV    R1, #Size
			
bsort_next
			   MOV	  R2, #0        				;current element number
			   MOV    R6, #0       			        ;number of swaps
			
bsort_loop
			   ADD    R3, R2, #1    				;R3 = next element number
			   CMP    R3, R1        				;check for the end of the array(R1 = size)
		       BGE    bsort_check   				;when we reach for the end check for changes
			   LDR    R4, [R0, R2, LSL #2]          ;R4 = current element value
			   LDR    R5, [R0, R3, LSL #2]          ;R5 = next element value
			   CMP	  R4, R5                        ;Compare element values
			   STRGT  R5,[R0,R2,LSL #2]             ;If R4 > R5, store current value at next
               STRGT  R4,[R0,R3,LSL #2]             ;If R4 > R5, Store next value at current
			   ADDGT  R6, R6, #1                    ;increment swap counter
			   MOV    R2, R3                        ;advance to the next element
			   B	  bsort_loop

bsort_check
			   CMP    R6, #0                        ;Check the swap counter - check if there were swaps this iteration
               SUBGT  R1, R1, #1                    ;Optimization: skip last value in the next loop
               BGT    bsort_next                    ;If there were changes do it again else return

Stop	 	   B	   Stop

;Define size constant
Size	       EQU   10
ARRAY		   DCD    9, 1, 3, 5, 6, 4, 2, 7, 8, 0		
END
	
	
;The array
;              AREA   UNSORTEDARRAY, DATA, READWRITE
;	          ALIGN
;ARRAY		  DCD    0, 1, 3, 5, 2, 4, 6, 7, 8, 9	
;              END
     


		 

This is the unsorted array declared with the DCD directive. I can see it in SRAM:

As you can see I store the values for the current and next element in R4 and R5 registers so I can compare them.

Right below that line I compare the two and attempt to do a swap if current>next:

In the first iteration for example current(R4) is 9 and next(R5) is 1 so there should be a swap. The STRGTs below the compare execute however when I look at the RAM in the memory window nothing is changed there. I was expecting to see the swap on the memory itself but the image is exactly like the one with the memory view above. Basically no swap takes place into the memory itself even tough the two conditional STR instructions get executed.

Can someone please help me understand why this is happening so I can fix the issue? Thank you for reading!

Some remarks:

- I have initially declared the array in a separate DATA  section but I could not even see it as initialized with the memory window. I did some reading and I found out  that happens because I have not written a piece of code that would copy that array from the FLASH and store it into RAM so I have declared the array into the CODE section so I can see it with the debugger.

- I'm not using any hardware I'm using the simulator in keil for this exercise. Not sure if that is problematic or not.