Assembly instructions bne and br (NIOS II). How is their offset calculated?

I have this assembler code, which I must translate to machine code in binary form:

.text .align 2 .global main .equ val,0x4712 main: movi r16,val movi r17,0 loop: addi r17,r17,1 subi r16,r16,1 bne r16,r0,loop stop: br stop .end 

and I'm not sure how bne r16, r0, loop, and br stop are interpreted.

My typing instruction states that the bne statement does the following:

 if(rA != rB) then PC ← PC + 4 + σ(IMM16) else PC ← PC +4 

which, as I understand it, is a program counter incrementing by 4 + offset or just 4.

But, in my case, what is the value of offset / IMM16? The reference instruction set says:

"In command encoding, the offset specified by IMM16 is treated as the signed number of bytes relative to the instruction immediately following bne."

My interpretation of this is that the value of IMM16 is the "distance" to the next instruction address, but I have no idea if this is correct. The hex address for bne is 0x40010 and 0x40014 for br, so what does this mean that the value of IMM16 is 4? (which will cause the PC to jump through the address 0x40014 if rA! = rB?)

+4
source share
1 answer

Disclaimer: I'm not quite sure which instruction is installed, so I will try to stick to what usually holds for assembly languages.

if (rA! = rB), then PC ← PC + 4 + σ (IMM16) else PC ← PC +4

which, as I understand it, is a program counter incrementing by 4 + offset or just 4.

It is right. It might be useful to keep in mind that the CPU basically always executes PC ← PC + 4 after retrieving instructions to move the program counter to the next command for the next cycle. Thus, even a NOP will have an effective result of PC +=4 (when instructions are 4 bytes long). Wikipedia has more.

Also, since IMM16 can be negative, you can jump backward.

But, in my case, what is the value of offset / IMM16? The reference instruction set says:

"In command encoding, the offset specified by IMM16 is treated as the signed number of bytes relative to the instruction immediately following bne."

My interpretation of this is that the value of IMM16 is the "distance" to the next instruction address, but I have no idea if this is correct. The hex address for bne is 0x40010 and 0x40014 for br, so what does this mean that the value of IMM16 is 4? (which will cause the PC to jump through the address 0x40014 if rA! = rB?)

The value of IMM16 is the distance, but this is the distance (in bytes) to the command to which you want to go in the case rA != rB ! Thus, in this case, you want the immediate distance from the command following bne (because the distance is relative to the next instruction) to be in the place where you want to go ( loop ). In this case (if my calculations are correct) address(jump-target) - (address(bne-instruction) + 4) = 0x40008 - (0x40010 + 4) = -12 = ~12 + 1 = 0xfff4 (16-bit) .

Since you are worried about command encoding, be careful noting that the sigma function is applied immediately. I will take a reasonable assumption and suppose that it directly multiplies by 4, and then the encoding instruction will contain the number of instructions to jump minus one. This -12 bytes is likely to be encoded as a 16-bit significant value 0xfffc = -3 , since we want to return two commands back with bne and therefore 3 instructions back from the instruction following bne .

If this is true, then I still do not understand what the IMM16 value of br is, since there are no following instructions. Maybe zero when the shortcut "stops"? (or what bne will lead to if rA! = rB and PC ← PC + 4 + σ (IMM16)).

Remember that br can have a different encoding (it can be, for example, an absolute offset or it can be of a different size). I am not familiar enough with the instructions that you know, but I can give a general idea: you really do not use the address of the next instruction, instead you use the address of the current instruction + 4 (which, if followed, will be the address of this command).

+4
source

All Articles