Reputation: 836
I am trying to understand assembly a little better, and I am not really sure how the i
(immediate) comes into play for BNE
. Please forgive the noobness.
If BNE
writes like this:
BNE $1, $2, loop
which means "goto loop if $1
does not equal $2
" then I am assuming that the loop is the immediate part. Correct?
Is this just a program counter or a location of the loop? So, if the loop was set at some instruction count (let's just call it 0x0001
), would this loop address (or instruction count) be set in this i
?
Maybe it would be better if I wrote it out like this...
opcode = 5 | reg1 = whatever addr | reg2 = whatever addr | immediate = 0x0001
Does that look right, or am I just really confused?
Upvotes: 1
Views: 1077
Reputation: 71526
the assembler takes the address loop (that is really what it is a name for some address somewhere) it does the work for you to compute the offset, and encodes that offset into the instruction for you. Otherwise you would have to say bne $1, $2, +28, hand computed number and then every time you modify your code between these two points you would have to re-adjust every one of these offsets.
Some assemblers or linkers actually, may go so far as to add a trampoline for you. Say for example you have some instruction set that the relative jump is only 128 instructions, and you use a label that is not defined in that object. The assembler might be so kind as to place an unconditional branch within 128 instructions to the place you really wanted to go. some might not and you end up with an error that it cannot encode the offset in the instruction you asked for.
where I am headed is that you program with labels (addresses) and let the assembler do some of the work for you. As in this case it converts the label you have to a distance from the current instruction and encodes that.
Upvotes: 1