【问题标题】:MIPS program to get two integers from the user, save it to an array, and print itMIPS 程序从用户那里获取两个整数,将其保存到数组中,然后打印出来
【发布时间】:2013-11-08 17:41:49
【问题描述】:

我正在尝试编写一个 MIPS 汇编程序来从用户那里获取两个整数,将其保存到数组中的内存中并打印出来。这就是我到目前为止所拥有的。我的程序打印了一些我没有输入的大数字。我对这个游戏很陌生。请有人帮忙!

这是我的代码:

.text
.globl main

    main:
        li $v0, 4       
        la $a0, prompt  
        syscall

        li $t0, 0      #count for the loop to get two integers
    getnum:
        li $v0, 5   #read integer
        syscall
        sw $v0, num($s0)    #save the integer from user input into num and $s0 has address for num, I'm not sure if i did this right
        addi $s0, $s0, 4    # increment $s0 by 4 to save another integer
        addi $t0, $t0, 1    #increment the counter
        ble $t0, 1, getnum       #if counter $t0, is less then or equal to 1, it will go through the loop again

    printnum:   
        la $a0, num($s0)        #load address of num to print
        li $v0, 1           #print int
        syscall 
        addi $s0, $s0, 4    
        addi $t1, $t1, 1
        ble $t1, 1, printnum        #does it twice

        li $v0, 10  
        syscall
.data 

    num:
         .word 0, 0  # i want to store my two numbers here
    prompt: 
        .asciiz "Enter 2 positive integers: "

【问题讨论】:

    标签: arrays mips


    【解决方案1】:

    你的问题是双重的。

    首先,您加载的是整数的地址,而不是实际的整数。修复此更改 lalw

    其次,因为您在getnum 循环中将$s0 增加两次并立即在printnum 循环中使用它,所以它太超前了,您需要添加move $s0, $zero 来解决这个问题。

    此外,您的代码似乎依赖于 $s0 以 0 值启动程序这一事实,这可能不是一个很好的假设。最好将其显式设置为零。

    【讨论】:

    • 数组的使用访问不也是不对的吧?当@pdhmal1 尝试使用num($s0) 存储和加载单词时,不应该是$s0(num) 吗?本质上格式是register(indexValue),而它应该是indexValue(register)
    猜你喜欢
    • 1970-01-01
    • 2014-09-23
    • 2017-10-07
    • 2020-03-03
    • 2015-03-05
    • 2014-01-10
    • 2014-03-15
    • 1970-01-01
    • 2021-05-01
    相关资源
    最近更新 更多