Advertisement
Rafael09ED

encode.c

Dec 7th, 2018
1,700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 2.48 KB | None | 0 0
  1. // Rafael Dejesus
  2. // Shana Slavin
  3. // CPSC 2310
  4. // Project 4
  5. // Due December 7, 2018
  6.  
  7.  
  8. /* function name:  encode
  9.  
  10.    description:  This program encodes and decodes messages
  11.  
  12.    input parameter(s):
  13.     r0 - address of input string
  14.     r1 - address of output string
  15.     r2 - address of key string
  16.     r3 - encode/decode switch
  17.          r3 == 0 means encode
  18.          r3 == 1 means decode
  19.  
  20.    return value (if any):
  21.     none
  22.      
  23.    other output parameters:
  24.     none
  25.  
  26.    effect/output
  27.     output string is updated with encoded or decoded message
  28.  
  29.    method / effect:
  30.      
  31.  
  32.    typical calling sequence:
  33.         pass input parameters
  34.     use modified output register
  35.  
  36.  
  37.    local register usage:
  38.     input         r0
  39.     output        r1
  40.     key           r2
  41.     decodeFlag    r3
  42.     inputIndex    r4
  43.     stringIVal    r5
  44.     keyIVal       r6
  45.     keyIndex      r7
  46.     temp          r8
  47.  
  48.  */
  49.  
  50.     .global encode
  51.  
  52.   input         .req r0
  53.   output        .req r1
  54.   key           .req r2
  55.   decodeFlag    .req r3
  56.   inputIndex    .req r4
  57.   stringIVal    .req r5
  58.   keyIVal       .req r6
  59.   keyIndex      .req r7
  60.   temp          .req r8
  61.  
  62. encode:
  63.   push {r4-r8, lr}
  64.  
  65.   mov keyIndex, #0
  66.   mov inputIndex, #0
  67.  
  68. encodeLoop:
  69.   // loads values
  70.   ldrb stringIVal, [input, inputIndex]
  71.   bl loadKeyIVal              
  72.  
  73.  
  74.   //If end of input, stop
  75.   cmp stringIVal, #0  
  76.   beq exit
  77.  
  78.   mov temp, stringIVal
  79.  
  80.   /* start alg */
  81.  
  82.   cmp keyIVal, #96
  83.   bls continue
  84.   cmp stringIVal, #96
  85.   bls continue
  86.  
  87.   // check for decode
  88.   cmp decodeFlag, #1
  89.   bge doDecode
  90.  
  91.   // encode:
  92.   sub keyIVal, keyIVal, #96
  93.   sub stringIVal, stringIVal, #96
  94.  
  95.   add temp, keyIVal, stringIVal
  96.   cmp temp, #26
  97.   bls backToAscii
  98.  
  99.   sub temp, temp, #26
  100.  
  101. backToAscii:
  102.   add temp, temp, #96
  103.  
  104.   /* end alg */
  105.  
  106. continue:
  107.   // store value in output
  108.   strb temp, [output, inputIndex]  
  109.  
  110.   // increment counters
  111.   add inputIndex, inputIndex, #1  
  112.   add keyIndex, keyIndex, #1
  113.  
  114.   bal encodeLoop
  115.  
  116.   // exit program
  117. exit:
  118.   mov temp, #0
  119.   strb temp, [output, inputIndex]
  120.   pop {r4-r8, pc}
  121.  
  122.  
  123.   //function for loading keyIVal
  124. loadKeyIVal:
  125.   ldrb keyIVal, [key, keyIndex]
  126.   cmp keyIVal, #0
  127.   bne loadKeyIValGood
  128.  
  129.   mov keyIndex, #0
  130.   ldrb keyIVal, [key]
  131.  
  132.   loadKeyIValGood:
  133.     bx lr
  134.  
  135.   // decodes message
  136. doDecode:
  137.   sub keyIVal, keyIVal, #96
  138.   sub temp, stringIVal, keyIVal
  139.   cmp temp, #97
  140.   bge continue
  141.  
  142.   add temp, temp, #26
  143.  
  144.   bal continue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement