Comparison Instruction Number Optimization (PDP-11)

For the PDP-11, how can I change the following assembly snippet so that it only does two commands, but does the same job as those four?

tst r0
blt label
cmp r0, #75
bgt label

      

+2


a source to share


3 answers


I've never worked with a PDP-11, but I have some experience with how testing and branching works on x86 systems and it looks like it might be similar.

In the x86 instruction set, the "test" instruction is equivalent to comparison with 0; the "less" flag is set if the value is less than 0, and so on. I'm going to assume that # 75 means a numeric literal in hexadecimal format - 0x75.

If my assumptions are correct, the code you have is doing two signed comparisons:



  • Is the (signed) r0 value less than 0?
  • Is the (signed) r0 value greater than 0x75?

If you treat it as an unsigned value instead , then - assuming PDP-11 systems use layer 2 appended encoding - values ​​that were negative become values ​​greater than or equal to 0x8000 (since the PDP-11 is 16- discharge system). Thus, if you do an unsigned comparison, checking for 0x75 will also take care of negative values; the smallest possible value becomes 0, which is acceptable for the tests here.

I'm not sure if unsigned comparison on PDP-11 is a different comparison comparison code or a different flag, but I'm sure you can understand this part. :-)

+6


a source


It's probably too late for your homework, so here's the answer:

cmp r0, # 75
bcc label; branch if r0 contains 75..65535, ie 75..32767, -32768 ..- 1


bcc has a synonym for assembly bhis (branch if HIgher or Same unsigned).

+1


a source


Assuming you want to jump to the same label, if the result of the comparison is greater or less, then you can get it up to two instructions using the branch-if-not-equal statement (because a! = B when a <b & a> b) and one more instruction.

Hint: This is not a test statement.

0


a source







All Articles