Advertisement
Guest User

Untitled

a guest
Oct 27th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 3.82 KB | None | 0 0
  1. /*
  2.  
  3. 4.  Implement the body of the main function so that it accepts a command line
  4. string argument.  Calculate the length of this string.  Create the space on
  5. the stack for the indices to this string e.g.  if the string is michal, you
  6. will need 6 indices:  0,1,2,3,4,5.  Place the indices on the stack.
  7.  
  8. Sort  the  indices  according  to  the  alphabetical  order  of  the
  9. corresponding  letters  using bubble sort algorithm as given in the C code
  10. below. Obviously, for the above input string the sorted list of indices should
  11. be:  4,2,3,1,5,0.  Return the indices to stdout
  12.  
  13. for (i = 0; i < indexLength; ++i)
  14.     {
  15.         for (j = 0; j < indexLength - 1; ++j)
  16.         {
  17.             if (String[index[j]] > String[index[j + 1]])
  18.             {
  19.                 temp = index[j + 1];
  20.                 index[j + 1] = index[j];
  21.                 index[j] = temp;
  22.         }
  23.     }
  24. }
  25.  
  26. cmp-pi42.cmp.uea.ac.uk
  27.  
  28. */
  29.  
  30. @call by passing one command line string argument for example ./lab7_4 michal
  31. .data
  32. .balign 4
  33. SL_response: .asciz "String length is: %d\n"    @ enable string to be decimal
  34. ind_in_memory: .asciz "index %d is %d\n"    @ enable index to be decimal
  35.  
  36. .text
  37. .balign 4
  38. .global main
  39.  
  40. main:
  41.     PUSH {r4-r12,lr}
  42.     @r4      - number of inputs - argc
  43.     @r5      - address to the array address of inputs
  44.     @[r5]        - address to function name string (not relevant)
  45.     @r7=[r5, #4]     - address to the first command line string i.e. the one we are interested in
  46.     @r6      - string length
  47.     @r8      - index over string
  48.  
  49.     MOV r4, r0
  50.     MOV r5, r1
  51.  
  52.     @calculate string length
  53.     MOV r6, #0          @r6 - string length
  54.     LDR r7, [r5, #4]        @r7 - base address for the loop
  55.     countSL:
  56.         LDRB r0, [r7], #1
  57.         CMP r0, #0      @check if NULL that is end of string
  58.         ADDNE r6, r6, #1
  59.         BNE countSL
  60.     LDRB r0, address_of_SL_response
  61.     MOV r1, r6
  62.     BL printf
  63.  
  64.     /*move indices to string characters into the stack, we will store indices as bytes, assumes we don't
  65.       have more than 256 of them*/
  66.     @@r3 - index over string characters
  67.     MOV r3, #0       @index=0
  68.     SUB sp, sp, r6, LSL #3   @make space on the stack, guarantees the stack is 8 byte aligned
  69.  
  70.  
  71.     loopstackpush:
  72.     CMP r6, r3
  73.     BEQ end1
  74.         STRB r3, [sp, r3]
  75.         ADD r3, r3, #1
  76.         B loopstackpush
  77.     end1:
  78.  
  79.     @@bubble sort
  80.     @sort indices starting at sp
  81.     @r1 - j
  82.     @r2 - i
  83.     @r8 - index[j]
  84.     @r9 - index[j+1]
  85.     @r0 - indexLength - 1
  86.     @r7 - character base address for the string as previously
  87.     @r6 - indexLength as previously i.e. string length
  88.     @r10 - j+1
  89.     @r11 - string[index[j]]
  90.     @r12 - string[index[j, 1]]
  91.     LDR r7, [r5, #4]        @r7 - base address for the string
  92.     MOV r2, #0          @i=0
  93.     SUB r0, r6, #1
  94.     outerloop:          @outer for loop
  95.     CMP r6, r2
  96.     BEQ endouterloop        @i == indexL
  97.         MOV r1, #0      @j=0
  98.         innerloop:      @inner for loop
  99.         CMP r0, r1      @j == index: - 1
  100.         BEQ endinnerloop
  101.             LDRB r8, [sp, r1]   @r8 = index[j]
  102.             ADD r10, r1, #1     @r10 = j+1
  103.             LDRB r9, [sp, r10]  @r9 = index[j+1]
  104.             LDRB r11, [r7,r8]   @r11 = string[index[j]]
  105.             LDRB r12, [r7, r9]  @r12 = string[index[j+1]]
  106.             CMP r11, r12
  107.             BLE endinnerif
  108.     @string[index[j]]<=string[index[j+1]]
  109.                 STRB r8, [sp, r10]  @swap them into memory, temp not needed as we have copies in registers
  110.                 STRB r9, [sp, r1]
  111.             endinnerif:
  112.             ADD r1, r1, #1      @j=j+1
  113.             B innerloop
  114.         endinnerloop:
  115.         ADD r2, r2, #1          @i=i+1
  116.         B outerloop
  117.     endouterloop:
  118.  
  119.     @print indices to stdout
  120.     MOV r10, #0 @r10 - loop index
  121.     printloop:
  122.     CMP r6, r10
  123.     BEQ exitprintloop
  124.  
  125.         LDRB r2, [sp, r10]
  126.         MOV r1, r10
  127.         LDR r0, address_ind_in_memory
  128.         BL printf
  129.  
  130.         ADD r10, r10, #1
  131.         B printloop
  132.     exitprintloop:
  133.  
  134.     ADD sp, sp, r6, LSL #3  @pop indices from the stack
  135.     POP {r4-r12, lr}
  136.     BX lr
  137.  
  138.     address_of_SL_response: .word SL_response       @ Allow return format of SL_response
  139.     address_ind_in_memory: .word ind_in_memory  @ Allow return format of ind_in_memory
  140.  
  141.     .global printf  @ Allow both to have full access to all functions
  142.     .global scanf   @ Allow both to have full access to all functions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement