diff --git a/md5.asm b/md5.asm index bfe2f74..ec34c77 100644 --- a/md5.asm +++ b/md5.asm @@ -26,7 +26,7 @@ a_ gequ 12 ; elements of state b gequ 16 c gequ 20 d gequ 24 -;unused gequ 28 +temp2 gequ 28 ;unused gequ 32 temp gequ 36 h0 gequ 40 diff --git a/md5.macros b/md5.macros index 36aaf66..8c75ce6 100644 --- a/md5.macros +++ b/md5.macros @@ -32,6 +32,24 @@ .end mend +* Expects the contents of &loc+2 to be loaded in A already + macro + ROTR4CONT &loc,&n + aif &n>16,.dorotl + lcla &i +&i seta &n +.rotrloop + lsr a ;to set carry + ror &loc + ror &loc+2 +&i seta &i-1 + aif &i>0,.rotrloop + ago .end +.dorotl + ROTL4 &loc,32-&n +.end + mend + * Left-rotate 32-bit value in &loc (DP or 16-bit address) by &n positions macro ROTL4 &loc,&n @@ -51,6 +69,24 @@ .end2 mend +* Expects the contents of &loc to be loaded in A already + macro + ROTL4CONT &loc,&n + aif &n>16,.dorotr2 + lcla &i +&i seta &n +.rotlloop2 + asl a ;to set carry + rol &loc+2 + rol &loc +&i seta &i-1 + aif &i>0,.rotlloop2 + ago .end2 +.dorotr2 + ROTR4 &loc,32-&n +.end2 + mend + * &to := &from ROTR4 &n macro ROTR4MOVE &to,&from,&n @@ -95,6 +131,75 @@ .end4 mend +* Left-rotate with various optimizations applied + macro + ROTL4AUTO &loc,&n + aif &n>5,.skip1 + ROTL4 &loc,&n + mexit +.skip1 + aif &n>7,.skip2 + ldx &loc+1 + lda &loc+3 + and #$00FF + sta temp2 + lda &loc-1 + and #$FF00 + ora temp2 + sta &loc + stx &loc+2 + txa + ROTR4CONT &loc,8-&n + mexit +.skip2 + aif &n>11,.skip3 + ldx &loc+1 + lda &loc+3 + and #$00FF + sta temp2 + lda &loc-1 + and #$FF00 + ora temp2 + sta &loc + stx &loc+2 + ROTL4CONT &loc,&n-8 + mexit +.skip3 + aif &n>15,.skip4 + lda &loc + ldy &loc+2 + sta &loc+2 + sty &loc + ROTR4CONT &loc,16-&n + mexit +.skip4 + aif &n>16,.skip5 + lda &loc + ldy &loc+2 + sta &loc+2 + sty &loc + mexit +.skip5 + aif &n>21,.skip6 + lda &loc+2 + ldy &loc + sta &loc + sty &loc+2 + ROTL4CONT &loc,&n-16 + mexit +.skip6 + ldx &loc+1 + lda &loc+3 + and #$00FF + sta temp2 + lda &loc-1 + and #$FF00 + ora temp2 + sta &loc+2 + stx &loc + ROTR4CONT &loc,24-&n + mend + * This makes a function wrapper that is callable from C, * taking a pointer to the context structure as its argument. @@ -219,7 +324,7 @@ iny sty idx - ROTL4 temp,&shift + ROTL4AUTO temp,&shift clc lda &b