Assembler labels with limited jump range

2019-09-02 20:28发布

问题:

I am writing an assembler for a simple RISC processor which has a very small immiediate for jumps (7 bit signed). All jumps are calculated by:

PC = PC + IMMEDIATE + 1

where PC is the program counter which points to the next instruction.

If you need to jump more than 64 lines ahead you need chained jumps as follows:

JMP, 0x3F

//64 lines ahead

JMP, 0x5;

This would effectively jump 70 lines ahead of the current instruction.

My question comes in when we have labels:

JMP, label

//more than 64 lines ahead

label:

How would the assembler generate code for this? would you need two labels or would the assembler put in two jumps for you? If it puts in two jumps how does it know if an instruction is not 64 lines ahead?

回答1:

Conditional jumps can not lead further than 127 bytes forward or 128 bytes backward. Unconditional jumps can jump farer. I guess you tried it with a conditional jump. If you want to jump conditionally to a place farer than 127 bytes, write a unconditional jump to that place and insert a conditional jump before that will jump over the other jump instruction if the condition isn't fulfilled. For example this code:

je label

//more than 127 bytes ahead

label:

Could be replace by this code:

jne omit
jmp label
omit:
//more than 127 bytes ahead
label: