Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @ Joseph Mendoza and
- @ Computer Science
- @ Assembly language coursework
- @ Program function
- @ Takes 2 keys and a messsage
- @ Formats characters to be processed
- @ Either encrypts or decrypts message
- @ Prints formatted result
- .global main
- .data
- .balign 4
- letter: .asciz "%c"
- .text
- .balign 4
- .global main
- main:
- @ Pushes values neededd onto the stack.
- PUSH {r4, r5, r6, r7, r8, r9, r10, r11, lr}
- @ Loads the number typed in when running program for either decrypt or encrypt.
- LDR r5, [r1,#4]
- LDRB r5, [r5]
- @ For key1
- @ Loads first character of the first key.
- LDR r4, [r1, #8]
- LDRB r6, [r4]
- BL mainloopend
- @ For key2
- @ Loads first character of second key.
- LDR r8, [r1, #12]
- LDRB r9, [r8]
- BL mainloopend
- @ This is the main loop in which the program will call lots of other functions from.
- @ The values ae: r4=first private char, r5=either encrypt or decrypt, r6=current char from key, r7=counter for key.
- mainloop:
- @ Makes changes to the characters if needed.
- BL formatchar
- @ Compare to see if there is a character there to encrypt.
- CMP r0, #-1
- BEQ mainloopend
- @ Moves the character of key into r1 so it can be processed.
- MOV r0 ,r0
- MOV r1, r6
- @ Encrypts if r5 is 48 as this is asci number for 0.
- CMP r5, #48
- BLEQ encrypt
- @ Decrypts if r5 is 49 as this is asci number for 1.
- CMP r5, #49
- BLEQ decrypt
- @ Below is for printing the character after it has been processed in the change message function.
- MOV r1, r0
- LDR r0,=letter
- BL printf
- @ Below is for making the key the same length as the message
- ADD r7,r7, #1
- LDRB r6, [r4, r7]
- @ This checks to make sure there is a character there and if there is not then index will be reset.
- CMP r6, #0
- MOVEQ r7, #0
- @ Puts back the character with index reset.
- LDRB r6, [r4, r7]
- mainloopend:
- @ This will be done on loop and will keep getting next character.
- BL getchar
- @ If there is a character in r0 then it will be passed through the main loop.
- CMP r0, #-1
- BNE mainloop
- BL end
- @ This section has to be done in order of the asci table.
- @ Order: Below A (for characters below capital a which are not needed.)
- @ Below [ (for characters which are capital to be changed to lowercase)
- @ Below a (for characters between Z and a in the asci table.
- @ Above 122 (for characters above z which are not in alphabet.)
- formatchar:
- PUSH {r4,r5,r6,r7,r8,r9,r10, lr}
- @ Removes anything below 65 or A in asci table.
- CMP r0, #65
- MOVLT r0, #-1
- @ Change all characters between 65 and 90 which are capital to lowercase by adding 32.
- CMP r0, #91
- ADDLT r0,r0,#32
- @ Removes anything between 90 and 97 in asci table which are not in alphabet.
- CMP r0, #97
- MOVLT r0, #-1
- @ Removes anything above 122 in asci table which is not in aphabet.
- CMP r0, #122
- MOVGT r0, #-1
- @Below is the encryption and decryption section
- @ Encrypts message with (r0 and r1 being character of msg and character of key)
- encrypt:
- PUSH {r4, r5, r6, r7, r8, r9,r10, lr}
- @ Encryption process stores result in r2.
- @ Does msg - key then adds 2.
- SUB r2, r0, r1
- ADD r2, #2
- @ Below is checking the chracter after encryption to loop back round to the lowercase alphabet.
- @ This is needed if the key char is bigger than the message char.
- CMP r2, #97
- ADDLT r2, #26
- BL encrypt2
- @ Encrypts message again
- encrypt2:
- PUSH {r4, r5, r6, r7, r8, r9,r10, lr}
- MOV r1, r9
- SUB r2, r0, r1
- ADD r2, #2
- @ Below is checking the character after encryption to loop back round to the lowercase alphabet.
- @ This is needed if the key char is bigger than the message char.
- CMP r2, #97
- ADDLT r2, #26
- @ Decrypts message
- decrypt:
- PUSH {r4,r5,r6,r7,r8,r9,r10, lr}
- MOV r1, r9
- ADD r2, r0, r1
- SUB r2, #2
- @ Below is checking if after decryption the char is within the range of the lowercase alphabet.
- @ If not then it will minus 26 to place it in range.
- CMP r2, #122
- SUBGT r2, r2, #26
- BL decrypt2
- @Decrypts message again
- decrypt2:
- PUSH {r4,r5,r6,r7,r8,r9,r10, lr}
- MOV r1, r6
- ADD r2, r0, r2
- SUB r2, #2
- @ Below is checking if after decryption if the char is in the range of the lowercase alphabet.
- @ If not then it will minus 26 to place it in range.
- CMP r2, #122
- SUBGT r2, r2, #26
- end:
- @ The pc stands for program counter.
- POP {r4,r5,r6,r7,r8,r9, pc}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement