user1131435
user1131435

Reputation:

Are JFE and JNE imperative to assembly, or can I remove them?

I'm laying out an 8-bit processor architecture with 4-bit instructions for fun, and am encountering some limitations with a 4-bit instruction.

I'd like to include SHR (shift right) and SHL (shift left) instructions, but I don't seem to have room for them. I currently have JGE (jump if greater or equal), JLE (jump if less or equal), JFE (jump if equal), and JNE (jump if not equal), and I'm wondering if all four of these are necessary.

Are all four of these jump conditions necessary for clean code, or can I safely drop JFE and JNE for SHR and SHL?

Edit: I do have JMP, and it is not absolute.

Upvotes: 1

Views: 256

Answers (3)

In addition to Carl Norum's answer, if you have support for an unconditional jump instruction (JMP), then once you have JFE you don't need its complement, JNE, since you can code that as a JFE to a JMP. Similarly, if you have JG, JFE, and JMP, you don't need JLE.

For shift, if your instruction takes an argument and that argument allows for values greater than your word size, you could use the same instruction for shift-left and shift-right and let the argument value determine if it's one or the other.

Upvotes: 4

Peter de Rivaz
Peter de Rivaz

Reputation: 33509

Here is one way of dropping JFE and JNE.

This code will branch to different locations depending on whether the test is equal (branches to definitely_equal) or not (branches to definitely_not_equal).

   JGE maybe_equal
   JLE definitely_not_equal
maybe_equal:
   JLE definitely_equal
   JGE definitely_not_equal

I'll leave it up to you to decide whether this counts as clean code or not...

Upvotes: 3

Carl Norum
Carl Norum

Reputation: 225092

If by "clean", you mean "easy to read", the more instructions you have, the better. Well, to a point, anyway.

In practice, you only need a single conditional jump instruction.

Brainfuck has only 8 'instructions', for example. It has two jumps - one forward and one back, but if your processor uses an absolute jump, those could be the same.

Are you writing an assembler for your processor? If so, you could add macro-instructions that manage the different jump forms and translate them into whatever low-level operations are actually supported by your chip.

Upvotes: 3

Related Questions