2017-03-06 78 views
1
# Prompt user to enter the integer scores for Exams 1, 2, and Final, 
# read the scores, 
# compute the weighted average score (using the following formula), and 
# display a labeled output about the weighted average score. 
# Formula: avg = (128/637)*e1Score + (307/1024)*e2Score + (feScore/2) 
# avgScore=128*(1/637)*e1Score+307*(1/1024)*e2Score+(1/2)*feScore 
############################ data segment ################################ 
.data 
scorePrompt0:  .asciiz "Enter integer score for Exam 1: " 
scorePrompt1:  .asciiz "Enter integer score for Exam 2: " 
scorePrompt2:  .asciiz "Enter integer score for Final Exam: " 
avgMsg:   .asciiz "The weighted average is: " 
############################ code segment ################################ 
      .text 
      .globl main 
main: 
      ################################################ 
      # Get the scores, store in $t0, $t1, $t2 
      ################################################ 

      li $v0, 4 
      la $a0, scorePrompt0 # prompt for a score 
      syscall 
      li $v0, 5 
      syscall   # read an integer 
      move $t0, $v0 

      li $v0, 4 
      la $a0, scorePrompt1 # prompt for a score 
      syscall 
      li $v0, 5 
      syscall   # read an integer 
      move $t1, $v0 

      li $v0, 4 
      la $a0, scorePrompt2 # prompt for a score 
      syscall 
      li $v0, 5 
      syscall   # read an integer 
      move $t2, $v0 


      ################################################ 
      # Compute weighted average, store in $t4 
      ################################################ 

      # multiply e1Score by 128 
      sll $t0, $t0, 7 

      # divide e2Score by 1024 
      sra $t1, $t1, 10 

      # divide feScore by 2 
      sra $t2, $t2, 1 

      # divide e1score by 637 
      li $t5, 637   
      div $t0, $t5    
      mfhi $t0 

      # multiply e2score by 307 
      li $t5, 307   
      mul $t1, $t1, $t5 

      li $t4, 0  # ensure $t4 is 0 
      add $t4, $t4, $t0 
      add $t4, $t4, $t1 
      add $t4, $t4, $t2 

      li $v0, 4 
      la $a0, avgMsg 
      syscall 
      li $v0, 1 
      move $a0, $t4 
      syscall 

      li $v0, 10  # graceful exit service 
      syscall 

上面的代碼是一個例子,但我的問題是如何將此代碼更改爲(205/1024)*e1Score + #(256/854)*e2Score + (feScore/2)?提示用戶輸入考試1,考試2和期末考試的整數分數,讀取分數,計算加權平均分數(使用以下公式),並顯示關於加權平均分數的標記輸出。MIPS Assembly Langauage:考試1,考試2和期末考試

+0

你沒有任何明顯的努力,我不願意爲你做功課。也許告訴我們你試過了什麼? – jayjay

+0

經過'div',你想'mvlo' [給商]和_not_'mvhi' [給出餘數] –

回答

0

子程序有助於使組織變得簡單。每個分數的計算以同樣的方式(即(x/y)*score這是改寫爲(score*x)/y

雖然可以做所有的用戶提示第一,在這種情況下,提示,計算一個子程序,並保持運行平均值更簡單。

注:正如我在上面的評論中提到,我們要mflo [商] VS mfhi [其餘]

這裏的返工代碼,使用原來的係數現在應該很容易在堵塞。新的[請原諒無償風格的清理]:

# Prompt user to enter the integer scores for Exams 1, 2, and Final, 
# read the scores, 
# compute the weighted average score (using the following formula), and 
# display a labeled output about the weighted average score. 
# Formula: avg = (128/637)*e1Score + (307/1024)*e2Score + (feScore/2) 
# avgScore=128*(1/637)*e1Score+307*(1/1024)*e2Score+(1/2)*feScore 
############################ data segment ################################ 
    .data 
scorePrompt0: .asciiz "Enter integer score for Exam 1: " 
scorePrompt1: .asciiz "Enter integer score for Exam 2: " 
scorePrompt2: .asciiz "Enter integer score for Final Exam: " 
avgMsg:  .asciiz  "The weighted average is: " 
############################ code segment ################################ 
    .text 
    .globl main 

main: 
    ################################################ 
    # Get the scores, store in $t0, $t1, $t2 
    ################################################ 

    li  $t4,0     # zero the sum 

    la  $a0,scorePrompt0 
    li  $a1,128 
    li  $a2,637 
    jal  getnum 

    la  $a0,scorePrompt1 
    li  $a1,307 
    li  $a2,1024 
    jal  getnum 

    la  $a0,scorePrompt2 
    li  $a1,1 
    li  $a2,2 
    jal  getnum 

    li  $v0,4 
    la  $a0,avgMsg 
    syscall 

    li  $v0,1 
    move $a0,$t4 
    syscall 

    li  $v0,10     # graceful exit service 
    syscall 

# getnum -- prompt user for number and compute weighted average 
# 
# RETURNS: 
# v0 -- weighted score value 
# t4 -- sum of terms so var 
# 
# arguments: 
# a0 -- prompt string 
# a1 -- scale factor (multiplier) 
# a2 -- scale factor (divider) 
getnum: 
    li  $v0,4     # syscall to print string 
    syscall 

    li  $v0,5     # syscall to read integer 
    syscall 

    mul  $v0,$v0,$a1    # multiply by scale (e.g. 128) 
    div  $v0,$a2     # divide by scale (e.g. 637) 
    mflo $v0      # get quotient 

    add  $t4,$t4,$v0    # add to sum 

    jr  $ra      # return 
+0

抱歉,我一直在嘗試不同的方法,我自己也不想混淆當我發佈它的問題。我已經嘗試了幾天,現在讓它工作。謝謝Craig Estey! – user7667966