以相反的顺序打印数组,汇编

时间:2016-02-28 16:57:02

标签: assembly mips spim qtspim

因为我在上一个问题中最后一次说过:

我在我的大学里练习,我想得到一些帮助!首先,我是mips语言的新手!所以,我的练习是要求在qtSpim中创建一个程序,它将从控制台读取8个整数,然后将它保存在数组中的内存中,最后它将以保留顺序打印这些整数的六个!你能帮我一点点编码吗?

所以,我尝试了一点,这就是我到目前为止所做的:

  # $18=c

.data

myArray: .space 32

.align 2   

    str_s:  .asciiz "give me 8 numbers in 8 lines:" 
    str_nl: .asciiz "\n"



.text

    .globl main     #label "main" must be global
    .globl loop

main:

la $17, myArray

                            #BUILD THE PROMPT
    addi $2, $0, 4    # system call code for print_string
    la   $4, str_s    # pseudo-istruction: address of string
    syscall           # print the string from str_s

    add $18, $0, $0    # c=0

loop:

    addi $2, $0, 5   #system call for read_string
    syscall
    add $16, $2, $0  #copy return int from $2 to $16
    sw  $16, 0($17)  #save int from $16 to the array

    addi $18, $18, 1   # c=c+1
    bne  $18, 8, loop  #repeat while (c!=n)

loop:

j main

因此,你可以在第二个循环中,我必须以相反的顺序打印数组的整数...在谷歌所有我能找到的是打印整数但现在我有一个字符串是不同的?我应该用开头的整数来做吗?

`

1 个答案:

答案 0 :(得分:-1)

.data
arr: .space 32
msg1: .asciiz "put in first integer"
msg2: .asciiz "put in second integer"
msg3: .asciiz "put in third integer"
msg4: .asciiz "put in fourth integer"
msg5: .asciiz "put in fifth integer"
msg6: .asciiz "put in sixth integer"
msg7: .asciiz "put in seventh integer"
msg8: .asciiz "put in eighth integer"
.globl main
.text
main:
la $s0, arr
li $v0, 4
la $a0, msg1
syscall

li $v0, 5
syscall
sw $v0, 28($s0)

li $v0, 4
la $a0, msg2
syscall
li $v0, 5
syscall
sw $v0, 24($s0)

li $v0, 4
la $a0, msg3
syscall
li $v0, 5
syscall
sw $v0, 20($s0)

li $v0, 4
la $a0, msg4
syscall
li $v0, 5
syscall
sw $v0, 16($s0)

li $v0, 4
la $a0, msg5
syscall
li $v0, 5
syscall
sw $v0, 12($s0)

li $v0, 4
la $a0, msg6
syscall
li $v0, 5
syscall
sw $v0, 8($s0)

li $v0, 4
la $a0, msg7
syscall
li $v0, 5
syscall
sw $v0, 4($s0)

li $v0, 4
la $a0, msg8
syscall
li $v0, 5
syscall
sw $v0, 0($s0)

li $v0, 1
lw $a0,0($s0)
syscall 

 li $v0, 1
 lw $a0,4($s0)
 syscall 

 li $v0, 1
 lw $a0,8($s0)
 syscall 

li $v0, 1
lw $a0,12($s0)
syscall 

li $v0, 1
lw $a0,16($s0)
syscall 

li $v0, 1
lw $a0,20($s0)
syscall 

 li $v0, 1
lw $a0,24($s0)
syscall 

li $v0, 1
lw $a0,28($s0)
syscall 

li $v0, 10#terminate execution
syscall

基本上,我在这里做的是将我在arr中创建的空间分配给$ s0然后我使用系统调用来获取输入并将它们存储在$ s0中,最后但并非最不重要的是我打印它们。容易非常的馅饼。

代码现已修复!!