; QOSSAY RIDA    1211553
	PRESERVE8 
	THUMB
	AREA RESET, DATA, READONLY 
	EXPORT __Vectors 
__Vectors DCD 0x20001000 ; stack pointer value when stack is empty 
          DCD Reset_Handler ; reset vector 

;====================================================================
;Declare pointer for each value in memory
		  
TXT1Address  DCD Str1
TXT2Address  DCD Str2
TXT1AfterEditAddress  DCD TXT1AfterEdit
TXT2AfterEditAddress  DCD TXT2AfterEdit
NumberTXT1Address  DCD NumberTXT1  
NumberTXT2Address  DCD NumberTXT2
TXT1AfterRemoveAddress DCD TXT1AfterRemove
COMMONAddress DCD  COMMON 
ENCRYPT1Address DCD ENCRYPT1
ENCRYPT2Address DCD ENCRYPT2
	
;====================================================================
;Declare the memory and what will contain

	AREA MYRAM, DATA, READONLY 
		
Str1  DCB "Programming",0
Str2  DCB "Assembly",0


	AREA MYRAM1, DATA, READWRITE
TXT1AfterEdit  space 100
TXT2AfterEdit  space 100
NumberTXT1  DCD 0				   ;Has number of letters converted for (Str1)
NumberTXT2  DCD 0				   ;Has number of letters converted for (Str2)
TXT1AfterRemove  space 100  	   ;This Address will contain an array of unique characters that make the first string
COMMON DCD 0 
ENCRYPT1 space 100
ENCRYPT2 space 100
	
;====================================================================
   AREA MYCODE, CODE, READONLY 
     ENTRY 
     EXPORT Reset_Handler

;******************************************
; Main:

Reset_Handler 

	 ;Call Convert Procedure to convert letters from upperCase to lowerCase for (Str1)
     LDR R0,TXT1Address  
	 MOV R2,#0
	 LDR R3 ,TXT1AfterEditAddress	;R3 is pointer where store converted string
	 LDR R4,NumberTXT1Address		;R4 is pointer to number of letters converted
	 BL Convert 
	 

	 
	 ;Call Convert Procedure to convert letters from upperCase to lowerCase for (Str2)
	 LDR R0,TXT2Address   
	 MOV R2,#0
	 LDR R3 ,TXT2AfterEditAddress	;R3 is pointer where store converted string
	 LDR R4,NumberTXT2Address		;R4 is pointer to number of letters converted
	 BL Convert

	 
	 ;Call DeleteRepeated Procedure to delete all repeted character from (Str1) 
	 ;And use the output to find repeted character between (the output) and (Str2) in nezt call
	 ;Example: input is "assemblyisveryeasy" the output will be "asemblyivr"
	 LDR R0, TXT1AfterEditAddress 
   	 LDR R1 , TXT1AfterRemoveAddress		;R1 is pointer where store new String
	 MOV  R2, #0							;Is index has displacement for (R0)
	 BL DeleteRepeated
	 

	 
	 ;Call CountCOMMON Procedure to count the number of repeted letters between (Str1) and (Str2)
	 ;BY give this Procedure array of unique characters (TXT1AfterRemove) and (Str2)
	 LDR R0 , TXT1AfterRemoveAddress
	 LDR R1 , TXT2AfterEditAddress
	 MOV R2, #0							
     MOV R3, #0
     MOV R4, R0
	 LDR R8, COMMONAddress
	 BL CountCOMMON
	 

	 
	 ;In fact, I think i deserve a full mark
	 
	 ;IMPORTANT: The doctor didn't mention if the encryption was specific to the texts before or after Edit
	 ;Call Encrypt Procedure to encrypt (Str1)
	 LDR R0 , TXT1Address 
	 LDR R1 , ENCRYPT1Address
	 BL Encrypt
	 

	 	 
	 ;Call Encrypt Procedure to encrypt (Str2)
	 LDR R0 , TXT2Address 
	 LDR R1 , ENCRYPT2Address
	 BL Encrypt
	 
	 ;Lode address for each value to Know the Address
	 LDR R0,TXT1Address
	 LDR R1,TXT2Address 
	 LDR R2,TXT1AfterEditAddress 
	 LDR R3,TXT2AfterEditAddress 
	 LDR R4,NumberTXT1Address 
	 LDR R5,NumberTXT2Address 
	 LDR R6,TXT1AfterRemoveAddress
	 LDR R7,COMMONAddress 
	 LDR R8,ENCRYPT1Address 
	 LDR R9,ENCRYPT2Address
	
STOP
     B STOP

;******************************************
; Convert Procedure: 
; This procedure will use to convert letters from upperCase to lowerCase

Convert PROC 
	 LDRB R1 ,[R0] , #1			;Load new character from (Str1)
	 CMP R1,#0 					;Compare character with zero
	 BGT Complete				;If loaded character not equal 0 (null) branch to check character
	 STRB R1 , [R3]				;If loaded character equal 0 (null) store 0 (null for end converted string)
	 STRB R2 , [R4]				;Store the number of character we converted it
	 BX LR						;Get Back to main
	 
Complete CMP R1 ,#0x5B			;Compare character with 'Z'
	 BGT Complete1				;If loaded character grether than Z go to store it
	 CMP R1 ,#0x40				;Compare character with 'A'
	 BLT Complete1				;If loaded character less than A go to store it
	 ADD R1,#0x20				;If loaded character between 'A'-'Z' add 20 to it
								;because the value between upperCase and lowerCase equal 20
	 ADD R2,#1					;Increment the counter (this counter for number of character we converted it)
Complete1 STRB R1 , [R3] , #1   ;Store the character
	 B Convert					;Go to check the next character
	 ENDP

;******************************************
; DeleteRepeated Procedure:
; This procedure will use to find array of unique characters for input string
	
DeleteRepeated	 PROC 
loop1  ;load character from input string
        LDRB R4, [R0, R2]		;Load new character from (TXT1AfterEdit)
        CMP R4, #0				;Compare character with zero
		BEQ endProc				;If loaded character equal 0 (null) branch to endProc
		CMP R4, #0x20			;If loaded character equal 20 (space) branch to continue_input 
		BEQ continue_input
        MOV R3, #0				;If loaded character don't equal 0 (null) and don't equal 20 (space) lode 0 to R3
								;R3 is index has displacement for output (TXT1AfterRemove) 
loop2
        LDRB R5, [R1, R3]		;Load character from output(TXT1AfterRemove)
        CMP R5, #0				;Compare character from output with zero
        BEQ add_to_output		;If loaded character from output equal 0 go to store it 
        CMP R4, R5				;Compare character from output(R5) with loaded character from input(R4)
        BEQ continue_input		;If loaded character from output(R5) equal loaded character from input(R4) 
								;Don't store it and go to fetch next input
        ADD R3, #1				;If loaded character from output(R5) don't equal loaded character from input(R4)
								;Increment (R3) to compaer the loaded character from input(R4) with all character in output 
        B loop2					;Continue to compare
add_to_output
        STRB R4, [R1, R3]		;Store loaded character from input(R4) to the output
continue_input
        ADD R2, #1				;Increment the displacement to fetch next character from input 
        B loop1					;Go to check character
endProc
		BX LR					;Get Back to main
		ENDP
			
;******************************************
; CountCOMMON Procedure :	
; This procedure will compear between (Str2) and array has unique characters for (Str1) 
		 
CountCOMMON	PROC		 
loop_str1
    LDRB R5, [R0] , #1		;Load new character from (TXT1AfterRemove)
    CMP R5, #0				;Compare character with zero
    BEQ end_str1			;If loaded character equal 0 (null) branch to end_str1
    MOV R6, R1				;Save address for (TXT2AfterEdit) at R6
loop_str2
    LDRB R7, [R1]			;Load new character from (TXT2AfterEdit) 
    CMP R7, #0				;Compare character with zero
    BEQ str2_done			;If loaded character equal 0 (null) branch to str2_done
    CMP R5, R7				;Compare character from (TXT1AfterRemove) with character from (TXT2AfterEdit)
    BEQ increment			;If this two character are equal branch increment 
    ADD R1,#1				;If If this two character aren't equal 
							;Increment R1 to fetch next character from (TXT2AfterEdit)
    B loop_str2				;Continue to compare
increment
	ADD R2,#1				;Increment because we find repeat character :)
str2_done
    MOV R1, R6				;Return the address for (TXT2AfterEdit) to R1
    B loop_str1				;Continue to compare
end_str1
    STRB R2,[R8]			;Store the number of repeat character
	BX LR					;Get Back to main
	ENDP
		
;******************************************
; Encrypt Procedure :
; This procedure will return encryption String
		
Encrypt PROC 
	 LDRB R2 ,[R0] , #1		;Load new character from (Str1)
	 CMP R2,#0 				;Compare character with zero
	 BEQ EndProc			;If loaded character equal 0 (null) branch to EndProc
	 MVN R2 , R2			;If loaded character not equal 0 (null) save NOT R2 in R2
	 STRB R2 , [R1] , #1	;Store the character
	 B Encrypt				;Go to check the next character
EndProc
	 BX LR					;Get Back to main
	 ENDP


	 END	
		 
;====================================================================