나눗셈을 사용하여 MIPS에서 점수를 백분율로 계산

DanMan3395

저는 플레이어에게 기본적인 수학 질문을하고 총 점수를 기록하는 작은 수학 퀴즈 프로그램을 만들고 있습니다. 마지막에는 총 점수를 백분율로 계산하고 표시하고 싶습니다. 전체 질문 중 몇 퍼센트가 올바르게 답변되었는지 의미합니다. 그러나 나는 그 비율을 올바르게 표시하는 방법을 알아 내려고 내 두뇌를 괴롭 혔습니다. 백분율을 계산하는 수학은 모두 종료 기능에서 수행되지만 컨텍스트를 위해 전체 프로그램을 첨부하고 있습니다. 많이 주목됩니다. 최종 결과를 수정하기 위해 제가 할 수있는 일에 대해 조언 해주십시오.

또한 제쳐두고 분할 질문을 수행하는 더 좋은 방법이 있습니까? 현재 내가 할 수있는 유일한 방법은 나머지가없는 몫이 될 수있는 답입니다. 이것은 분명히 형편없는 해결책입니다.

.data
    startMsg:   .asciiz "Hello, welcome to MathQuiz, here is your first problem:\nEnter -100 to exit\n"
    qf1:        .asciiz "What is "  
    qf2:        .asciiz "? "
    a1:     .asciiz "Correct!\n"
    a2:     .asciiz "Incorrect!\n"
    emf1:       .asciiz "You solved "
    emf2:       .asciiz " math problems and got "
    emf3:       .asciiz " correct and "
    emf4:       .asciiz " incorrect, for a score of "
    emf5:       .asciiz "%.\nThanks for playing!"
    operator1:      .asciiz " + "
    operator2:      .asciiz " - "
    operator3:      .asciiz " * "
    operator4:      .asciiz " % "
    totalCount: .word -1
    correctCount:   .word 0
    incorrectCount: .word 0
    scoreCalc:  .word 0
    correctAnswer:  .word 0
    wrongAnswer:    .word 0
    derp:       .word 0

.text

.globl  main

main:   
    li $v0, 4 # greet the user
    la $a0, startMsg
    syscall

calc:   
    # the primary function that handles most of the calculations.
    
    li $s5, -100 #use register s5 as the exit program value.
    
    # operator reference table
    li $t2, 0
    li $t3, 1
    li $t4, 2
    li $t5, 3
            
    li $a1, 21 # set range for random number to 0-20
    li $v0, 42 # generate random number, saved in $a0
    syscall
    
    move $s1, $a0 # Move random number to register s1
    
    li $a1, 21 # set range for random number to 0-20
    li $v0, 42 # generate random number, saved in $a0
    syscall
    
    move $s2, $a0 # Move random number to register s2
    
    li $a1, 4 # set range for random number to 0-3
    li $v0, 42 # generate random number, saved in $a0
    syscall
    
    move $t1, $a0 # Move random number to register s2
    
    # operator table
    beq $t1, $t2, addition
    beq $t1, $t3, subtraction
    beq $t1, $t4, multiplication
    beq $t1, $t5, division

addition:
    
    li $v0,4 # output an ascii string
    la $a0, qf1 # load the ascii string qf1 for output to screen
    syscall
    
    li $v0,1 # output an int
    move $a0, $s1
    syscall
    
    li $v0,4 # output an ascii string
    la $a0, operator1 # load the ascii string qf1 for output to screen
    syscall
    
    li $v0,1 # output an int
    move $a0, $s2
    syscall
    
    li $v0,4 # output an ascii string.
    la $a0, qf2 # load the ascii string qf2 for output to screen.
    syscall
    
    li $v0, 5 # read an integer from the command line, result saved in $v0.
    syscall
    
    move $s4, $v0 # move the user input to a register for comparison.
    
    add $s3, $s1, $s2 # perform the addition of the 2 random numbers.
    
    lw $t1, totalCount # load the current value of totalCount into a register.
    
    add $t2, $t1, 1 # add 1 to the value in the register for totalCount.
    
    sw $t2, totalCount # save the iterated value of totalCount back to the memory space of the variable.
    
    beq $s4, $s5, exit # if the user input matches -1, jump to "exit" function.
    
    beq $s4, $s3, correct # if user input matches the correct answer, jump to the "correct" function.
    
    j incorrect # if the answer is wrong AND not "-1", jump to the "incorrect" function.
    
subtraction:
    
    li $v0,4 # output an ascii string
    la $a0, qf1 # load the ascii string qf1 for output to screen
    syscall
    
    li $v0,1 # output an int
    move $a0, $s1
    syscall
    
    li $v0,4 # output an ascii string
    la $a0, operator2 # load the ascii string qf1 for output to screen
    syscall
    
    li $v0,1 # output an int
    move $a0, $s2
    syscall
    
    li $v0,4 # output an ascii string.
    la $a0, qf2 # load the ascii string qf2 for output to screen.
    syscall
    
    li $v0, 5 # read an integer from the command line, result saved in $v0.
    syscall
    
    move $s4, $v0 # move the user input to a register for comparison.
    
    sub $s3, $s1, $s2 # perform the subtraction of the 2 random numbers.
    
    lw $t1, totalCount # load the current value of totalCount into a register.
    
    add $t2, $t1, 1 # add 1 to the value in the register for totalCount.
    
    sw $t2, totalCount # save the iterated value of totalCount back to the memory space of the variable.
    
    beq $s4, $s5, exit # if the user input matches -1, jump to "exit" function.
    
    beq $s4, $s3, correct # if user input matches the correct answer, jump to the "correct" function.
    
    j incorrect # if the answer is wrong AND not "-1", jump to the "incorrect" function.
    
multiplication:
    
    li $v0,4 # output an ascii string
    la $a0, qf1 # load the ascii string qf1 for output to screen
    syscall
    
    li $v0,1 # output an int
    move $a0, $s1
    syscall
    
    li $v0,4 # output an ascii string
    la $a0, operator3 # load the ascii string qf1 for output to screen
    syscall
    
    li $v0,1 # output an int
    move $a0, $s2
    syscall
    
    li $v0,4 # output an ascii string.
    la $a0, qf2 # load the ascii string qf2 for output to screen.
    syscall
    
    li $v0, 5 # read an integer from the command line, result saved in $v0.
    syscall
    
    move $s4, $v0 # move the user input to a register for comparison.
    
    mul $s3, $s1, $s2 # perform the addition of the 2 random numbers.
    
    lw $t1, totalCount # load the current value of totalCount into a register.
    
    add $t2, $t1, 1 # add 1 to the value in the register for totalCount.
    
    sw $t2, totalCount # save the iterated value of totalCount back to the memory space of the variable.
    
    beq $s4, $s5, exit # if the user input matches -1, jump to "exit" function.
    
    beq $s4, $s3, correct # if user input matches the correct answer, jump to the "correct" function.
    
    j incorrect # if the answer is wrong AND not "-1", jump to the "incorrect" function.
    
division:
    
    li $v0,4 # output an ascii string
    la $a0, qf1 # load the ascii string qf1 for output to screen
    syscall
    
    li $v0,1 # output an int
    move $a0, $s1
    syscall
    
    li $v0,4 # output an ascii string
    la $a0, operator4 # load the ascii string qf1 for output to screen
    syscall
    
    li $v0,1 # output an int
    move $a0, $s2
    syscall
    
    li $v0,4 # output an ascii string.
    la $a0, qf2 # load the ascii string qf2 for output to screen.
    syscall
    
    li $v0, 5 # read an integer from the command line, result saved in $v0.
    syscall
    
    move $s4, $v0 # move the user input to a register for comparison.
    
    div $s1, $s2 # perform the addition of the 2 random numbers.
    
    mflo $s3
    
    lw $t1, totalCount # load the current value of totalCount into a register.
    
    add $t2, $t1, 1 # add 1 to the value in the register for totalCount.
    
    sw $t2, totalCount # save the iterated value of totalCount back to the memory space of the variable.
    
    beq $s4, $s5, exit # if the user input matches -1, jump to "exit" function.
    
    beq $s4, $s3, correct # if user input matches the correct answer, jump to the "correct" function.
    
    j incorrect # if the answer is wrong AND not "-1", jump to the "incorrect" function.

correct:
    # produce the incorrect answer response and adjust counter.
    
    li $v0,4 # output an ascii string.
    la $a0, a1 # load the ascii string qf1 for output to screen.
    syscall
    
    lw $t1, correctCount # load the value of the correctCount variable into a register.
    
    add $t2, $t1, 1 # add 1 to the value for correctCount in the register.
    
    sw $t2, correctCount # save the iterated value of correctCount back into the memory space of the variable.
    
    j calc # jump back to the calc function to ask another question.
    
incorrect:

    # produce the incorrect answer response and adjust counter.
    
    li $v0,4 # output an ascii string.
    la $a0, a2 # load the ascii string qf1 for output to screen.
    syscall
    
    lw $t1, incorrectCount # load the value of the incorrectCount variable into a register.
    
    add $t2, $t1, 1 # add 1 to the value for incorrectCount in the register.
    
    sw $t2, incorrectCount # save the iterated value of incorrectCount back into the memory space of the variable.
    
    j calc # jump back to the calc function to ask another question.
    
exit:
    # perform the calculations needed to produce the final output to the user.
    
    lw $t1, totalCount # load the totalCount value into a register
    
    lw $t2, correctCount # load the correctCount value into the register
    
    li $t5, 100 # set a register to 100 for use in the percentage conversion process.
    
    div $t2, $t1 # calculate the players total correct score percentage using division.
    
    mflo $t3 # move the lo register value to $t3 for further calculations.
    
    mul $t6, $t3, $t5 # multiply the score value by 100 to convert to a whole number for output.
    
    # Assemble the output
    li $v0, 4 # output an ascii string
    la $a0, emf1 # load end message fragment 1 into the registr for output.
    syscall
    
    lw $a0, totalCount # load the value of totalCount to register a0 for output.
    li, $v0, 1 # output an int
    syscall
    
    li $v0, 4 # output an ascii string
    la $a0, emf2 # load end message fragment 2 into the registr for output.
    syscall
    
    lw $a0, correctCount # load the value of correctCount to register a0 for output.
    li, $v0, 1 # output an int
    syscall
    
    li $v0, 4 # output an ascii string
    la $a0, emf3 # load end message fragment 3 into the registr for output.
    syscall
    
    lw $a0, incorrectCount # load the value of incorrectCount to register a0 for output.
    li, $v0, 1 # output an int
    syscall
    
    li $v0, 4 # output an ascii string
    la $a0, emf4 # load end message fragment 4 into the registr for output.
    syscall
    
    move $a0, $t6
    li, $v0, 1 # output an int
    syscall
    
    li $v0, 4 # output an ascii string
    la $a0, emf5 # load end message fragment 5 into the registr for output.
    syscall
    
    li $v0, 10 #exits the program on syscall
    syscall
prl

정수 나눗셈을하면 항상 0을 얻습니다. 이후에 100을 곱하면 손실 된 분수를 복구 할 수 없습니다. 가장 쉬운 해결책은 먼저 100을 곱한 다음 나누는 것입니다.

가장 가까운 정수 백분율로 반올림하려면 정답에 100을 곱하고 질문 수의 절반을 더한 다음 질문 수로 나눕니다. 예를 들어 12 개의 질문이 있고 8 개가 맞으면 (8 x 100 + 6) / 12 = 67 %입니다.

66.7 %와 같이 퍼센트의 일부를 원하면 정수 연산 만 사용하여 수행 할 수 있지만 부동 소수점을 사용하는 것이 더 간단 할 것입니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

R에서-백분율 수익률을 사용하여 벡터의 값 계산

분류에서Dev

rowPercents를 사용하여 합계에 대한 백분율 계산

분류에서Dev

MIPS를 사용하여 배열에서 4로 나눌 수있는 요소 수 계산

분류에서Dev

dplyr을 사용하여 열의 특수 값 백분율 계산

분류에서Dev

DAX Power BI를 사용하여 백분율로 각 변수의 값을 계산하는 방법

분류에서Dev

관계형 나눗셈 (기본 대수식)을 SQL로 표현하는 방법

분류에서Dev

R에서 다른 빈도를 기반으로 한 변수의 백분율을 계산하는 방법

분류에서Dev

PHP에서 백분율을 계산 한 후 87.555555를 얻었습니다. 87.55로 수정하고 싶습니다.

분류에서Dev

Excel에서 두 시간 사이의 효율성을 백분율로 계산

분류에서Dev

Tidyverse를 사용하여 Total이 행에있을 때 백분율 열을 계산하는 For 루프 다시 작성

분류에서Dev

변수 당 Nan을 계산하고 백분율로 표시

분류에서Dev

jquery를 사용하여 upwork와 같은 지불에서 백분율 계산

분류에서Dev

Linq Lambda 함수를 사용하여 Nullable Decimal을 백분율로 합산

분류에서Dev

JS : 백분율을 사용하여 [-10, 10]과 같은 범위 숫자를 계산하고 백분율 값에 따라 숫자를 얻습니다.

분류에서Dev

PostgreSQL 여러 열을 사용하여 백분율에 대한 숫자 계산

분류에서Dev

Power BI는 필터를 사용하여 백분율 (나누기) 계산

분류에서Dev

수동으로 백분율을 계산하지 않고 Flex를 사용하여 섹션을 수직 및 수평으로 숨기기

분류에서Dev

하드웨어 제곱근으로 나눗셈 연산을 수행 할 수 있습니까?

분류에서Dev

dplyr을 사용하여 여러 종, 처리 및 변수가있는 데이터 프레임에서 백분율 계산

분류에서Dev

R에서 lapply를 사용하여 열을 반복하는 동안 행 값에 대한 백분율 변경 계산

분류에서Dev

awk를 사용하여 열 2에서 파생 된 열 1의 백분율을 계산하고 열 3에 추가합니다.

분류에서Dev

팬더 또는 파이썬 모듈을 사용하여 계산하는 동안 셀에서 백분율 기호를 얻는 방법

분류에서Dev

Power BI에서 DAX의 필터를 사용하여 백분율을 계산하는 방법은 무엇입니까?

분류에서Dev

Jquery에서 입력 값의 백분율을 기반으로 수수료를 계산하는 가장 좋은 방법은 무엇입니까?

분류에서Dev

MongoDB에서 패싯을 사용하여 백분율을 계산하는 방법은 무엇입니까?

분류에서Dev

bigdecimal을 사용하여 3에서 100으로 나눌 수있는 Java 계산

분류에서Dev

비트 연산을 사용하여 나눗셈 규칙 확인

분류에서Dev

사용자 양식에서 곱셈과 백분율을 계산하는 방법은 무엇입니까?

분류에서Dev

Pandas로 백분율 및 누적 백분율을 계산하는 방법

Related 관련 기사

  1. 1

    R에서-백분율 수익률을 사용하여 벡터의 값 계산

  2. 2

    rowPercents를 사용하여 합계에 대한 백분율 계산

  3. 3

    MIPS를 사용하여 배열에서 4로 나눌 수있는 요소 수 계산

  4. 4

    dplyr을 사용하여 열의 특수 값 백분율 계산

  5. 5

    DAX Power BI를 사용하여 백분율로 각 변수의 값을 계산하는 방법

  6. 6

    관계형 나눗셈 (기본 대수식)을 SQL로 표현하는 방법

  7. 7

    R에서 다른 빈도를 기반으로 한 변수의 백분율을 계산하는 방법

  8. 8

    PHP에서 백분율을 계산 한 후 87.555555를 얻었습니다. 87.55로 수정하고 싶습니다.

  9. 9

    Excel에서 두 시간 사이의 효율성을 백분율로 계산

  10. 10

    Tidyverse를 사용하여 Total이 행에있을 때 백분율 열을 계산하는 For 루프 다시 작성

  11. 11

    변수 당 Nan을 계산하고 백분율로 표시

  12. 12

    jquery를 사용하여 upwork와 같은 지불에서 백분율 계산

  13. 13

    Linq Lambda 함수를 사용하여 Nullable Decimal을 백분율로 합산

  14. 14

    JS : 백분율을 사용하여 [-10, 10]과 같은 범위 숫자를 계산하고 백분율 값에 따라 숫자를 얻습니다.

  15. 15

    PostgreSQL 여러 열을 사용하여 백분율에 대한 숫자 계산

  16. 16

    Power BI는 필터를 사용하여 백분율 (나누기) 계산

  17. 17

    수동으로 백분율을 계산하지 않고 Flex를 사용하여 섹션을 수직 및 수평으로 숨기기

  18. 18

    하드웨어 제곱근으로 나눗셈 연산을 수행 할 수 있습니까?

  19. 19

    dplyr을 사용하여 여러 종, 처리 및 변수가있는 데이터 프레임에서 백분율 계산

  20. 20

    R에서 lapply를 사용하여 열을 반복하는 동안 행 값에 대한 백분율 변경 계산

  21. 21

    awk를 사용하여 열 2에서 파생 된 열 1의 백분율을 계산하고 열 3에 추가합니다.

  22. 22

    팬더 또는 파이썬 모듈을 사용하여 계산하는 동안 셀에서 백분율 기호를 얻는 방법

  23. 23

    Power BI에서 DAX의 필터를 사용하여 백분율을 계산하는 방법은 무엇입니까?

  24. 24

    Jquery에서 입력 값의 백분율을 기반으로 수수료를 계산하는 가장 좋은 방법은 무엇입니까?

  25. 25

    MongoDB에서 패싯을 사용하여 백분율을 계산하는 방법은 무엇입니까?

  26. 26

    bigdecimal을 사용하여 3에서 100으로 나눌 수있는 Java 계산

  27. 27

    비트 연산을 사용하여 나눗셈 규칙 확인

  28. 28

    사용자 양식에서 곱셈과 백분율을 계산하는 방법은 무엇입니까?

  29. 29

    Pandas로 백분율 및 누적 백분율을 계산하는 방법

뜨겁다태그

보관