首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >存储词(sw)

存储词(sw)
EN

Stack Overflow用户
提问于 2012-09-19 18:06:20
回答 1查看 35K关注 0票数 3

我试图将一个整数存储到$s0中,这是一个循环标签。

但问题是,我将打印一个整数,并将其保存在$s0中。

我做了以下工作

代码语言:javascript
复制
sw $t7,0($s0)
addi $s0,$s0,4

我认为以下内容将允许我将每个整数存储在$s0中的一个槽中,但如果我lw,则只能得到打印出来的最后一个整数

下面的代码:我试图用粗体存储一个单词,但就像你说的那样,它覆盖了这是一个家庭作业问题,所以我真的很想学习,请给出一些方向,这样我下次就知道了。

代码语言:javascript
复制
        .data                               


n1:     .asciiz "\n"                        #ASCII for a new line

        .align 2                            



name:   .asciiz "Fibonacci\n\n\n"               #Lab Title and Student Name
        .align 2

msg1:   .asciiz "Fibonacci result " #note the space
        .align 2                            


msg2:   .asciiz " is "                      #note the space
        .align 2

**array:  .space 20                     #define the array**

        .text                               #
        .globl  main                        #declare main to be global



main:   



        la  $a0,name                        #load the ram address of "name" into $a0
        li  $v0,4                           #system call, type 4, print a string, $a0
        syscall                             #call the "OS" to perform operation

        li  $t2,0                           # $t2 :=0; initial value of F(n-2)
        li  $t1,1                           # $t1 :=1; initial value of F(n-1)

        li  $t4,0                           #index, n.

                                            #loop is a local Variable
        li  $t5,11                      #loading immediate preset value for $t5 




        **la $t7,array                      #load address (array) into $t7**





loop:   addu    $t0,$t1,$t2                 # F(n) = F(n-1) + F(n-2)

        **#Only print the last 5 numbers**
        sub $t3,$t5,$t4
        li $t6,5
        bgt $t3,$t6,Printl


        la $a0,msg1                         # $a0 := address of message 1
        li $v0,4                            #system call, type 4, print a string
        syscall                             #call the "OS"

        move    $a0,$t4                     # $t4 contains the current value of n
        li  $v0,1                           #system call, type 1, print an integer $a0
        syscall                             #call the "OS"

        la $a0,msg2                         # $a0:=address of "msg2"
        li $v0,4                            #system call, type 4, print a string
        syscall                             #call the "OS"

        move    $a0,$t0                     # $a0 :=$t0, which is F(n)
        li  $v0,1                           #system call, type 1, print an integer $a0
        syscall                             #call the "OS"


        **sw $s0, 4($t7)**





        la  $a0,n1                          # $a0 := address of "n1"
        li  $v0,4                           #system call, type 4, print a string
        syscall                             #call the "OS"




        addi    $t4,1                       # $t4 := $t4 + 1; increment n
        move    $t2,$t1                     # $t2 := $t1; old F(n-2) because old F(n-1)
        move    $t1,$t0                     # $t1 := $t0; old F(n-2) because old F(n)



        beq     $t5,$t4,Exit            #Conditional Check | exit







        j       loop                        # branch unconditionally to loop ($t3 != 0)

Exit:   
        la $a0,n1                           # $a0 := address of message 1
        li $v0,4                            #system call, type 4, print a string
        syscall                             #call the "OS"

        #la $t7,array
        #li $v0,1
        #sw $s0, 4($t7)
        #syscall


        li      $v0,10                      #system call, type 10, standard exit
        syscall                             #call the "OS"

Printl: 
        addi $t4,1
        move $t2,$t1
        move$t1,$t0
        j loop
EN

回答 1

Stack Overflow用户

发布于 2013-10-17 19:37:06

不知道你是否还在这。)但如果你是,而且在两个学期之后你仍在偶然地学习MIPS,下面是一些可能有帮助的东西。

您已经注意到,sw $s0, 4($t7)将把$s0的内容写入($t7 + 4)所指向的地址。由于$t7的值从未更改过,所以您将不断地将$s0的内容写入相同的地址。

如果要将$s0的内容存储到从array开始的连续单词,则需要初始化指针,执行您想用它做的事情,然后增加指针。

代码语言:javascript
复制
la $t7,array
loop:
# do something to set $s0
sw $s0, 0($t7)
# do something to break out of loop if condition is met
addi $t7, 4
j loop

不要忘记,您已经将array定义为20个字节--这只是5个单词。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12500405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档