使用以下代码作为程序的开头。写一个程序找出最大和最小的数。最大和最小的数字需要分别保存在寄存器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 STR r1, [r0],#4 MOV r1, #5 STR r1, [r0],#4 MOV r1, #7 STR r1, [r0],#4 MOV r1, #2 STR r1, [r0],#4 MOV r1, #10 STR r1, [r0],#4 MOV r1, #6 STR r1, [r0],#4,注释用英文
时间: 2023-07-02 21:10:05 浏览: 145
利用汇编语言编写找出10个无符号数中最大数和最小数分别存储程序.doc
```
; Initialize variables
MOV r7, #0 ; Maximum number
MOV r8, #0 ; Minimum number
MOV r0, #0x00002000 ; Start of array
MOV r1, #9 ; Array size
; Load first number into r7 and r8
LDR r2, [r0],#4 ; Load first number
MOV r7, r2 ; Set as maximum
MOV r8, r2 ; Set as minimum
; Loop through remaining numbers in array
Loop:
LDR r2, [r0],#4 ; Load next number
CMP r2, r7 ; Compare to current maximum
BGT UpdateMax ; If greater, update maximum
CMP r2, r8 ; Compare to current minimum
BLT UpdateMin ; If less, update minimum
SUBS r1, r1, #1 ; Decrement loop counter
BNE Loop ; Loop until all numbers have been checked
; Display results on console
MOV r0, #0x40000000 ; Address of UART data register
MOV r1, #0x0A ; New line character
MOV r2, #0x20 ; Space character
MOV r3, r7 ; Maximum number
BL DisplayNumber ; Display maximum
MOV r3, r8 ; Minimum number
BL DisplayNumber ; Display minimum
; Display results on LCD screen
MOV r0, #0x08000108 ; Address of LCD control register
MOV r1, #0x01 ; Clear screen
STR r1, [r0]
MOV r1, #0x80 ; Set cursor to first row, first column
STR r1, [r0]
MOV r3, r7 ; Maximum number
BL DisplayNumberLCD ; Display maximum
MOV r1, #0xC0 ; Set cursor to second row, first column
STR r1, [r0]
MOV r3, r8 ; Minimum number
BL DisplayNumberLCD ; Display minimum
; End of program
END
; Subroutine to display a number on the console
; Inputs: r0 = UART data register address
; r1 = character to send after number (e.g. new line or space)
; r2 = character to use for padding (e.g. space or zero)
; r3 = number to display
DisplayNumber:
MOV r4, #0 ; Initialize digit count
MOV r5, #10000 ; Initialize divisor
MOV r6, #0 ; Initialize padding count
Loop1:
CMP r5, #0 ; Check if divisor is zero
BEQ EndLoop1 ; If zero, exit loop
MOV r7, #10 ; Divide by 10
SDIV r3, r3, r7
MUL r3, r3, r7
SUBS r6, r6, #1 ; Decrement padding count
BGT PadNumber ; If padding is needed, jump to PadNumber
CMP r3, #0 ; Check if number is zero
BEQ EndLoop1 ; If zero, exit loop
ADDS r4, r4, #1 ; Increment digit count
MOV r7, #0x30 ; Convert digit to ASCII code
ADDS r7, r7, r3
STR r7, [r0] ; Send digit to console
EndLoop1:
MOV r7, #0x30 ; Convert padding character to ASCII code
PadNumber:
STR r7, [r0] ; Send padding character to console
SUBS r5, r5, #10 ; Divide divisor by 10
B Loop1 ; Loop until all digits have been displayed
STR r1, [r0] ; Send end character to console
BX LR ; Return from subroutine
; Subroutine to display a number on the LCD screen
; Inputs: r0 = LCD control register address
; r2 = character to use for padding (e.g. space or zero)
; r3 = number to display
DisplayNumberLCD:
MOV r4, #0 ; Initialize digit count
MOV r5, #10000 ; Initialize divisor
MOV r6, #0 ; Initialize padding count
Loop2:
CMP r5, #0 ; Check if divisor is zero
BEQ EndLoop2 ; If zero, exit loop
MOV r7, #10 ; Divide by 10
SDIV r3, r3, r7
MUL r3, r3, r7
SUBS r6, r6, #1 ; Decrement padding count
BGT PadNumberLCD ; If padding is needed, jump to PadNumberLCD
CMP r3, #0 ; Check if number is zero
BEQ EndLoop2 ; If zero, exit loop
ADDS r4, r4, #1 ; Increment digit count
MOV r7, #0x30 ; Convert digit to ASCII code
ADDS r7, r7, r3
STR r7, [r0, #4] ; Send digit to LCD data register
EndLoop2:
MOV r7, #0x30 ; Convert padding character to ASCII code
PadNumberLCD:
STR r7, [r0, #4] ; Send padding character to LCD data register
SUBS r5, r5, #10 ; Divide divisor by 10
B Loop2 ; Loop until all digits have been displayed
BX LR ; Return from subroutine
```
阅读全文