tea_block:
          keyinfo    db 8 dup(0)
          ciphertext dw 0
          plaintext  dw 0         	   
          csize      dw 0
          y          dd 0
          z          dd 0
          sum        dd 0
          n          dw 0
tea_endblock equ $ - tea_block

tea_encipher proc far
; input : ds:bx = plaintext
;         es:dx = cipheredtext
;         cx    = size
; output: es:dx[di] = ciphertext
;         ax = 1 = ok, else error

        xor  si, si
        xor  di, di
;	mov  bx, word ptr cs:[plaintext]
;        mov  ax, word ptr cs:[plaintext+2]
;        mov  ds, ax
        ; ds:bx[si] = plaintext         
; while n--<0 {
tea_en_:


        loop tea_en_
; }





	;; begin encryption
encipher_rounds:
	;; compute new y
	mov	ax,[bp-8]	; low word z
	mov	bx,[bp-6]	; high word z
	mov	cx,ax		; copy to the rest of the registers
	mov	dx,bx
	mov	si,ax
	mov	di,bx
	;; (z<<4) ^ (z>>5)
	shl	ax,#1		; shift left once
	rcl	bx,#1
	shl	ax,#1		; shift twice
	rcl	bx,#1
	shl	ax,#1		; shift three times
	rcl	bx,#1
	shl	ax,#1		; shift four times
	rcl	bx,#1
	shr	dx,#1		; shift right once
	rcr	cx,#1
	shr	dx,#1		; shift right twice
	rcr	cx,#1
	shr	dx,#1		; shift right three times
	rcr	cx,#1
	shr	dx,#1		; shift right four times
	rcr	cx,#1
	shr	dx,#1		; shift right five times
	rcr	cx,#1
	xor	ax,cx		; combine
	xor	dx,bx		; dx:ax has result
	xor	si,[bp-12]	; combine to sum
	xor	di,[bp-10]
	add	ax,si		; add them together
	adc	dx,di
	mov	bx,[bp-12]	; get low word of sum (all we need for this)
	and	bx,#3		; get low two bits (modulo 4)
	shl	bx,#1		; convert to dword offset
	shl	bx,#1
	add	bx,[bp+8]	; add to base address of key info
	add	ax,[bx]		; low word of key
	adc	dx,[bx+2]	; high word of key
	add	[bp-4],ax	; add back to y
	adc	[bp-2],dx
	;; update sum
	mov	ax,#0x79b9	; low word of delta
	add	[bp-12],ax
	mov	ax,#0x9e37	; high word of delta
	adc	[bp-10],ax
	;; compute new z
	mov	ax,[bp-4]	; low word of y
	mov	bx,[bp-2]	; high word of y
	mov	cx,ax		; copy to the rest of the registers
	mov	dx,bx
	mov	si,ax
	mov	di,bx
	;; (y<<4) ^ (y>>5)
	shl	ax,#1		; shift left once
	rcl	bx,#1
	shl	ax,#1		; shift twice
	rcl	bx,#1
	shl	ax,#1		; shift three times
	rcl	bx,#1
	shl	ax,#1		; shift four times
	rcl	bx,#1
	shr	dx,#1		; shift right once
	rcr	cx,#1
	shr	dx,#1		; shift right twice
	rcr	cx,#1
	shr	dx,#1		; shift right three times
	rcr	cx,#1
	shr	dx,#1		; shift right four times
	rcr	cx,#1
	shr	dx,#1		; shift right five times
	rcr	cx,#1
	xor	ax,cx		; combine
	xor	dx,bx		; dx:ax has result
	xor	si,[bp-12]	; combine to sum
	xor	di,[bp-10]
	add	ax,si		; add them together
	adc	dx,di
	mov	bx,[bp-12]	; get sum low word
	mov	cx,[bp-10]	; get sum high word
	shr	cx,#1		; shift right once
	rcr	bx,#1
	shr	cx,#1		; shift right twice
	rcr	bx,#1
	shr	cx,#1		; shift right three times
	rcr	bx,#1
	shr	cx,#1		; shift right four times
	rcr	bx,#1
	shr	cx,#1		; shift right five times
	rcr	bx,#1
	shr	cx,#1		; shift right six times
	rcr	bx,#1
	shr	cx,#1		; shift right seven times
	rcr	bx,#1
	shr	cx,#1		; shift right eight times
	rcr	bx,#1
	shr	cx,#1		; shift right nine times
	rcr	bx,#1
	shr	cx,#1		; shift right ten times
	rcr	bx,#1
	shr	cx,#1		; shift right eleven times
	rcr	bx,#1
	and	bx,#3
	shl	bx,#1		; convert to dword offset
	shl	bx,#1
	add	bx,[bp+8]	; add to base address of key
	add	ax,[bx]		; low word of key
	adc	dx,[bx+2]	; high word of key
	add	[bp-8],ax	; add back to z
	adc	[bp-6],dx
	dec	byte ptr [bp-14] ; decrement rounds counter
	jz	finish_encipher
	jmp	near encipher_rounds
finish_encipher:	
	mov	bx,[bp+6]	; get address of ciphertext storage
	mov	ax,[bp-4]	; y, low word
	mov	[bx],ax
	mov	ax,[bp-2]	; y, high word
	mov	[bx+2],ax
	mov	ax,[bp-8]	; z, low word
	mov	[bx+4],ax
	mov	ax,[bp-6]	; z, high word
	mov	[bx+6],ax
	pop	di
	pop	si
	add	sp,#14		; discard local vars
	pop	bp
	ret
tea_encipher endp

tea_decipher proc far

_xtea_decipher_asm:
	push	bp
	mov	bp,sp
	sub	sp,#14		; space for y, z, sum, and n
	push	si
	push	di
	;; bp+8 = pointer to key information
	;; bp+6 = pointer to plaintext to return to caller
	;; bp+4 = pointer to ciphertext
	;; bp+2 = return address from caller
	;; bp = pushed bp
	;; bp-2 = y high word
	;; bp-4 = y low word
	;; bp-6 = z high word
	;; bp-8 = z low word
	;; bp-10 = sum high word
	;; bp-12 = sum low word
	;; bp-14 = n
	;; bp-16 = pushed si
	;; bp-18 = pushed di
	mov	bx,[bp+4]	; get address of ciphertext
	mov	ax,[bx]		; low word of first dword of ciphertext
	mov	[bp-4],ax
	mov	ax,[bx+2]	; high word
	mov	[bp-2],ax
	mov	ax,[bx+4]	; second dword of ciphertext (low)
	mov	[bp-8],ax
	mov	ax,[bx+6]	; (high)
	mov	[bp-6],ax
	mov	ax,#0x3720	; low word of initial sum
	mov	[bp-12],ax
	mov	ax,#0xc6ef
	mov	[bp-10],ax
	mov	byte ptr [bp-14],#32 ; set n (just 8 bits), # rounds
	;; begin decryption
decipher_rounds:	
	mov	ax,[bp-4]	; low word of y
	mov	bx,[bp-2]	; high word of y
	mov	cx,ax		; copy to the rest of the registers
	mov	dx,bx
	mov	si,ax
	mov	di,bx
	;; (y<<4) ^ (y>>5)
	shl	ax,#1		; shift left once
	rcl	bx,#1
	shl	ax,#1		; shift twice
	rcl	bx,#1
	shl	ax,#1		; shift three times
	rcl	bx,#1
	shl	ax,#1		; shift four times
	rcl	bx,#1
	shr	dx,#1		; shift right once
	rcr	cx,#1
	shr	dx,#1		; shift right twice
	rcr	cx,#1
	shr	dx,#1		; shift right three times
	rcr	cx,#1
	shr	dx,#1		; shift right four times
	rcr	cx,#1
	shr	dx,#1		; shift right five times
	rcr	cx,#1
	xor	ax,cx		; combine
	xor	dx,bx		; dx:ax has result
	xor	si,[bp-12]	; combine to sum
	xor	di,[bp-10]
	add	ax,si		; add them together
	adc	dx,di
	mov	bx,[bp-12]	; get sum low word
	mov	cx,[bp-10]	; get sum high word
	shr	cx,#1		; shift right once
	rcr	bx,#1
	shr	cx,#1		; shift right twice
	rcr	bx,#1
	shr	cx,#1		; shift right three times
	rcr	bx,#1
	shr	cx,#1		; shift right four times
	rcr	bx,#1
	shr	cx,#1		; shift right five times
	rcr	bx,#1
	shr	cx,#1		; shift right six times
	rcr	bx,#1
	shr	cx,#1		; shift right seven times
	rcr	bx,#1
	shr	cx,#1		; shift right eight times
	rcr	bx,#1
	shr	cx,#1		; shift right nine times
	rcr	bx,#1
	shr	cx,#1		; shift right ten times
	rcr	bx,#1
	shr	cx,#1		; shift right eleven times
	rcr	bx,#1
	and	bx,#3
	shl	bx,#1		; convert to dword offset
	shl	bx,#1
	add	bx,[bp+8]	; add to base address of key
	add	ax,[bx]		; low word of key
	adc	dx,[bx+2]	; high word of key
	sub	[bp-8],ax	; subtract from z
	sbb	[bp-6],dx
	;; update sum
	mov	ax,#0x79b9	; low word of delta
	sub	[bp-12],ax
	mov	ax,#0x9e37	; high word of delta
	sbb	[bp-10],ax
	;; compute new y
	mov	ax,[bp-8]	; low word z
	mov	bx,[bp-6]	; high word z
	mov	cx,ax		; copy to the rest of the registers
	mov	dx,bx
	mov	si,ax
	mov	di,bx
	;; (z<<4) ^ (z>>5)
	shl	ax,#1		; shift left once
	rcl	bx,#1
	shl	ax,#1		; shift twice
	rcl	bx,#1
	shl	ax,#1		; shift three times
	rcl	bx,#1
	shl	ax,#1		; shift four times
	rcl	bx,#1
	shr	dx,#1		; shift right once
	rcr	cx,#1
	shr	dx,#1		; shift right twice
	rcr	cx,#1
	shr	dx,#1		; shift right three times
	rcr	cx,#1
	shr	dx,#1		; shift right four times
	rcr	cx,#1
	shr	dx,#1		; shift right five times
	rcr	cx,#1
	xor	ax,cx		; combine
	xor	dx,bx		; dx:ax has result
	xor	si,[bp-12]	; combine to sum
	xor	di,[bp-10]
	add	ax,si		; add them together
	adc	dx,di
	mov	bx,[bp-12]	; get low word of sum (all we need for this)
	and	bx,#3		; get low two bits (modulo 4)
	shl	bx,#1		; convert to dword offset
	shl	bx,#1
	add	bx,[bp+8]	; add to base address of key info
	add	ax,[bx]		; low word of key
	adc	dx,[bx+2]	; high word of key
	sub	[bp-4],ax	; subtract from y
	sbb	[bp-2],dx
	dec	byte ptr [bp-14] ; decrement rounds counter
	jz	finish_decipher
	jmp	near decipher_rounds
finish_decipher:	
	mov	bx,[bp+6]	; get address of ciphertext storage
	mov	ax,[bp-4]	; y, low word
	mov	[bx],ax
	mov	ax,[bp-2]	; y, high word
	mov	[bx+2],ax
	mov	ax,[bp-8]	; z, low word
	mov	[bx+4],ax
	mov	ax,[bp-6]	; z, high word
	mov	[bx+6],ax
	pop	di
	pop	si
	add	sp,#14		; discard local vars
	pop	bp
	ret
tea_decipher endp
