wordfrequency

Dependencies:   LCD_DISCO_F429ZI mbed TS_DISCO_F429ZI BSP_DISCO_F429ZI

Revision:
1:586013f67abf
Parent:
0:349c994479c8
diff -r 349c994479c8 -r 586013f67abf Program4_F20_Nguyen.s
--- a/Program4_F20_Nguyen.s	Sat Dec 05 21:12:01 2020 +0000
+++ b/Program4_F20_Nguyen.s	Mon Dec 07 18:23:27 2020 +0000
@@ -1,67 +1,139 @@
-        AREA Program4_F20_Nguyen, CODE, READONLY
-        
-       // 
+            AREA asm_func, CODE, READONLY, align = 8
+       
 //         R1 int32_t CountItems();
 //         R2 int32_t SumAllFrequencies();
 //         R3 uint8_t * GetWordAt(int32_t i);   // NOTE: returns a pointer to a string
 //         R4 int16_t GetFreqAt(int32_t i);      // NOTE: return the value of the frequency count   
 //            
-    
+       
+
+            GLOBAL SumAllFrequencies
+            ;int32_t SumAllFrequencies()
+SumAllFrequencies
+
+            LDR R9,=table
+            PUSH{R4} 
+            MOV R5, #0
+            
+            LDRH    R6, [R9],#8        
+            ADD     R5, R5, R6     
+loop2     
+            LDR    R6, [R9],#12           
+            CMP     R6, #0
+            BEQ     next
+            ADD     R5, R5, R6     
+            b loop2
+next        
+            MOV R0, #66
+            
+            BX LR      
+
             GLOBAL CountItems
             ;int32_t CountItems()
-CountItems     
-            //COUNT WORD
-            ADR     R1, table
-            MOV     R2, #0 ; count
-            MOV     R3, #0 ; ACCESS MEMORY
+CountItems 
+            
+            LDR     R2,=table
+            MOV R5, #0
+            PUSH{R4}
+         
 loop            
-            LDRB    R4, [R1, R3]
-            CMP     R4, #0
+            LDRB    R7, [R2],#12
+            CMP     R7, #0
             BEQ leave
-            ADD R2, #1
-            ADD R3, #12
+            ADD R5, #1
             b loop
+            
 leave       
-            MOV R0, R2
+            STRB R5, [SP,#0]
+            POP{R0}
+         
             BX LR
             
-            GLOBAL SumAllFrequencies
-            ;int32_t SumAllFrequencies()
-SumAllFrequencies 
+          
+   
+            
             GLOBAL GetWordAt
             ;uint8_t * GetWordAt(int32_t i)
-GetWordAt            
+GetWordAt   
+            LDR R5, =table
+            MOV R9, #12
+            ;GET WORD AT I
+            ;R2 = Max;
+            ;R8 store the return address of the char
+            
+            MOV R3, R9   
+            MUL R7, R0, R3  ; i*offset
+            MOV R3, #0
+grabWord            
+            LDRB R4, [R5, R7] 
+            CMP R4, #0      ;went through the i+1
+            BEQ end     ;if word done, grab frequency next
+            STRB R4, [R0, R3]; Store the string to address R8
+            ADD R7, R7, #1 ;
+            ADD R3, R3, #1;
+            b grabWord
+end         
+            
+            BX LR            
+            
             GLOBAL GetFreqAt
             ;int16_t GetFreqAt(int32_t i)
 GetFreqAt   
-               
+            
+            MOV R9, #12
+            
+          ;GET FREQ At i
+            LDR R8, =table  
+            MOV R3, #8 ;Frequency offset
+            CMP R0, #1
+            BGT greaterThan1 ;If i more than 1 switch formula
             
- 
+            MUL R7, R0, R3    ;Otherwise, get the value at offset 8
+            LDRH R4, [R8, R7]
+            MOV R0, R4
+            b exit
+greaterThan1                ;i more than 1
+            MOV R3, #8
+            SUB R6, R6, #1  ; subtract 1 from i
+            MUL R5, R6, R9  ; R0 is the new offset, (i-1)*12
+            ADD R7, R3, R5  ; Add new offset with #8 so it could move the the correct place where the data at 
+            LDRH R4, [R8, R7] ; Get the value of freq 
+            MOV  R10, R4
+exit
+            MOV R0, R10 
+            BX LR
+                        
+          
+         
+
+           
 table       DCB "pears",0
             ALIGN 4
             DCW 8 ; Freq
             SPACE 2
-            //-----------
+            ;-----------
             
             DCB "apples",0
             ALIGN 4
             DCW 16
             SPACE 2
-            //------------
+            ;------------
             DCB "pie",0
             ALIGN 4
             space 4
             DCW 30
             SPACE 2
-            //--------------
+            ;--------------
             DCB "beans",0
             ALIGN 4
             DCW 12
             SPACE 2
+            ALIGN 8
+            DCB 0
+            ALIGN 8
             DCB 0
             
             
-             
             END
                 
     
\ No newline at end of file