Question

.data 

source: .word 3, 1, 4, 1, 5, 9, 0 
dest: .word 0, 0, 0, 0, 0, 0, 0 
countmsg: .asciiz " values copied. " 

.text 
main: add $s0, $0, $ra # Save our return address 
      la $a0, source 
      la $a1, dest 
loop: lw $v1, 0($a0) # read next word from source 
      addi $v0, $v0,1 # increment count words copied 
      sw $v1, 0($a1) # write to destination 
      addi $a0, $a0,1 # advance pointer to next source 
      addi $a1, $a1,1 # advance pointer to next dest 
      bne $v1, $zero, loop # loop if word copied not zero 
loopend: 
      move $a0, $v0 # We want to print the count 
      li $v0, 1 
      syscall # Print it 
      la $a0, countmsg # We want to print the count message 
      li $v0, 4 
      syscall # Print it 
      li $a0, 0x0A # We want to print '\n' 
      li $v0, 11 
      syscall # Print it 
      jr $s0 # Return from main. We stored $ra in $s0 

Hello, basically i wanted my program to copy integers from memory address $a0 to memory address $a1, until it reads a zero value. The zero value should not be copied. The number of integers copied should be stored in $v0. Can someone please tell me why this doesn't work and where I might have gone wrong? Cheers.

Was it helpful?

Solution

The problem here is that you are advancing your pointers $a0 and $a1 by 1, but a word is 4 bytes in length.

Change to:

addi $a0 $a0 4
addi $a1 $a1 4
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top