PASM sign extend (+ a few other questions)?
I've written a fairly long PASM code that reads through a bytecode script and executes routines according to the script. For several of routines it needs to read a portion of the script as a 9-bit 2's compliment signed integer. Though it reads the integer correctly if it is positive, if a negative number is given (for example %111110110) it will read it as a positive number because the size of the register extends it to %000000000000000000000000111110110 and doesn't see it as negative. I cannot think of an efficient way to "sign extend" the register to read it as a negative, without using the "neg" command which will change the intended integer.
Also, there is another part of the code that only works when I change a portion of SPIN code running in another cog, completely unrelated to this PASM code entirely, from "variable++" to "variable +=2". Though this variable is linked to the PASM script, the portion of SPIN code it is in is bypassed and not even run. I'll post the code if needed, but it's not very neat and I haven't finished commenting it yet. Plus, the fact this function isn't even used yet still affects the code makes me think it doesn't have to do with the PASM code itself.
And one more question, since I am a bit of a n00b to PASM specifically, when you shift bits, do they get shifted into the next (or previous) corresponding register, or are they just removed?
Thanks
Also, there is another part of the code that only works when I change a portion of SPIN code running in another cog, completely unrelated to this PASM code entirely, from "variable++" to "variable +=2". Though this variable is linked to the PASM script, the portion of SPIN code it is in is bypassed and not even run. I'll post the code if needed, but it's not very neat and I haven't finished commenting it yet. Plus, the fact this function isn't even used yet still affects the code makes me think it doesn't have to do with the PASM code itself.
And one more question, since I am a bit of a n00b to PASM specifically, when you shift bits, do they get shifted into the next (or previous) corresponding register, or are they just removed?
Thanks

Comments
A shift instruction will put the first bit in the carry bit if you specify WC, and throw away all the other bits that are shifted out.
The ++ operator does the same function as +=1. If you want to do +=2 you would need to execute ++ twice. So if your code is only doing it once that might be the problem. If your code is doing ++ twice, then you may have a problem with scribbling into memory. This can be cause by using a bad pointer, or a stack that is too small.
SHL val, #23 SAR val, #23But there are many ways to do this, MUXNZ will do nicely for instance:TEST val, #$100 wz ' test sign bit to Z flag MUXNZ val, HFFFFFE00 ' mux into other top bits HFFFFFE00 long $FFFFFE00Or the very pedestrian test and subtract:TEST val, #$100 wz ' test sign bit to Z flag if_nz SUB val, H200 ' subtract 2^9 H200 long $200(Hopefully I've not made any mistakes, but I'd double check just in case)
outputs 6, while
outputs 5. But
will output 6.
-Phil
Microcontrolled, please post your code.
LC3.spin
True, we don't know whether it was being used in an expression; but regardless, in the general case x++ is still not the same as x += 1, whereas ++x is.
-Phil
You've got:
I think you mean:
-Phil
EDIT: I'm confused by the first few instructions in your PASM code. Shouldn't the PC be set to the value of PAR instead of the contents of the long pointer to by PAR. That is, the PC should be @fal, and not long[@fal] correct?
mainLoop rdword cInst, PC 'Read the current instruction from RAM add PC, [COLOR="#FF0000"]#2[/COLOR] 'Increment the program counterAs we have word granularity we need an increment of two. The test program doesn't show this but you'll notice when you increment a register which will then happen twice.The existence of this extra long determines whether the branch in your test program is taken or not. This is down to a faulty condition check in the branch subroutine. At this point DR (holding the exec condition) is already destroyed (*2+base) IOW the outcome is highly code position dependent.
'Isolate DR mov DR, cInst 'Obtain DR from the instruction mov addSub, opCode 'Make addSub opCode in format xxxx shl addSub, #12 'Turn xxxx to xxxx000000000000 sub DR, addSub 'Clear this making 0000xxx000000000 shr DR, #9 'Shift to make it 0000xxx (END OF PROCESS)May I suggest (this also applies to the remaining parameters):'Isolate DR mov DR, cInst shr DR, #9 and DR, #%111