【问题标题】:MIPS - How to find under root? [closed]MIPS - 如何在根目录下找到? [关闭]
【发布时间】:2021-03-31 13:34:16
【问题描述】:

只是想知道 MIPS 中的一个函数,它可以在我的 MIPS 程序中以任何数字为根。

【问题讨论】:

  • 你想知道什么?
  • 你的意思是 平方 根吗?还是您的意思是任意根,例如pow(x, 1.0/3)
  • 好吧,说真的,最近 2 或 3 天的 MIPS 答案是怎么回事?新用户对 MIPS 问题的回答如潮水般涌现,包括多年前的问题,其中一些已经回答,其余大部分已经在 cmets 中回答。是否有课堂作业来查找和回答关于 SO 的 MIPS 问题?

标签: assembly mips


【解决方案1】:

你可以使用简化版的牛顿法求整数的根

x=N
iterate 20 times:
x'=(x+N/x) /2
x=x'

Mips 实现

.data
.text
main:
    li $t0,25        #N

    move $t1,$t0     #x
    li $t4,0    #loop variable
sqrLoop:
    #Newton Formula
    div $t3, $t0, $t1   # N/x 
    add $t1, $t3, $t1   # x + N/x 
    div $t1, $t1, 2     # (x + N/x)/2 

    #loop
    add $t4, $t4, 1 
    blt $t4, 20, sqrLoop 


end:

     li $v0,10
    syscall

【讨论】:

  • 如果您要使用整数数学(不是 FP),请使用 srl 除以 1 来除以 2,而不是另一个慢速 div
  • 另外,循环使用 add $t4, $t4, -1 / bne $zero, $t4 sqrLoop 以将 $t4 向下 计数为零。 (两个寄存器之间的 BNE 是真正的硬件指令;针对立即数的 BNE 只是伪指令,必须先将 20 放入寄存器中)。
【解决方案2】:

试试这个。

   #DATA

   .data

   square: .asciiz "Enter the number you wish to find the square root for: "
   answer: .asciiz "The answer is: "
   newline: .asciiz "\n"

   #Text

   .text
   .globl main

    main:
    li $v0, 4               #Prompt user for input
    la $a0, square
    syscall
    
    li $v0, 5               #Receive said input
    syscall
    move $a0, $v0
    
    move $t4, $zero         #Move variables to t registers
    move $t1, $a0
    
    addi $t0, $zero, 1      #Set $t0 to 1
    sll $t0, $t0, 30        #Bit Shift $t0 left by 30
    
    #For loop
    loop1:
        slt $t2, $t1, $t0
        beq $t2, $zero, loop2   
        nop
        
        srl $t0, $t0, 2         #Shift $t0 right by 2
        j loop1
        
    loop2:
        beq $t0, $zero, return  
        nop
        
        add $t3, $t4, $t0       #if $t0 != zero add t0 and t4 into t3
        slt $t2, $t1, $t3       
        beq $t2, $zero, else1   
        nop
        
        srl $t4, $t4, 1         #shift $t4 right by 1
        j loopEnd
        
    else1:
        sub $t1, $t1, $t3       #Decrement $t1 by $t3
        srl $t4, $t4, 1         #Shift $t4 right by 1
        add $t4, $t4, $t0       #then add $t0 to that
        
    loopEnd:
        srl $t0, $t0, 2         #shift $t0 to the right
        j loop2
        
    return:
        li $v0, 4               #print out the answer then exit
        la $a0, answer
        syscall
    
        li $v0, 1
        move $a0, $t4
        syscall
        
        li $v0, 10
        syscall

【讨论】:

  • Finding square root of an integer on MIPS assembly 相同的算法和非常相似的代码,但cmets 更差,标签名称不太清晰。在几种情况下使用不同的寄存器,并添加 NOP 以在具有分支延迟槽的 MIPS 上工作。 (但只是在b 不是j 指令之后?)也许只是翻译相同的伪代码/C 的巧合。
【解决方案3】:

.数据

.文本

.globl 主要

.ent main

平方:

移动 $v1, $a1 # $v0 = x = N li $t0, 0 # 计数器

sqr循环:

div $t8, $a1, $v1 # N/x 添加 $v1, $t8, $v1 # x + N/x div $v1, $v1, 2 # (x + N/x)/2 添加 $t0, $t0, 1 blt $t0, 20, sqrLoop

jr $ra

.end Sqrt

这是一个函数,调用时会取出数字的平方根。只需在必要或需要的地方编写“jal Sqrt”即可调用该函数。

【讨论】:

  • 欢迎来到 Stack Overflow。请阅读editing help 并将您的代码格式化为代码。
【解决方案4】:

你可以试试这个算法,它给出的整数小于或等于你的数字的平方根。

假设你想要 n 的平方根。然后不断重复以下计算:

x = (x + n/x) / 2

选择 x = n 开始并不断重复直到 x 停止变化。

这是您可以在程序中添加的以下 MIPS 程序库。

#SquareRoot.s

#DATA
.data

square: .asciiz "Enter the number you wish to find the square root for: "
answer: .asciiz "The answer is: "
newline: .asciiz "\n"

#Text

.text
.globl main

main:
    li $v0, 4               #Prompt user for input
    la $a0, square
    syscall
    
    li $v0, 5               #Receive said input
    syscall
    move $a0, $v0
    
    move $t4, $zero         #Move variables to t registers
    move $t1, $a0
    
    addi $t0, $zero, 1      #Set $t0 to 1
    sll $t0, $t0, 30        #Bit Shift $t0 left by 30
    
    #For loop
    loop1:
        slt $t2, $t1, $t0
        beq $t2, $zero, loop2   
        nop
        
        srl $t0, $t0, 2         #Shift $t0 right by 2
        j loop1
        
    loop2:
        beq $t0, $zero, return  
        nop
        
        add $t3, $t4, $t0       #if $t0 != zero add t0 and t4 into t3
        slt $t2, $t1, $t3       
        beq $t2, $zero, else1   
        nop
        
        srl $t4, $t4, 1         #shift $t4 right by 1
        j loopEnd
        
    else1:
        sub $t1, $t1, $t3       #Decrement $t1 by $t3
        srl $t4, $t4, 1         #Shift $t4 right by 1
        add $t4, $t4, $t0       #then add $t0 to that
        
    loopEnd:
        srl $t0, $t0, 2         #shift $t0 to the right
        j loop2
        
    return:
        li $v0, 4               #print out the answer then exit
        la $a0, answer
        syscall
    
        li $v0, 1
        move $a0, $t4
        syscall
        
        li $v0, 10
        syscall

【讨论】:

猜你喜欢
  • 2015-01-01
  • 2017-08-16
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-13
  • 2021-07-12
  • 2011-10-24
相关资源
最近更新 更多