使用以下代码作为程序的开头。写一个程序找出最大和最小的数。最大和最小的数字需要分别保存在寄存器r7和r8中。对于ARMSim#,这些数字也需要显示在控制台和LCD屏幕上(使用Embest Board) 代入量) MOV r0, #0x00002000 MOV r1, #9 STR r1, [r0],#4 MOV r1, #3 STR r1, [r0],#4 MOV r1, #1 STR r1, [r0],#4 MOV r1, #4 STR r1, [r0],#4 MOV r1, #8,注释用英文,并并对每一步进行详细解读
时间: 2023-07-02 22:10:07 浏览: 30
```
; Initialize variables
MOV r0, #0x00002000 ; Set starting address of array to r0
MOV r1, #9 ; Set the number of elements in the array to r1
; Store values in the array
STR r1, [r0],#4 ; Store the value 9 in the first element of the array and increment the address by 4 bytes
MOV r1, #3 ; Set the value 3 to r1
STR r1, [r0],#4 ; Store the value 3 in the second element of the array and increment the address by 4 bytes
MOV r1, #1 ; Set the value 1 to r1
STR r1, [r0],#4 ; Store the value 1 in the third element of the array and increment the address by 4 bytes
MOV r1, #4 ; Set the value 4 to r1
STR r1, [r0],#4 ; Store the value 4 in the fourth element of the array and increment the address by 4 bytes
MOV r1, #8 ; Set the value 8 to r1
STR r1, [r0],#4 ; Store the value 8 in the fifth element of the array and increment the address by 4 bytes
```
This program initializes an array of 5 elements starting at memory address 0x00002000. It stores the values 9, 3, 1, 4, and 8 in the array in that order.
To find the maximum and minimum values, we need to loop through the array and compare each element to the current maximum and minimum values. Here's one way to do it:
```
; Find maximum and minimum values
MOV r7, #0 ; Initialize r7 (max value) to 0
MOV r8, #255 ; Initialize r8 (min value) to the highest possible value
MOV r2, #0 ; Counter variable to keep track of array index
loop:
LDR r1, [r0, r2] ; Load the value at the current array index into r1
CMP r1, r7 ; Compare the value to the current max value
BGT max ; Branch to max if r1 is greater than r7
CMP r1, r8 ; Compare the value to the current min value
BLT min ; Branch to min if r1 is less than r8
ADD r2, r2, #4 ; Increment the array index by 4 bytes
CMP r2, #20 ; Compare the array index to the end of the array (5 elements * 4 bytes each)
BLT loop ; If r2 is less than 20, go back to the beginning of the loop
done:
; Display the maximum and minimum values
; (This will vary depending on the specific hardware and software being used)
...
max:
MOV r7, r1 ; Update the maximum value to r1
B done ; Branch to done to exit the loop
min:
MOV r8, r1 ; Update the minimum value to r1
B done ; Branch to done to exit the loop
```
This code uses a loop to go through each element of the array and compare it to the current maximum and minimum values (stored in r7 and r8, respectively). If the current value is greater than the current maximum, it updates r7. If it's less than the current minimum, it updates r8. The loop continues until all elements of the array have been checked.
Once the loop is finished, r7 will contain the maximum value and r8 will contain the minimum value. These values can then be displayed using the appropriate output commands for the specific hardware and software being used.