MIPS汇编语言:考试1,考试2和期末考试

时间:2017-03-06 17:51:13

标签: mips mars

# 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和期末考试的整数分数,阅读分数,计算加权平均分数(使用以下公式),并显示关于加权平均分数的标记输出。

1 个答案:

答案 0 :(得分:0)

子程序有助于简化组织。每个分数的计算方式相同(即(x/y)*score,其被重写为(score*x)/y

虽然可以首先执行所有用户提示,但在这种情况下,提示,计算和维护运行平均值的单个子例程更简单。

注意:正如我在最高评论中提到的,我们希望mflo [商]与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