Use the following codes as the beginning of your program. Write a sort program to sort these ten numbers from largest to smallest, then save them to memory. For ARMSim#, the sorted number also need to be displayed in the console and LCD screen (use the Embest Board Plug‐In). MOV r0, #0x00002000 MOV r1, #18 STR r1, [r0],#4 MOV r1, #6 STR r1, [r0],#4 MOV r1, #2 STR r1, [r0],#4 MOV r1, #8 STR r1, [r0],#4 MOV r1, #16 STR r1, [r0],#4 MOV r1, #10 STR r1, [r0],#4 MOV r1, #14 STR r1, [r0],#4 MOV r1, #4 STR r1, [r0],#4 MOV r1, #20 STR r1, [r0],#4 MOV r1, #12 STR r1, [r0],#4
时间: 2023-07-02 16:12:37 浏览: 92
Sure, here is a possible solution in ARM assembly language:
```assembly
; Set up the constants and pointers
MOV r0, #0x00002000 ; Memory address to store the 10 numbers
MOV r1, #10 ; Number of elements to sort
LSL r2, r1, #2 ; Size of the array in bytes (10 * 4)
ADD r3, r0, r2 ; Pointer to the end of the array
; Copy the array to the stack for sorting
SUB sp, sp, r2 ; Allocate space on the stack
MOV r4, sp ; Pointer to the start of the stack
copy_loop:
LDR r5, [r0], #4 ; Load a number from the array
STR r5, [r4], #4 ; Store the number on the stack
CMP r0, r3 ; Check if we've reached the end of the array
BNE copy_loop ; If not, continue copying
; Sort the array using bubble sort
sort_loop:
MOV r4, sp ; Pointer to the start of the stack
inner_loop:
LDR r5, [r4], #4 ; Load the current number
LDR r6, [r4] ; Load the next number
CMP r5, r6 ; Compare the two numbers
BGE skip_swap ; If the current number is greater, skip the swap
STR r6, [r4, #-4]! ; Swap the numbers
STR r5, [r4], #4
MOV r7, #1 ; Set a flag to indicate that we swapped
skip_swap:
CMP r4, sp ; Check if we've reached the end of the array
BNE inner_loop ; If not, continue comparing
CMP r7, #1 ; Check if we swapped any numbers
BNE sort_done ; If not, the array is sorted and we're done
SUB r3, r3, #4 ; Otherwise, decrement the end pointer
B sort_loop ; and continue sorting
sort_done:
; Copy the sorted array back to memory
MOV r4, sp ; Pointer to the start of the stack
copy_back_loop:
LDR r5, [r4], #4 ; Load a number from the stack
STR r5, [r0], #4 ; Store the number in memory
CMP r0, r3 ; Check if we've reached the end of the array
BNE copy_back_loop ; If not, continue copying
; Display the sorted array in the console and LCD screen
MOV r0, #0x00002000 ; Memory address of the sorted array
MOV r1, #10 ; Number of elements to display
LSL r2, r1, #2 ; Size of the array in bytes (10 * 4)
MOV r3, #0x40000000 ; Base address of the Embest Board Plug-In
ADD r3, r3, #0x1000 ; Offset to the LCD screen buffer
ADD r4, r3, #0x80 ; Offset to the console output buffer
display_loop:
LDR r5, [r0], #4 ; Load a number from the sorted array
MOV r6, #0x30 ; ASCII code for '0'
ADD r6, r6, r5, LSL #1 ; Convert the number to a two-digit decimal string
STRB r6, [r4], #1 ; Display the string in the console
STRH r5, [r3], #2 ; Display the number in the LCD screen
CMP r0, r3 ; Check if we've reached the end of the array
BNE display_loop ; If not, continue displaying
; Clean up the stack and return from the program
ADD sp, sp, r2 ; Deallocate space on the stack
MOV pc, lr ; Return from the program
```
This program uses bubble sort to sort the 10 numbers in descending order, and then copies the sorted array back to memory. It also displays the sorted array in both the console and the LCD screen using the Embest Board Plug-In. Note that the LCD screen buffer is located at offset 0x1000 from the base address of the plug-in, and the console output buffer is located at offset 0x80. Also, the program assumes that the Embest Board Plug-In is connected and configured correctly.
阅读全文