简体   繁体   中英

MIPS - Accessing an array

So i have an array, that is filled previously with 1's or 0's, whenever i try to compile this code MIPS gives me a syntax error, could someone explain what this syntax error is? I'm having trouble understanding why you can't access the array like that, of course $t1 is a counter for the index, which increments up through 100

  slti $t7, prim_flag($t1), 1 # checks if prim_flag ($t1) < 1 stores 1 if so stores 0 if not beq $t7, 0, print_numbers # checks if the value in $t7 is 0, if so jump to end_game 

and the array:

 .data test: .asciiz "Printing numbers:" test_2: .asciiz "Before loop" space: .asciiz " " done: .asciiz "\\n Done printing the array" numbers: .word 0:210 numbers_size: .word 210 prim_flag: .word 1:210 

The only valid operand combination for slti is register,register,immediate . You're trying to use register,memory,immediate , and there's simply no such version of slti in the MIPS instruction set.

Practically every time you need to perform an operation on some data in memory in MIPS assembly, you first have to load that data into a register using lb/lh/lw ; then you can perform the operation you need on that register; and finally write some result back to memory if necessary.


Also note that the constant to the left of the parentheses in prim_flag($t1) is an offset , not the base address. The base address is the part that's inside the parentheses, and has to be a register. And since the offset has to fit in 16 bits due to how MIPS instructions are encoded, it's possible that prim_flag won't fit. So you might have to load the address of prim_flag into some register, then add that register plus $t1 and store the sum in a third register, and then read from memory using that last register as the base address.

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