Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 4. Implement the body of the main function so that it accepts a command line
- string argument. Calculate the length of this string. Create the space on
- the stack for the indices to this string e.g. if the string is michal, you
- will need 6 indices: 0,1,2,3,4,5. Place the indices on the stack.
- Sort the indices according to the alphabetical order of the
- corresponding letters using bubble sort algorithm as given in the C code
- below. Obviously, for the above input string the sorted list of indices should
- be: 4,2,3,1,5,0. Return the indices to stdout
- for (i = 0; i < indexLength; ++i)
- {
- for (j = 0; j < indexLength - 1; ++j)
- {
- if (String[index[j]] > String[index[j + 1]])
- {
- temp = index[j + 1];
- index[j + 1] = index[j];
- index[j] = temp;
- }
- }
- }
- cmp-pi42.cmp.uea.ac.uk
- */
- @call by passing one command line string argument for example ./lab7_4 michal
- .data
- .balign 4
- SL_response: .asciz "String length is: %d\n" @ enable string to be decimal
- ind_in_memory: .asciz "index %d is %d\n" @ enable index to be decimal
- .text
- .balign 4
- .global main
- main:
- PUSH {r4-r12,lr}
- @r4 - number of inputs - argc
- @r5 - address to the array address of inputs
- @[r5] - address to function name string (not relevant)
- @r7=[r5, #4] - address to the first command line string i.e. the one we are interested in
- @r6 - string length
- @r8 - index over string
- MOV r4, r0
- MOV r5, r1
- @calculate string length
- MOV r6, #0 @r6 - string length
- LDR r7, [r5, #4] @r7 - base address for the loop
- countSL:
- LDRB r0, [r7], #1
- CMP r0, #0 @check if NULL that is end of string
- ADDNE r6, r6, #1
- BNE countSL
- LDRB r0, address_of_SL_response
- MOV r1, r6
- BL printf
- /*move indices to string characters into the stack, we will store indices as bytes, assumes we don't
- have more than 256 of them*/
- @@r3 - index over string characters
- MOV r3, #0 @index=0
- SUB sp, sp, r6, LSL #3 @make space on the stack, guarantees the stack is 8 byte aligned
- loopstackpush:
- CMP r6, r3
- BEQ end1
- STRB r3, [sp, r3]
- ADD r3, r3, #1
- B loopstackpush
- end1:
- @@bubble sort
- @sort indices starting at sp
- @r1 - j
- @r2 - i
- @r8 - index[j]
- @r9 - index[j+1]
- @r0 - indexLength - 1
- @r7 - character base address for the string as previously
- @r6 - indexLength as previously i.e. string length
- @r10 - j+1
- @r11 - string[index[j]]
- @r12 - string[index[j, 1]]
- LDR r7, [r5, #4] @r7 - base address for the string
- MOV r2, #0 @i=0
- SUB r0, r6, #1
- outerloop: @outer for loop
- CMP r6, r2
- BEQ endouterloop @i == indexL
- MOV r1, #0 @j=0
- innerloop: @inner for loop
- CMP r0, r1 @j == index: - 1
- BEQ endinnerloop
- LDRB r8, [sp, r1] @r8 = index[j]
- ADD r10, r1, #1 @r10 = j+1
- LDRB r9, [sp, r10] @r9 = index[j+1]
- LDRB r11, [r7,r8] @r11 = string[index[j]]
- LDRB r12, [r7, r9] @r12 = string[index[j+1]]
- CMP r11, r12
- BLE endinnerif
- @string[index[j]]<=string[index[j+1]]
- STRB r8, [sp, r10] @swap them into memory, temp not needed as we have copies in registers
- STRB r9, [sp, r1]
- endinnerif:
- ADD r1, r1, #1 @j=j+1
- B innerloop
- endinnerloop:
- ADD r2, r2, #1 @i=i+1
- B outerloop
- endouterloop:
- @print indices to stdout
- MOV r10, #0 @r10 - loop index
- printloop:
- CMP r6, r10
- BEQ exitprintloop
- LDRB r2, [sp, r10]
- MOV r1, r10
- LDR r0, address_ind_in_memory
- BL printf
- ADD r10, r10, #1
- B printloop
- exitprintloop:
- ADD sp, sp, r6, LSL #3 @pop indices from the stack
- POP {r4-r12, lr}
- BX lr
- address_of_SL_response: .word SL_response @ Allow return format of SL_response
- address_ind_in_memory: .word ind_in_memory @ Allow return format of ind_in_memory
- .global printf @ Allow both to have full access to all functions
- .global scanf @ Allow both to have full access to all functions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement