如何计算 MIPS 中奇数正整数的总和? [英] How do I calculate the sum of odd positive integers in MIPS?

查看:78
本文介绍了如何计算 MIPS 中奇数正整数的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算 MIPS 中奇数正整数的总和?我家里有一个 MIPS 模拟器,我用来预订来帮助验证我的工作.我的大学有一个计算机实验室,其硬件由外部公司提供.我认为这个想法是大学通过课程向学生拉皮条".对我来说,部分问题是我想验证我的代码是否正常工作,而在学校使用董事会时,但在家验证代码似乎更容易.无论如何,我认为代码应该是这样的:

How do I calculate the sum of odd positive integers in MIPS? I have a MIPS simulator at home and I use to book to help verify my work. My university has a computer lab that has hardware provided by an outside company. The idea I suppose is that the University "pimps out" the hardware to students through the classes. Part of the problem for me is that I want to verify my code work properly, while using the board at school but it seems easier to verify the code works at home. Anyway, I think the code should read something like this:

andi $t8, $s0, 1  #value from $s0 and add 1 to it. Place in $t8 register
bnez $t8 #This should determine if its odd
beqz $79 #This should determine if its even
even:
  addi $t7, $t8, -1
  bnez $t7, odd
odd:
  addi $t6, $t7, -2
  Rt6, loop

有没有更简单的方法来做到这一点?我需要编写一个主程序,其中在执行结束时执行 v0=11000 之间奇数正整数的总和.在这种情况下,$t8 是我的 $v0.任何有用的建议都会被仔细考虑.

Is there an easier way to do this? I need to write a main routine in which at the end of the execution perform v0=the sum of odd positive integers between 1 and 1000. $t8 is my $v0 in this case. Any helpful suggestions would be considered very closely.

推荐答案

这里有一些带注释的代码,用于计算奇数 偶数值的和.它还有一个子程序的例子.

Here's some annotated code that does the sum of both odd and even values. It also has an example of a subroutine.

    .data
array:
    .word   17767, 9158, 39017, 18547
    .word   56401, 23807, 37962, 22764
    .word   7977, 31949, 22714, 55211
    .word   16882, 7931, 43491, 57670
    .word   124, 25282, 2132, 10232
    .word   8987, 59880, 52711, 17293
    .word   3958, 9562, 63790, 29283
    .word   49715, 55199, 50377, 1946
    .word   64358, 23858, 20493, 55223
    .word   47665, 58456, 12451, 55642
arrend:

msg_odd:    .asciiz     "The sum of the odd numbers is: "
msg_even:   .asciiz     "The sum of the even numbers is: "
msg_nl:     .asciiz     "\n"

    .text
    .globl  main
# main -- main program
#
# registers:
#   t0 -- even sum
#   t1 -- odd sum
#   t2 -- current array value
#   t3 -- isolation for even/odd bit
#   t6 -- array pointer
#   t7 -- array end pointer
main:
    li      $t0,0                   # zero out even sum
    li      $t1,0                   # zero out odd sum
    la      $t6,array               # address of array start
    la      $t7,arrend              # address of array end

main_loop:
    bge     $t6,$t7,main_done       # are we done? if yes, fly

    lw      $t2,0($t6)              # get value
    addiu   $t6,$t6,4               # point to next array element

    andi    $t3,$t2,1               # isolate LSB
    beqz    $t3,main_even           # is is even? if yes, fly

    add     $t1,$t1,$t2             # add to odd sum
    j       main_loop

main_even:
    add     $t0,$t0,$t2             # add to even sum
    j       main_loop

main_done:
    # output the even sum
    la      $a0,msg_even
    move    $a1,$t0
    jal     print

    # output the odd sum
    la      $a0,msg_odd
    move    $a1,$t1
    jal     print

    # terminate program
    li      $v0,10
    syscall

# print -- output a number
#
# arguments:
#   a0 -- pointer to message
#   a1 -- number to output
print:
    # output the message
    la      $v0,4
    syscall

    # output the number
    li      $v0,1
    move    $a0,$a1
    syscall

    # output a newline
    la      $a0,msg_nl
    li      $v0,4
    syscall

    jr      $ra                     # return

如果你想根据我自己的经验写一些干净的 asm 的技巧,请看我的回答:MIPS 链表

If you'd like some tips on writing clean asm, based on my own experience, see my answer: MIPS linked list

我已经将 spimQtSpimmars 用于模拟器.就个人而言,如果可能,我更喜欢 mars.请参阅:http://courses.missouristate.edu/KenVollmar/mars/

I've used spim, QtSpim, and mars for simulators. Personally, I prefer mars where possible. See: http://courses.missouristate.edu/KenVollmar/mars/

这篇关于如何计算 MIPS 中奇数正整数的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆