15

I'm trying to create an array in MIPS Assembly, and then add all the elements together. However, when I try to assemble the following, it says

Error in read_array line 1 position 7: ".word" directive cannot appear in text segment Assemble: operation completed with errors.

Here's my assembly:

list: .word 3, 2, 1, 0, 1, 2
li $t0, 0x00000000  #initialize a loop counter to $t0
li $t4, 0x00000005  #last index of array
li $t3, 0x00000000  #this will hold our final sum
la $t1, list  #the address of list[0] is in $t1

loop: addi $t0, $t0, 0x00000001 #index++
  add $t5, $t0, $t0 #array index X2
  add $t5, $t0, $t0 #array index X2 again
  add $t6, $t5, $t1 #4x array index in $t6

  lw $t2, 0($t6)   #load list[index] into $t2
  add $t3, $t3, $t2 #$t3 = $t3 + $t2
  beq $t0, $t4, end
  j loop

end:

Thanks!

2
  • I am aware of some logical errors in this code, but I got my question answered. Thanks! Commented Mar 3, 2010 at 3:47
  • That's really weird, IDK if this limitation is intentional to protect beginners from mixing data with code and having their program crash when execution falls into their data, or what. In most assemblers, you can use .byte / .word or db / dd anywhere to emit whatever bytes you want at any position. (e.g. to emit a non-default encoding of an instruction for some reason.) Commented Dec 5, 2017 at 4:01

2 Answers 2

16

You have to put this line:

list: .word 3, 2, 1, 0, 1, 2

Into the .data section. Check this quick tutorial.

Sign up to request clarification or add additional context in comments.

1 Comment

The 'quick tutorial' link is off.
6

The error is telling you you can't put data (.word 3, 2) in the code segment. "Text segment" is an old style term meaning "code segment" http://en.wikipedia.org/wiki/Code_segment

The assembler wants you to declare a data segment and put the array there. I've never done Mips assembler, but I would expect it to be something like this

.data
list: .word 3, 2, 1, 0, 1, 2

.text
start:
li $t0, 0x00000000  #initialize a loop counter to $t0
li $t4, 0x00000005  #last index of array
li $t3, 0x00000000  #this will hold our final sum
la $t1, list  #the address o

2 Comments

It would be .text rather than .code in most assemblers I've used.
@Carl: you're probably right, especially given the error message. I'll change it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.