wordfrequency

Dependencies:   LCD_DISCO_F429ZI mbed TS_DISCO_F429ZI BSP_DISCO_F429ZI

Program4_F20_Nguyen.s

Committer:
kaneyn
Date:
2020-12-07
Revision:
1:586013f67abf
Parent:
0:349c994479c8

File content as of revision 1:586013f67abf:

            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 
            
            LDR     R2,=table
            MOV R5, #0
            PUSH{R4}
         
loop            
            LDRB    R7, [R2],#12
            CMP     R7, #0
            BEQ leave
            ADD R5, #1
            b loop
            
leave       
            STRB R5, [SP,#0]
            POP{R0}
         
            BX LR
            
          
   
            
            GLOBAL GetWordAt
            ;uint8_t * GetWordAt(int32_t i)
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