0
votes

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
You're writing that assembler, so it's up to you. Automatically inserting jumps leads to trouble in weird cases, and is tricky all the time. If you're still interested in it I can write up some details about it.harold
It's a thorny issue, and sometimes the exact choice of jump instruction (or sequence of jump instructions) is only resolved by the linker.EOF
Should I just make it so to jump to any label it must be 64 lines ahead or 63 before? In that case would doing large jumps be almost impossible? The ISA has a JALR instruction which puts the PC+1 into a register and loads a new PC values from a register. The way this would work would be to load the address to a register (say this register is an assembler only register like on MIPS) and set PC to that. The PC+1 could just be sent to the zero register which would destroy the unwanted result.Anthony
Use jump-to-register for far jumps.Seva Alekseyev

1 Answers

1
votes

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: