Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Assembly MIPS-如何在根目录下查找?_Assembly_Mips - Fatal编程技术网

Assembly MIPS-如何在根目录下查找?

Assembly MIPS-如何在根目录下查找?,assembly,mips,Assembly,Mips,只想知道MIPS中的一个函数,它可以在我的MIPS程序中的任何数字的根下执行。.data .文本 格洛博梅因酒店 新界北总区 Sqrt: 移动$v1,$a1#$v0=x=N 李$t0,0#柜台 sqrLoop: 分区$t8、$a1、$v1#N/x 添加$v1、$t8、$v1#x+N/x 分区$v1,$v1,2#(x+N/x)/2 加上$t0,$t0,1 blt$t0,20,平方英尺 jr$ra .end Sqrt 这是一个函数,当调用它时,它将取出数字的平方根。只要在必要或需要的地方写上“jal

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

.data

.文本

格洛博梅因酒店

新界北总区

Sqrt:

移动$v1,$a1#$v0=x=N 李$t0,0#柜台

sqrLoop:

分区$t8、$a1、$v1#N/x 添加$v1、$t8、$v1#x+N/x 分区$v1,$v1,2#(x+N/x)/2 加上$t0,$t0,1 blt$t0,20,平方英尺

jr$ra

.end Sqrt

这是一个函数,当调用它时,它将取出数字的平方根。只要在必要或需要的地方写上“jal Sqrt”就可以调用函数。

.data

.文本

格洛博梅因酒店

新界北总区

Sqrt:

移动$v1,$a1#$v0=x=N 李$t0,0#柜台

sqrLoop:

分区$t8、$a1、$v1#N/x 添加$v1、$t8、$v1#x+N/x 分区$v1,$v1,2#(x+N/x)/2 加上$t0,$t0,1 blt$t0,20,平方英尺

jr$ra

.end Sqrt


这是一个函数,当调用它时,它将取出数字的平方根。只要在必要或需要的地方写上“jal Sqrt”来调用函数。

您可以使用简化版的牛顿法来找到整数的根

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

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

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
试试这个

   #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
试试这个

   #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

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

假设你想要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

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

假设你想要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

欢迎来到堆栈溢出。请阅读并将代码格式化为代码。欢迎使用堆栈溢出。请阅读并将您的代码格式化为代码。您想知道关于它的什么?您是指平方根吗?或者你指的是任意根,比如
pow(x,1.0/3)
Ok说真的,在过去的两三天里,MIPS的答案是怎么回事?新用户对MIPS问题的回答层出不穷,包括那些已经存在多年的问题,其中一些已经得到了回答,其余大部分已经在评论中得到了回答。是否有课堂作业来查找和回答MIPS问题?重复你想知道的内容?你的意思是平方根吗?或者你指的是任意根,比如
pow(x,1.0/3)
Ok说真的,在过去的两三天里,MIPS的答案是怎么回事?新用户对MIPS问题的回答层出不穷,包括那些已经存在多年的问题,其中一些已经得到了回答,其余大部分已经在评论中得到了回答。是否有一个课堂作业来查找和回答MIPS问题?重复的如果你要使用整数数学(不是FP),使用
srl
除以2,而不是另一个缓慢的
div
。此外,使用
add$t4,$t4,-1
/
bne$zero,$t4 sqrLoop
循环将$t4向下计数到零。(两个寄存器之间的BNE是一条真正的硬件指令;针对立即数常量的BNE只是一条伪指令,必须先将
20
放入寄存器中)。如果要使用整数数学(不是FP),请使用
srl
除以1除以2,而不是另一条缓慢的
div
。此外,使用
add$t4,$t4循环,-1
/
bne$0,$t4 sqrLoop
将$t4向下计数至零。(两个寄存器之间的BNE是一条真正的硬件指令;针对立即数常量的BNE只是一条伪指令,必须先将
20
放入寄存器)。此代码块与在此之前几分钟发布的代码块相同(除了顶部附近的一点缩进)。它实现了一种二进制一次一位算法,它不同于
x=(x+n/x)/2
Newton迭代。(算法与中的算法类似)。所以它可能是剽窃的,并且错误地描述了它实现的算法。这个代码块与几分钟前发布的代码块完全相同(除了顶部有一点缩进)。它实现了一种二进制一次一位算法,它不同于
x=(x+n/x)/2
Newton迭代。(算法与中的算法类似)。因此,它可能是剽窃的,并且错误地描述了它实现的算法。相同的算法和非常相似的代码,注释更糟糕,标签名称更不清晰。在两种情况下使用不同的寄存器,并添加了NOP以在具有分支延迟槽的MIPS上工作。(但只有在
b
而不是
j
指令之后?)也许只是翻译相同的伪代码/C的巧合。相同的算法和非常相似的代码,注释更差,标签名称更不清晰。在两种情况下使用不同的寄存器,并添加了NOP以在具有分支延迟槽的MIPS上工作。(但只有在
b
而不是
j
指令之后)也许只是翻译相同伪代码/C的巧合。