【问题标题】:Problem in finding the Average of an array in mips在 mips 中查找数组平均值的问题
【发布时间】:2021-03-31 17:36:12
【问题描述】:

我最近开始学习 Mips 汇编语言,并且正在学习它的基础知识。在 mips 中找到我的程序的平均值时,我遇到了一些问题。有人可以通过向我解释用于找出平均值的说明来帮助我。非常感谢您提供代码示例。

【问题讨论】:

    标签: arrays sum mips average instructions


    【解决方案1】:

    我可以为您提供示例代码。有用的信息是通过下面代码中的 cmets 告知的。我希望这会有所帮助。

    .数据

    arr: .word 1, 2, 3, 4, 5, 6, 7 arrLen:.word 7 平均:.asciiz "\n平均:"

    .text .globl 主要

    主要:

    la  $t0, arr    #array is stored
    lw  $t1, arrLen #array length stored
    li  $t2, 0      
    li  $t3, 0
    

    循环:#循环求和

    sltiu $t7, $t2, 7   #should be run uptil 7 since its the array length
    beq   $t7, $0,  stop    
    lw    $t4, ($t0)
    addu $t3, $t3, $t4
    addiu   $t0, $t0, 4
    addiu $t2, $t2, 1
    j loop
    

    停止:

    la  $a0, avg
    li  $v0, 4
    syscall
    
    div $t5, $t3, $t1   #average is taken out using the sum calculated above
    move $a0, $t5       #value moved to a0 register to be printed
    li $v0, 1
    syscall
    
    li  $v0, 10
    syscall         #end
    

    【讨论】:

      【解决方案2】:
       .data
      

      大小:.word 0 to_store: .word 0 总和:.word 0 平均:.word 0

      size_prompt: .asciiz "输入元素数量:" element_prompt: .asciiz "输入一个元素:" msg_space: .asciiz " "

      .text
      .globl  main
      

      主要:

      # Prints prompt
      la      $a0,size_prompt
      addi    $v0,$0,4
      syscall
      
      # Gets input from user
      addi    $v0,$0,5
      syscall
      sw      $v0,size
      
      addi    $t0,$0,0                
      
      # Creates the list
      lw      $s0,size                
      addi    $t1,$0,4               
      mult    $s0,$t1                 
      mflo    $t2                     
      add     $a0,$t1,$t2
      addi    $v0,$0,9
      syscall
      

      堆栈: beq $t0,$s0,list_sum

      # prompt user for list element
      la      $a0,element_prompt
      addi    $v0,$0,4
      syscall
      
      # read in element value
      addi    $v0,$0,5
      syscall
      sw      $v0,to_store
      lw      $t5,to_store
      
      # push element to stack
      addi    $sp,$sp,-4
      sw      $t5,0($sp)
      
      addi    $t0,$t0,1           
      j       stack
      
      # get sum of array elements
      

      列表总和: beq $s0,0,average # 最后?如果是,飞 lw $t6,sum # 获取上一个总和 lw $t7,0($sp) # 获取下一个数组元素值 addi $sp,$sp,4 # 提前数组指针 add $t6,$t7,$t6 # sum += array[i] sw $t6,sum # 存储它 addi $s0,$s0,-1 # 减少 count j list_sum

      平均: lw $t0,sum # 得到总和

      # print the sum
      li      $v0,1
      move    $a0,$t0
      syscall
      
      # NOTE/FIX: restore the count value
      lw      $s0,size
      
      div     $t0,$s0             # divide by count
      mflo    $t0
      sw      $t0,avg             # store average
      
      li      $v0,4
      la      $a0,msg_space
      syscall
      
      # print average
      lw      $a0,avg
      addi    $v0,$0,1
      syscall
      

      退出: li $v0,10 # 退出程序 系统调用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-09-06
        • 1970-01-01
        • 2017-06-11
        • 2021-12-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多