0

Let's say I want to do an lw:

lw $s1, l2 ($t1)

From what I understand I'm saving in $s1 the word pointed by register ($t1) + l2 (immediate which in this case is the offset).

But isn't the offset a reference to a certain block (in this case word) of a line specified by an index? How can I do the load only with an offset and without an index? (And isn't the offset going to change if I use, let's say, a register $t2 instead of a $t1?) I guess not cause otherwise the pointer arithmetic would be too complicated, so how does this work?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • 1
    If you don't want an index use `$0` (which is done automatically by most assemblers if you omit that part anyway). Not sure what you are trying to ask in the second part. – Jester Nov 04 '17 at 18:32
  • I guess im not really understanding how the $t1 + immediate part works. If, i used a register $t2 value instead of $t1 wouldnt the immediate change? – user8848592 Nov 05 '17 at 00:48
  • Just because you changed the register, no. If the register contains a different value but you still want to have the same result, then yes. `c=a+b`. If you change `b` but want the same `c` you will need to change `a` too. – Jester Nov 05 '17 at 00:50
  • Ok, i think i understood part of the problem. It is like all registers have the same "base address". Now what i dont understand is how do i know the value i want to put in an immediate? – user8848592 Nov 05 '17 at 00:59
  • Depends on what you want to do. If you want a known `c` and you know `b` then `a=c-b`. – Jester Nov 05 '17 at 01:00
  • The example you are giving with a subtraction (which reminds me of an ADD or a SUB)is not clarifying me very well sorry, i think i can narrow my question to, how do i know which value to put in an immediate in a lw? – user8848592 Nov 05 '17 at 01:03
  • How should we know? It depends on what **you** want to do! – Jester Nov 05 '17 at 01:04
  • For example, i want to load a stored word into a destination register. But before that i need to find where that word is kept, now nobody keeps a map of the positions in memory where all the words are stored, so how do i know which value to put in the immediate in order to find the address of a specific word i want to load. – user8848592 Nov 05 '17 at 01:09
  • You use a label. – Jester Nov 05 '17 at 01:09
  • What is that label? And why it is not represented in the load instruction? – user8848592 Nov 05 '17 at 01:11
  • You only use this form when you know the offset you want. Otherwise you leave it to the assembler/linker to figure it out, by using a label. E.g. `lw $s1, foo` with `foo` defined elsewhere. – Jester Nov 05 '17 at 01:26
  • *"nobody keeps a map of the positions in memory where all the words are stored"* - exact opposite, the code has to know all the positions, so it "keeps a map". With static/global instances you write labels in source, which are know to assembler during compilation, and it will calculate needed offsets from that. With dynamic instances you have to store the instance pointer somewhere where you can find it (either into local/static variable, or some kind of list of values, or temporary stack storage), and then you use this as "base" address of instance + minor offset to pick particular word. – Ped7g Nov 05 '17 at 08:15
  • registers have no base address, registers are registers, they reside directly in the CPU chip. They contain word value (32 bits). That word value can be used any way the instructions exist, so when you do `lw $1,($2)`, the word value in register `$2` is used as memory address. If you do `add $1, $2, $2`, the same word value in `$2` will be used as integer number for adding. But the `$2` register itself has no memory address, as it does not reside in memory at all, it's on CPU chip. Memory chip is far away beyond the data/address bus, communicating with CPU over the bus, requiring addresses. – Ped7g Nov 05 '17 at 08:18
  • @user8848592: Are you maybe mixing up "index" (as in indexed addressing mode) with the index bits of a set-associative cache? The two things have nothing to do with each other. The index in a MIPS addressing mode doesn't have to be a multiple of that cache line size or anything like that. – Peter Cordes Nov 05 '17 at 10:33
  • Another possible source of confusion: for this addressing to work you have to load the register with the base address first, e.g. `la $t1, array_of_words` and then you can do `lw $s1, 8($t1)` to access the third element. – Jester Nov 05 '17 at 11:56
  • Ok, i understood that the registers have no base address and i was confusing the index-bits with a set associative cache, but i still don`t understand how do i choose an immediate for the lw. – user8848592 Nov 05 '17 at 12:20
  • Have you seen my latest example? I chose `8` because I wanted the third element from a word array. As to which element you want depends on what you are doing. – Jester Nov 05 '17 at 12:40
  • Ok. I think i almost got it. If you had choosen immediate = 0, you would get the first element of the word. immediate =4, you would get the second element of the word, and so on(third element = 8) , it has to do with potences of 2 . Now since we are talking about words is that element like a word or is it a char? – user8848592 Nov 05 '17 at 12:55
  • In my example it was a machine word, that is a 32 bit integer. If you had a char array that would have elements of 1 byte each, so third element is offset 2. – Jester Nov 05 '17 at 16:01
  • Thanks, i think i got it! – user8848592 Nov 08 '17 at 17:04

0 Answers0