help with PASM loop rollup
ericball
Posts: 774
Here is a challenge - any suggestions on how to roll up the following code to make it smaller?
ShiftRows MOV in, istate CALL #SubByte MOV ostate, SBvar CALL #SubByte0 SHL SBvar, #8 MOV ostate+3, SBvar CALL #SubByte0 SHL SBvar, #16 MOV ostate+2, SBvar CALL #SubByte0 SHL SBvar, #24 MOV ostate+1, SBvar MOV in, istate+1 CALL #SubByte OR ostate+1, SBvar CALL #SubByte0 SHL SBvar, #8 OR ostate, SBvar CALL #SubByte0 SHL SBvar, #16 OR ostate+3, SBvar CALL #SubByte0 SHL SBvar, #24 OR ostate+2, SBvar MOV in, istate+2 CALL #SubByte OR ostate+2, SBvar CALL #SubByte0 SHL SBvar, #8 OR ostate+1, SBvar CALL #SubByte0 SHL SBvar, #16 OR ostate, SBvar CALL #SubByte0 SHL SBvar, #24 OR ostate+3, SBvar MOV in, istate+3 CALL #SubByte OR ostate+3, SBvar CALL #SubByte0 SHL SBvar, #8 OR ostate+2, SBvar CALL #SubByte0 SHL SBvar, #16 OR ostate+1, SBvar CALL #SubByte0 SHL SBvar, #24 OR ostate, SBvar <other stuff> SubByte0 SHR in, #8 SubByte MOV SBvar, in ' SBvar = Sboxptr[in] AND SBvar, #$FF ADD SBvar, SBoxptr RDBYTE SBvar, SBvar SubByte0_ret SubByte_ret RETObviously there are many pieces not shown here. The important criteria is to make ShiftRows require the least number of longs overall (including any temporary storage & initialization, note: istate does not need to be preserved); speed of execution is not important. Normally this could be done in a loop but because the ostate indexes wrap around, I don't think that's possible.
Comments
Good catch. I meant to CALL #SubByte0 to preshift the input so the second byte gets done first and the LSByte done last. And I completely forgot to include the ROL after the CALL to shift the byte into position.
Thanks to "eagle eye" kuroneko for spotting that the output of my revised code still didn't work correctly. I have now reversed the order ostate is updated so the bytes are shifted left rather than right. (Note: the output ends up in istate, which is okay.) Now I need to extend the courtesy and look (and understand) at his code.