SHARE
TWEET

charlie

a guest Nov 2nd, 2019 161 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @ Joseph Mendoza and
  2. @ Computer Science
  3. @ Assembly language coursework
  4.  
  5. @ Program function
  6. @ Takes 2 keys and a messsage
  7. @ Formats characters to be processed
  8. @ Either encrypts or decrypts message
  9. @ Prints formatted result
  10.  
  11.  
  12. .global  main
  13. .data
  14. .balign 4
  15.  
  16. letter: .asciz "%c"
  17.  
  18. .text
  19. .balign 4
  20. .global main
  21.  
  22.  
  23. main:
  24.     @ Pushes values neededd onto the stack.
  25.     PUSH {r4, r5, r6, r7, r8, r9, r10, r11, lr}
  26.  
  27.     @  Loads the number typed in when running program for either decrypt or encrypt.
  28.     LDR r5, [r1,#4]
  29.     LDRB r5, [r5]
  30.  
  31.     @ For key1
  32.     @ Loads first character of the first key.
  33.     LDR r4, [r1, #8]
  34.     LDRB r6, [r4]
  35.     BL mainloopend
  36.     @ For key2
  37.     @ Loads first character of second key.
  38.     LDR r8, [r1, #12]
  39.     LDRB r9, [r8]
  40.     BL mainloopend
  41.  
  42. @ This is the main loop in which the program will call lots of other functions from.
  43. @ The values ae: r4=first private char, r5=either encrypt or decrypt, r6=current char from key, r7=counter for key.
  44.  
  45.  
  46. mainloop:
  47.     @ Makes changes to the characters if needed.
  48.     BL formatchar
  49.  
  50.     @ Compare to see if there is a character there to encrypt.
  51.     CMP r0, #-1
  52.     BEQ mainloopend
  53.  
  54.     @ Moves the character of  key into r1 so it can be processed.
  55.     MOV r0 ,r0
  56.     MOV r1, r6
  57.  
  58.     @  Encrypts if  r5 is 48 as this is asci number for 0.
  59.     CMP r5, #48
  60.     BLEQ encrypt
  61.  
  62.     @ Decrypts if r5 is 49 as this is asci number for 1.
  63.     CMP r5, #49
  64.     BLEQ decrypt
  65.  
  66.  
  67.  
  68.     @ Below is for printing the character after it has been processed in the change message function.
  69.     MOV r1, r0
  70.     LDR r0,=letter
  71.     BL printf
  72.  
  73.  
  74.     @ Below is for making the key the same length as the message
  75.     ADD r7,r7, #1
  76.     LDRB r6, [r4, r7]
  77.  
  78.     @ This checks to make sure there is a character there and if there is not then index will be reset.
  79.     CMP r6, #0
  80.     MOVEQ r7, #0
  81.  
  82.     @ Puts back the character  with index reset.
  83.     LDRB r6, [r4, r7]
  84.  
  85.  
  86.  
  87. mainloopend:
  88.     @ This will be done on loop and will keep getting next character.
  89.     BL getchar
  90.  
  91.     @ If there is a character in r0 then it will be passed through the main loop.
  92.     CMP r0, #-1
  93.     BNE mainloop
  94.     BL end
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. @ This section has to be done in order of the asci table.
  105. @ Order: Below A (for characters below  capital a which are not needed.)
  106. @        Below [ (for characters which are capital to be changed to lowercase)
  107. @        Below a (for characters between Z and a in the asci table.
  108. @        Above 122 (for characters above z which are not in alphabet.)
  109. formatchar:
  110.     PUSH {r4,r5,r6,r7,r8,r9,r10, lr}
  111.  
  112.     @ Removes anything below 65 or A in asci table.
  113.     CMP r0, #65
  114.     MOVLT r0, #-1
  115.  
  116.     @ Change all characters between 65 and 90 which are capital to lowercase by adding 32.
  117.     CMP r0, #91
  118.     ADDLT r0,r0,#32
  119.  
  120.     @ Removes anything between 90 and 97 in asci table which are not in alphabet.
  121.     CMP r0, #97
  122.     MOVLT r0, #-1
  123.  
  124.     @ Removes anything above 122 in asci table which is not in aphabet.
  125.     CMP r0, #122
  126.     MOVGT r0, #-1
  127.  
  128.  
  129.  
  130.  
  131.  
  132. @Below is the encryption and decryption section
  133. @ Encrypts message with (r0 and r1 being character of msg and character of key)
  134. encrypt:
  135.     PUSH {r4, r5, r6, r7, r8, r9,r10, lr}
  136.  
  137.     @ Encryption process stores result in r2.
  138.     @ Does msg - key then adds 2.
  139.     SUB r2, r0, r1
  140.     ADD r2, #2
  141.  
  142.     @ Below is checking  the chracter after encryption to loop back round to the lowercase alphabet.
  143.     @ This is needed if the key char is bigger than the message char.
  144.     CMP r2, #97
  145.     ADDLT r2, #26
  146.     BL encrypt2
  147.  
  148.  
  149. @ Encrypts message again
  150. encrypt2:
  151.     PUSH {r4, r5, r6, r7, r8, r9,r10, lr}
  152.     MOV r1, r9
  153.     SUB r2, r0, r1
  154.     ADD r2, #2
  155.  
  156.     @ Below is checking the character after encryption  to loop back round to the lowercase alphabet.
  157.     @ This is needed if the key char is bigger than the message char.
  158.     CMP r2, #97
  159.     ADDLT r2, #26
  160.  
  161. @ Decrypts message
  162. decrypt:
  163.     PUSH {r4,r5,r6,r7,r8,r9,r10, lr}
  164.     MOV r1, r9
  165.     ADD r2, r0, r1
  166.     SUB r2, #2
  167.  
  168.     @ Below is checking if after decryption the char is within the range of the lowercase alphabet.
  169.     @ If not then it will minus 26 to place it in range.
  170.     CMP r2, #122
  171.     SUBGT r2, r2, #26
  172.     BL decrypt2
  173.  
  174.  
  175. @Decrypts message again
  176. decrypt2:
  177.     PUSH {r4,r5,r6,r7,r8,r9,r10, lr}
  178.     MOV r1, r6
  179.     ADD r2, r0, r2
  180.     SUB r2, #2
  181.  
  182.     @ Below is checking if after decryption  if the char is in the range of the lowercase alphabet.
  183.     @ If not then it will minus 26 to place it in range.
  184.     CMP r2, #122
  185.     SUBGT r2, r2, #26
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193. end:
  194.     @ The pc stands for program counter.
  195.     POP {r4,r5,r6,r7,r8,r9, pc}
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top