0
votes

I'm working with mips. I am confused between machine code and mips code. if I use mips I can see the addresses of the branch is in number of words we need to jump from the instruction after the branch . what I don't understand is how it work "behind the sceen" ? and how the "shift left" by 2 is involved in this case? I need the word to be aligned by 4 byte(word) so actually the address that I see in mips languge is the number of word *4 =number of bytes we need to skeep?

another question: if the shift left was by 3? what can happend? it will give me the wrong address?

1
All instructions are 4 bytes in size, and must start at an address that is word-aligned (a multiple of 4). From that, it follows that the distance between two instructions always will be a multiple of 4 bytes. So it makes no sense to waste bits encoding the branch distance as a number of bytes when you know that it always will be a multiple of 4 (i.e. the two least significant bits will always be 0). Instead, you store the distance divided by 4 (shifted right by 2) in the instruction word, and when the instruction is executed the processor takes care of left-shifting that value.Michael

1 Answers

0
votes

In the MIPS architecture, branching is done by comparing a value as soon as the instruction is given, relying on no previous operation or flag. This takes up space in the instruction format, leaving only 16 bits to be used as the branch address. This is much too small of an address to be particularly useful, so instead of branching to that address, it branches relative to it's own address. The calculation of this branch offset is handled all by the assembler, so it looks like a branch operation would branch directly to a label/address.

Source: http://www.mrc.uidaho.edu/mrc/people/jff/digital/MIPSir.html