
.386p
fastmem_TEXT    segment para public use16 'CODE'
		assume	cs:_TEXT
; dest = [ES DI], value = [AL], bytes = [CX]
memset8_	proc    far
                jcxz thatsIt_8

                cld

                ; copy al to whole eax
                mov ah,al
                mov dx,ax
                shl eax,16
                mov ax,dx

                test di,1               ; is dest a word-aligned address?
                jz aligned16_8          ; Yes
                stosb
                dec cx
                jz thatsIt_8

aligned16_8:    test di,2               ; is dest a dword-aligned address?
                jz aligned32_8          ; Yes
                stosb
                dec cx
                jz thatsIt_8
                stosb
                dec cx

aligned32_8:    mov dx,cx
                shr cx,2                ; dwords = bytes/4
                rep stosd
                mov cx,dx               ; now finish the rest
                and cx,3
                rep stosb

thatsIt_8:      ret
memset8_  	endp


; dest = [ES DI], value = [AX], words = [CX]
memset16_       proc far
                jcxz thatsIt_16

                cld

                ; copy ax to high-word of eax
                mov bx,ax
                shl eax,16
                mov ax,bx

                mov dx,di              ; remember original di

                test di,1              ; is dest a word-aligned address?
                jz aligned16_16        ; Yes
                stosb
                ror eax,8
                dec cx
                jz lastByte_16

aligned16_16:   test di,2              ; is dest a dword-aligned address?
                jz aligned32_16        ; Yes
                stosw
                dec cx

aligned32_16:   shr cx,1
                rep stosd
                jnc lastByte_16
                stosw

lastByte_16:    test dx,1              ; do we need the last byte?
                jz thatsIt_16          ; No
                stosb

thatsIt_16:     ret
memset16_       endp



; dest = [es:di], value = [dx:ax], dwords = [cx]
memset32_       proc far
                jcxz thatsIt_32

                cld

                ;move dx:ax to eax
                xchg dx,ax
                shl eax,16
                mov ax,dx

                mov dx,di              ; remember di

                test di,1              ; is dest word-aligned?
                jz aligned16_32        ; Yes
                stosb
                ror eax,8
                dec cx
                jz doRest_32
                test di,2;is di dword-aligned?
                jz mainCopy_32
                stosw
                ror eax,16
                jmp mainCopy_32

aligned16_32:   test di,2              ; is di dword-aligned?
                jz mainCopy_32         ; yes
                stosw
                ror eax,16
                dec cx
                jz doRest_32

mainCopy_32:    rep stosd

doRest_32:	test dx,2              ; was org. di dword-aligned?
                jz lastByte_32         ; Yes
                stosw
                ror eax,16
lastByte_32:    test dx,1              ; was org. di word-aligned?
                jz thatsIt_32          ; yes, we are finished
                stosb                  ; store last byte

thatsIt_32:     ret
memset32_       endp




; dest = [es:di], src = [dx si], bytes= [cx]
memcpy8_        proc far
                push ds
                mov ds,dx
                jcxz thatsIt_memcpy

                cld

                test si,1
                jnz si_word_unaligned
                test di,1
                jz sidi_word_aligned
                jmp maincopy_memcpy    ; si nonalign, di align - give up
si_word_unaligned:
                test di,1
                jz maincopy_memcpy

sidi_word_unaligned:
                movsb
                dec cx
                jz thatsIt_memcpy

sidi_word_aligned:
                test di,2
                jz maincopy_memcpy
                test si,2
                jz maincopy_memcpy
                ;neither di and di is dword-aligned - fix it
                movsw
                sub cx,2
                jz thatsIt_memcpy

maincopy_memcpy:
                mov dx,cx
                shr cx,2
                rep movsd
                mov cx,dx
                and cx,3
                rep movsb

thatsIt_memcpy:
                pop ds
                ret
memcpy8_        endp

		public memset8_
		public memset16
                public memset32_
                public memcpy8_

fastmem_TEXT	ends

		end
