Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 2:968db132481b, committed 2020-01-04
- Comitter:
- samvilm
- Date:
- Sat Jan 04 12:34:04 2020 +0000
- Parent:
- 1:03c191369089
- Commit message:
- Sorts array of numbers in assembly language. Also sorts same array in C to compare and ensure same sorted array is returned.
Changed in this revision
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/asm_sort.s	Sat Jan 04 12:34:04 2020 +0000
@@ -0,0 +1,33 @@
+    AREA asm_func, CODE, READONLY
+    EXPORT asm_sort
+asm_sort
+    PUSH    {R2, R3, R4, R5, LR} ; Some code goes here
+outer_loop
+    LDR R2, =0x0 ;first index
+    LDR R3, =0x1 ;second index
+    SUB R1, R1, #1 ;size of array intervaled minus one
+    CMP R1, #1 ;make sure interval has at least two elements
+    BLT done 
+inner_loop
+    CMP R3, R1 ;compare interval
+    BGT loop_exit ;make sure to restart loop if at the end of first pass
+    LDR R4, [R0, R2, LSL #2] ;numbers[first] into R4
+    LDR R5, [R0, R3, LSL #2] ;number[second] into R5
+    CMP R4, R5 ;compare numbers
+    BGT asm_swap ;if out of order swap
+    ADD R2, R2, #1 ;increment index R2
+    ADD R3, R3, #1 ;increment index R3
+    B inner_loop ; go back to keep swapping
+asm_swap
+    STR     R4, [R0, R3, LSL #2] ;swap numbers in the array
+    STR     R5, [R0, R2, LSL #2]
+    ADD R2, R2, #1 ;increment index
+    ADD R3, R3, #1;increment index
+    B inner_loop ;go back to keep swapping
+loop_exit
+    B outer_loop ;next iteration
+done
+    POP     {R2, R3, R4, R5, LR} ;free regs
+    BX      LR
+    ALIGN
+    END
\ No newline at end of file
--- a/main.cpp	Sun Jan 01 20:57:57 2012 +0000
+++ b/main.cpp	Sat Jan 04 12:34:04 2020 +0000
@@ -1,12 +1,51 @@
-#include "mbed.h"
+ #include "mbed.h"
+// Function prototypes
+extern "C" int asm_sort(int numbers[16], int size);
+extern "C" int my_leds(int value);
 
-DigitalOut myled(LED1);
+void c_sort (int numbers[16], int size){
+  int temp;
+    for (int out_count = 0; out_count <= size - 2; out_count++){
+        for (int in_count = 0; in_count <= size - 2 - out_count; in_count++){
+            if (numbers[in_count] > numbers[in_count + 1]){
+                temp = numbers[in_count];
+                numbers[in_count] = numbers[in_count + 1];
+                numbers[in_count + 1] = temp;
+            }
+        }
+    }
+}
+
+// Declare LED outputs
+DigitalOut myled1(LED1);
+DigitalOut myled2(LED2);
+DigitalOut myled3(LED3);
+DigitalOut myled4(LED4);
+// Set up array to sort
+const int size = 16;
+int numbers[size] = {12, 11, 14, 10, 9, 8, 22, 7, 6, 5, 15, 4, 3, 2, 0, 1};
 
 int main() {
-    while(1) {
-        myled = 1;
-        wait(0.2);
-        myled = 0;
-        wait(0.2);
+   // Show current array contents and set lights to "before" pattern
+   
+    printf("Before\n\r");
+    for(int index = 0; index <= size-1; index++){
+        printf("%d, ", numbers[index]);
+        my_leds(numbers[index]);
+        wait(0.5);
     }
-}
+    
+   // Call the sort function, comment out the one not being used
+   asm_sort(numbers, size);  // Call to assembly sort
+   //c_sort(numbers, size);  // Call to driver test stub
+   
+   printf("\n\rAfter\n\r");
+   
+    for(int index = 0; index <= size-1; index++){
+        printf("%d, ", numbers[index]);
+        my_leds(numbers[index]);
+        wait(0.5);
+    }
+      
+    while(1) {} 
+}
\ No newline at end of file
--- a/mbed.bld Sun Jan 01 20:57:57 2012 +0000 +++ b/mbed.bld Sat Jan 04 12:34:04 2020 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/078e4b97a13e +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/my_leds.s	Sat Jan 04 12:34:04 2020 +0000
@@ -0,0 +1,38 @@
+   AREA asm_demo, CODE, READONLY
+   EXPORT my_leds
+my_leds
+   PUSH    {R1, R2, R3, R4, LR}
+   LDR R1, =0x2009C020 ; loading the base.
+   AND R4, R0, #8 ;take biggest bit
+   AND R3, R3, #0 
+   ORR R3, R3, R4 ;put in R3
+   LSL R3, R3, #1 ;shift for spacing for pins
+   AND R4, R0, #6 ;take middle bits
+   ORR R3, R3, R4 ;put in R3
+   LSL R3, R3, #1 ;shift for spacing for pins
+   AND R4, R0, #1 ;take first bit
+   ORR R3, R3, R4 ;put in R3
+   LSL R3, R3, #18 ;shift to pin location in hex
+   STR R3, [R1, #0x18] ;turns on led with FIOSet
+   
+   BL      wait ; Call to wait
+   STR R3, [R1, #0x1C] ;clears LED with FIOClear
+   POP     {R1, R2, R3, R4, LR}
+   BX      LR
+   ALIGN   ; End of my_leds function
+   
+wait
+   PUSH    {R1, R2}
+   MOV.W R1, #1
+   LDR R2, =5000000
+           ; Omitted loop code goes here
+loop_entry
+        CMP R1, R2
+        BGT loop_exit
+        ADD R1,R1, #1
+        B loop_entry
+loop_exit
+   POP     {R1, R2}
+   BX      LR ; Return from wait
+   ALIGN
+   END
\ No newline at end of file