2017-03-06 6 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アセンブリLangauage:試験1、試験2及び期末試験

+0

私はあなたのためにあなたの宿題をすることに消極的です。たぶんあなたが試したことを教えてください。 – jayjay

+0

'div'の後に、' mvlo' [商を与える]と_not_ 'mvhi' [残りを与える] –

答えて

0

サブルーチンは、組織を簡単にするのに役立ちます。各スコアは、(すなわち(x/y)*score(score*x)/yのように書き換えされているのと同じ方法で計算されます。

それは、すべてのユーザーが、この場合には、最初のプロンプトを単一のサブルーチンを促し計算し、移動平均が単純である維持行うことが可能ですが。

注:私は私の上のコメントで述べたように、我々はしたいmflomfhi [残り] VS [商]

ここ

作り直しコードがあなたの元の係数を使用して、だ今にプラグインすることは容易でなければなりません。新しいもの[無償のスタイルクリーンアップをご容赦ください]:

# 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