简体   繁体   中英

Copying content of array to another array and printing in MIPS assembly

Im new to assembly language. Ive managed with a lot of help to write a piece of code that takes an array and prints it. I now want to just simply copy the content of the first array into a second array, and print the second array. (later I will be manipulating it so the second array prints the first array backwards pretty much)

I just want to learn how to copy an array into a second array and print it. Below is my code that just prints the first array

# PrintList.asm
.data
Sz: .word 10
Array: .word 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
NL: .asciiz " "

.text

main:
lw $s7, Sz 
move $s1, $zero  
move $s2, $zero 

print_loop:
bge $s1, $s7, print_loop_end 

lw $a0, Array($s2) 
li $v0, 1
syscall
la $a0, NL 
li $v0, 4
syscall
addi $s1, $s1, 1 
addi $s2, $s2, 4 
j print_loop 

print_loop_end:

any guidance or tips would be helpful thanks

Some comments about your program first:

move $2, $zero
.....
lw $a0, Array($s2) 

When using MIPS in a real environment (example: many WLAN routers use MIPS processors) the second line will not work. The reason is that the address of "Array" will typically be in a range above 0x10000 and the line in your code will not be able to access addresses above 0x7FFF.

It would be better to use the following two lines:

la $2, Array
...
lw $a0, 0($s2) 

You should also be aware that "syscall" calls the operating system. This means that the meaning of the number in the "$v0" register depends on the operating system (or simulator) used. In Unix the following lines:

li $v0, 1
syscall

would call the "exit()" system call that will stop the program at once. When working with the "syscall" instruction you should mention that you use for example the SPIM simulator (where $v0=1 means: print integer number).

Now about the actual problem:

To copy the array you simply add a "sw" instruction after the "lw" instruction:

lw $a0, Array($s2)
sw $a0, SecondArray($s2)

Some real MIPS processors do not allow using a register loaded using "lw" in the following instruction. So you should re-order the instructions in a way to avoid this:

lw $a0, Array($s2)
li $v0, 1
sw $a0, SecondArray($s2)

The problem with the addresses > 0x10000 still exists. You'll need a second array pointer if your program should be functional with such addresses:

la $s2, Array
la $s3, SecondArray
  ...
lw $a0, 0($s2)
li $v0, 1
sw $a0, 0($s3)
  ...
addi $s2, $s2, 4
addi $s3, $s3, 4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM