学习MIPS:创建一个简单的程序来读取数字并打印它们

时间:2017-06-05 14:46:05

标签: mips

我正在努力学习MIPS,我发现它并不像C ++那么容易。

现在,我要编辑/编写一个程序,该程序将从用户那里获取十个数字,然后在它们之间用逗号打印这些数字,并在最后一个数字后面没有逗号。例如:

Enter a number 1
Enter a number 2
Enter a number 9
Enter a number 11
Enter a number 99
Enter a number 12
Enter a number 24
Enter a number 90
Enter a number 17
Enter a number 82
1, 2, 9, 11, 99, 12, 24, 90, 7, 82

这是给定的代码:

    .data
    .space 40                 # set aside 40 bytes for 10 integers
prompt:
    .asciiz "Enter a number " # address 0x10010028
comma:
    .asciiz ", "              # address 0x10010038

.globl main
.text
main:
    lui $a0, 0x1001 # get start of data segment
    jal getdata     # call getdata function

    lui $a0, 0x1001 # get start of data segment
    jal print       # call print function

    ori $v0, $0, 10 # set command to exit
    syscall         # end program

getdata:
    addi $sp, $sp, -4    # allocate space on stack
    sw $ra, 0($sp)       # save $ra
    ori $t1, $a0, 0      # address of array

    lui $t1, 0x1001      # start of data
    ori $a0, $a0, 0x0028 # address of prompt
    ori $t0, $0, 10      # counter

top:
    beq $t0, $0, ret     # while not 0

    jal printstr         # call function to print prompt
    ori $v0, $0, 5       # set command to read integer
    syscall              # read int
    sw $v0, 0($t1)       # save int in memory
    addi $t1, $t1, 4     # increment to next location

    addi $t0, $t0, -1    # decrement counter
    j top                # repeat

ret:
    lw $ra, 0($sp)       # restore return address
    addi $sp, $sp, 4     # clean up stack
    jr $ra               # return from call

    # print
    # parameter: $a0 holds address of list in memory
    # purpose: print the list separated by commas
print:
    #to be completed
    jr $ra

    # printstr
    # paramters: $a0 holds address of string to print
    # purpose: print the string parameter
printstr:
    #to be completed
    jr $ra

我相信我在努力的是“印刷”部分。我有 如下所示:printstr:和getdata:

printstr:
    ori $v0, $0, 4  # command to print string at $a0
    syscall
    jr $ra          # return from call

getdata:
    addi $sp, $sp, -4    # allocate space on stack
    sw $ra, 0($sp)       # save $ra
    ori $t1, $a0, 0      # address of array

    lui $t1, 0x1001      # start of data
    ori $a0, $a0, 0x0028 # address of prompt
    ori $t0, $0, 10      # counter

然后这是我一直试图用于循环的代码......

print:
    addi $t2, $t2, 10
    addi $t0, $t0, 1      # Enter into the loop by increment 1
    ori $v0, $0, 4        # Print the 1st value and then loop
                          # start from the 2nd value

    syscall

    jal loop # Jump to loop

loop:
    beq $t0, $t2, end  # checks if t2=t0, if it does then ends the program
    lui $t3, 0x1001    # loads the upper limit of t3 to 1001
    or $a0, $0, $t3    # sets the upper limit of a0 to 1001
    ori $v0, $0, 1     # sets v0 to print integer
    syscall            # prints integer
    addi $t3, $t3, 4   # moves t3 to the next address

    ori $a0, $a0, 0x0038  # sets a0 to the address of comma
    ori $v0, $0, 4        # Print string at $a0
    syscall
    addi $t0, $t0, 1      # increases the value at t0 by 1
    jr $ra

但是,当我运行该程序时,它会吐出“268500992,”。所以我没有得到我的输入数字或正确的逗号数量。我知道这意味着我的循环没有一直运行。我不应该使用和伪代码,只有我到目前为止学到的东西 - 所以数组是不可能的。

有人可以帮我弄清楚我错过了什么吗?它是否与堆栈值有关(我不完全理解)?

1 个答案:

答案 0 :(得分:1)

好的,我认为printstr的代码是正确的,而且getdata看起来也很好(有点奇怪,但你没有写出来,是吗?)这些错误都在print。我不打算为你拼写,因为这是作业,但print中循环的结构应该与getdata中的循环结构非常相似:

    # on entry, $a0 = address of integer array
print:
    ori $t0, $0, 10      # counter
    ori $t1, $a0, 0      # t1 points to current array element

print_loop:
    ### if this isn't the first iteration, print ", "
    ### load value at $t1 into $a0
    ### print the integer in $a0

    addi $t1, $t1, 4     # advance to next array element
    addi $t0, $t0, -1    # decrement counter
    bne  $t0, $0, print_loop

    ### print a carriage return

    jr $ra               # return from call

每个以###为前缀的内容都是您需要编写代码的占位符。