【问题标题】:Representing numbers greater than 65535 in MIPS在 MIPS 中表示大于 65535 的数字
【发布时间】:2010-07-22 18:24:36
【问题描述】:

我在 MIPS 中工作,使用超过 65535 的数字,我遇到了超出范围的错误。如何在这段代码中解决这个问题?

## p2.asm
##
## Andrew Levenson, 2010
## Problem 2 from Project Euler
## In MIPS Assembly, for SPIM
##
## Calculate the sum, s of all
## even valued terms in the
## Fibonacci sequence which
## do not exceed 4,000,000
        .text
        .globl  main

main:
    ## Registers
        ori     $t0, $0, 0x0        # $t0 will contain scratch
        ori     $t1, $0, 0x1        # $t1 will contain initial fib(N-1) 
        ori     $t2, $0, 0x2        # $t2 will contain initial fib(N) 
        ori     $t3, $0, 0x0        # $t3 will be our loop incrementor
        ori     $t4, $0, 0x0        # $t4 will be our sum
        ori     $t5, $0, 0x2        # $t5 contains two to test if even
        ori     $t8, $0, 4000000    # $t8 contains N limit

even_test:  
    ## Test to see if a given number is even
        div     $t1, $t5            # $t1 / 2
        mflo    $t6                 # $t6 = floor($t1 / 2) 
        mfhi    $t7                 # $t7 = $t1 mod 2

        bne     $t7, $0, inc        # if $t7 != 0 then bypass sum
        sll     $0, $0, $0          # no op


sum:
    ## Add a given value to the sum
        addu    $t4, $t4, $t2       # sum = sum + fib(N)

inc:
    ## Increment fib's via xor swap magic
        xor     $t1, $t1, $t2       # xor swap magic
        xor     $t2, $t1, $t2       # xor swap magic
        xor     $t1, $t1, $t2       # xor swap magic
    ## Now $t1 = $t2 and $t2 = $t1

    ## Increment $t2 to next fib
        addu    $t2, $t1, $t2

    ## Is $t2 < 4,000,000?
    ## If so, go to loop
        sltu    $8, $t2, $t8        # If $t2 < 4,000,000 
                                    # then $8 = 1
        bne     $8, $0, even_test   # if $8 == $0 then jump to even_test
        sll     $0, $0, $0          # no op

print:
        li      $v0, 0x1            # system call #1 - print int
        move    $a0, $t4
        syscall                     # execute

        li      $v0, 0xA            # system call #10 - exit
        syscall

## End of Program

我该如何解决这个问题?

【问题讨论】:

    标签: assembly mips


    【解决方案1】:

    (昨天之前我对 MIPS 组装一无所知,但我会试一试)

    LUI 为 0x3D,ORI 为 0x900(4,000,000 为 0x3D0900)?

    【讨论】:

    • +1。你应该可以简单地写li $t8, 4000000,但这正是它将扩展到的内容。
    • 我必须在 or 之前进行左移,但是是的,这行得通,谢谢。
    • LUI 不需要任何左移。
    • 我的错。另外,哦,天哪,哦,天哪,我把最后的扭结弄出来了,它起作用了!甚至在 30 分钟前,我都没有想过我会在 MIPS 中得到一个可行的解决方案。 :D
    【解决方案2】:

    我猜这是问题所在?

       ori     $t8, $0, 4000000    # $t8 contains N limit
    

    MIPS 指令只有 16 位常量字段,因此您需要使用更复杂的序列构造大于 65535 的常量,或者从内存中加载它们。像这样的东西应该可以工作:

      ori      $t8, $0, 0x3d09     # 4 000 000 >> 8
      sll      $t8, $t8, 8
    

    认为“sll dest, src, count”是你在 MIPS 程序集中左移的方式,但我可能是错的。您还可以使用“li”宏指令,它采用任何 32 位常量并以某种方式将其放入寄存器中,必要时使用多个指令。

    【讨论】:

    • 谢谢,这解决了这个问题(我认为)。现在我只需要弄清楚为什么它会吐出错误的答案。
    【解决方案3】:

    我的第一个想法是使用两个寄存器,一个用于高位,一个用于低位。您必须同时跟踪它们,但这正是我想到的。

    【讨论】:

      猜你喜欢
      • 2014-01-31
      • 2019-04-13
      • 1970-01-01
      • 2014-11-09
      • 1970-01-01
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多