Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
936 views
in Technique[技术] by (71.8m points)

assembly - Storing addresses in a register for MIPS

I have allocated a certain amount of memory and would like to assign the location of this memory to variable I have declared in the .data section of the program. I know how to assign the memory location to the variable, but once I do that how can I use that variable to access the allocated memory?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If I understand your problem correctly, you will want to use the la (load address) instruction to get the address into a register. You will then use the lw (load word) and sw (store word) instructions to manipulate the data. For instance, consider the following piece of code

.data
tmpval: .word 5

__start:
  sub $sp, $sp, 12
  sw  $ra, 0($sp) # Return addy
  sw  $t0, 4($sp)
  sw  $t1, 8($sp)

  la  $t0, tmpval
  lw  $t1, 0($t0)  # $t1 == tmpval == 5
  li  $t1, $2      # $t1 == 2
  sw  $t1, 0($t0)  # tmpval == 2

  lw  $ra, 0($sp)
  lw  $t0, 4($sp)
  lw  $t1, 8($sp)
  add $sp, $sp, 12
  jr $ra

So in the inner-block of code you can see that you treat $t0 (or any other register for that matter) as a memory location and work with it appropriately.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...