在首地址为 ARY 的宇数组中存放若干数据 (100,-150,65, 450, 80,300, 120,600,800,0,-120,200),编写程序求它们的平均值放于V字单元,并统计有多少个数大于此平均值,调用显示子程序将统计结果用十进制显示出来用32位汇编语言写出
时间: 2024-03-11 15:49:58 浏览: 121
汇编语言子程序求数组平均值
以下是使用MIPS汇编语言的实现:
```assembly
.data
ARY: .word 100, -150, 65, 450, 80, 300, 120, 600, 800, 0, -120, 200
V: .word 0
msg1: .asciiz "The average value is "
msg2: .asciiz "The number of values greater than average is "
newline: .asciiz "\n"
.text
.globl main
main:
# load the constants
la $t0, ARY # load the address of ARY into $t0
lw $t1, V # load the address of V into $t1
# calculate the average value
li $t2, 0 # initialize sum to 0
li $t3, 12 # initialize count to 12
li $t4, 4 # set the increment to 4 bytes
loop:
lw $t5, ($t0) # load the next value from ARY
add $t2, $t2, $t5 # sum the values
addi $t0, $t0, $t4 # increment the address
addi $t3, $t3, -1 # decrement the count
bgtz $t3, loop # loop while count > 0
div $t2, $t2, 12 # divide the sum by the count
sw $t2, ($t1) # store the average value in V
# count the number of values greater than the average
li $t6, 0 # initialize count to 0
la $t0, ARY # load the address of ARY into $t0
loop2:
lw $t5, ($t0) # load the next value from ARY
bgt $t5, $t2, greater # branch if greater than average
addi $t0, $t0, $t4 # increment the address
addi $t3, $t3, -1 # decrement the count
bgtz $t3, loop2 # loop while count > 0
j done # jump to the end
greater:
addi $t6, $t6, 1 # increment the count
addi $t0, $t0, $t4 # increment the address
addi $t3, $t3, -1 # decrement the count
bgtz $t3, loop2 # loop while count > 0
done:
# display the results
la $a0, msg1 # load the address of msg1 into $a0
li $v0, 4 # set the syscall code for printing a string
syscall # print msg1
lw $a0, ($t1) # load the average value into $a0
li $v0, 1 # set the syscall code for printing an integer
syscall # print the average value
la $a0, newline # print a newline
li $v0, 4 # set the syscall code for printing a string
syscall # print newline
la $a0, msg2 # load the address of msg2 into $a0
li $v0, 4 # set the syscall code for printing a string
syscall # print msg2
move $a0, $t6 # move the count into $a0
li $v0, 1 # set the syscall code for printing an integer
syscall # print the count
la $a0, newline # print a newline
li $v0, 4 # set the syscall code for printing a string
syscall # print newline
# exit the program
li $v0, 10 # set the syscall code for exiting
syscall # exit the program
```
阅读全文