[9] | 1 | ! oneC_sum() - One complement`s checksum Author: Kees J. Bot
|
---|
| 2 | ! 23 May 1998
|
---|
| 3 | ! See RFC 1071, "Computing the Internet checksum"
|
---|
| 4 | ! See also the C version of this code.
|
---|
| 5 |
|
---|
| 6 | .text
|
---|
| 7 |
|
---|
| 8 | .define _oneC_sum
|
---|
| 9 | .align 4
|
---|
| 10 | _oneC_sum:
|
---|
| 11 | push bp
|
---|
| 12 | mov bp, sp
|
---|
| 13 | push si
|
---|
| 14 | push di
|
---|
| 15 | mov ax, 4(bp) ! Checksum of previous block
|
---|
| 16 | mov si, 6(bp) ! Data to compute checksum over
|
---|
| 17 | mov di, 8(bp) ! Number of bytes
|
---|
| 18 |
|
---|
| 19 | xor dx, dx
|
---|
| 20 | xorb cl, cl
|
---|
| 21 | align: test si, #1 ! Is the data aligned?
|
---|
| 22 | jz aligned
|
---|
| 23 | test di, di
|
---|
| 24 | jz 0f
|
---|
| 25 | movb dh, (si) ! First unaligned byte in high half of
|
---|
| 26 | dec di ! the dx register, i.e. rotate 8 bits
|
---|
| 27 | 0: inc si
|
---|
| 28 | movb cl, #8 ! Number of bits "rotated"
|
---|
| 29 | ror ax, cl ! Rotate the checksum likewise
|
---|
| 30 | aligned:add ax, dx ! Summate the unaligned byte
|
---|
| 31 | adc ax, #0 ! Add carry back in for one`s complement
|
---|
| 32 |
|
---|
| 33 | jmp add6test
|
---|
| 34 | .align 4
|
---|
| 35 | add6: add ax, (si) ! Six times unrolled loop, see below
|
---|
| 36 | adc ax, 2(si)
|
---|
| 37 | adc ax, 4(si)
|
---|
| 38 | adc ax, 6(si)
|
---|
| 39 | adc ax, 8(si)
|
---|
| 40 | adc ax, 10(si)
|
---|
| 41 | adc ax, #0
|
---|
| 42 | add si, #12
|
---|
| 43 | add6test:
|
---|
| 44 | sub di, #12
|
---|
| 45 | jae add6
|
---|
| 46 | add di, #12
|
---|
| 47 |
|
---|
| 48 | jmp add1test
|
---|
| 49 | .align 4
|
---|
| 50 | add1: add ax, (si) ! while ((di -= 2) >= 0)
|
---|
| 51 | adc ax, #0 ! ax += *si++;
|
---|
| 52 | add si, #2 ! di += 2;
|
---|
| 53 | add1test:
|
---|
| 54 | sub di, #2
|
---|
| 55 | jae add1
|
---|
| 56 | add di, #2
|
---|
| 57 |
|
---|
| 58 | jz done ! Is there an extra byte?
|
---|
| 59 | movb dl, (si) ! Load extra byte in word
|
---|
| 60 | xorb dh, dh
|
---|
| 61 | add ax, dx ! Add in the last bits
|
---|
| 62 | adc ax, #0
|
---|
| 63 | done:
|
---|
| 64 | rol ax, cl ! Undo the rotation at the beginning
|
---|
| 65 | pop di
|
---|
| 66 | pop si
|
---|
| 67 | pop bp
|
---|
| 68 | ret
|
---|