⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 util.asm

📁 8051 VARIOUS UTILITY FUNCTIONSUTIL_ADCAD - Add Acc to DPTR, sets CYUTIL_ADCBAD - Add B/A to DPTR, se
💻 ASM
📖 第 1 页 / 共 4 页
字号:
;  Affected:
;	PSW.CY, PSW.Z, PSW.P, Acc, B, R2, R3, R4, R5
;
;  Stack:
;	5 Bytes, Not Including Space Used By Called Routines
;
;  Comments:
;	Swiped From A Now Defunct 'C' Compiler
;
UTIL_DIV	proc
		push	0
		push	1
		mov	a,r4
		xrl	a,r2
		push	acc
		call	NEGEM
		call	DIVIDE
		pop	acc
		jnb	acc.7,l?p1
		clr	a
		clr	c
		subb	a,r3
		mov	r3,a
		clr	a
		subb	a,r2
		mov	r2,a
l?p1		pop	1
		pop	0
		ret				; done!
		endproc
;
;****************************************************************************
;
;  Description:
;	Does Sign Fixup For Divide
;
;  Entry Requirements:
;	Divisor In R4/5
;	Dividend In R2/3
;
;  On Exit:
;	Divisor In R4/5, Sign Normalized
;	Dividend In R2/3, Sign Normalized
;
;  Affected:
;	PSW.CY, PSW.Z, PSW.P, Acc, R2, R3, R4, R5
;
;  Stack:
;	0 Bytes, Not Including Space Used By Called Routines
;
;  Comments:
;	Swiped From A Now Defunct 'C' Compiler
;
NEGEM		proc
		mov	a,r2
		jnb	acc.7,l?p1
		clr	a
		clr	c
		subb	a,r3
		mov	r3,a
		clr	a
		subb	a,r2
		mov	r2,a
;
l?p1		mov	a,r4
		jnb	acc.7,l?p2
		clr	a
		clr	c
		subb	a,r5
		mov	r5,a
		clr	a
		subb	a,r4
		mov	r4,a
l?p2		ret
		endproc
;
;****************************************************************************
;
;  Description:
;	Unsigned Divide Of R2/3 By R4/5
;
;  Entry Requirements:
;	Divisor In R4/5
;	Dividend In R2/3
;
;  On Exit:
;	Quotient In R2/3
;	Remainder In R4/5
;
;  Affected:
;	PSW.CY, PSW.Z, PSW.P, Acc, B, R0, R1, R2, R3, R4, R5
;
;  Stack:
;	X Bytes, Not Including Space Used By Called Routines
;
;  Comments:
;	Swiped From A Now Defunct 'C' Compiler
;
DIVIDE		proc
		clr	a
		mov	b,a			; initialize count
		mov	r0,a			; zero quotient
		mov	r1,a
		mov	a,r2			; check for zero dividend
		orl	a,r3
		jz	l?p8
		mov	a,r4			; check for zero divisor
		orl	a,r5
		jnz	l?p3
		ret
;
l?p1		mov	a,r2
		clr	c
		subb	a,r4			; is divisor greater than dividend yet
		jc	l?p4			; yes, go no further
		jnz	l?p2
		mov	a,r3
		subb	a,r5
		jc	l?p4
;
l?p2		mov	a,r5			; shift divisor up one bit
		clr	c
		rlc	a
		mov	r5,a
		mov	a,r4
		rlc	a
		mov	r4,a
;
l?p3		inc	b			; increment count
		mov	a,r4			; check for safe to shift some more
		jnb	acc.7,l?p1		; loop if top bit clear
;
l?p4		mov	a,r2
		clr	c
		subb	a,r4			; is divisor greater than dividend
		jc	l?p5	
		jnz	l?p6
		mov	a,r3
		subb	a,r5
		jnc	l?p6
;
l?p5		clr	c
		sjmp	l?p7
;
l?p6		clr	c			; subtract divisor from dividend
		mov	a,r3
		subb	a,r5
		mov	r3,a
		mov	a,r2
		subb	a,r4
		mov	r2,a
		setb	c			; now set bit for quotient
;
l?p7		mov	a,r1
		rlc	a
		mov	r1,a
		mov	a,r0
		rlc	a
		mov	r0,a

		mov	a,r4			; shift divisor down
		clr	c
		rrc	a
		mov	r4,a
		mov	a,r5
		rrc	a
		mov	r5,a
		djnz	b,l?p4			; and continue with the rest
;
l?p8		mov	5,r3
		mov	4,r2
		mov	2,r0
		mov	3,r1
		ret
		endproc
;
;****************************************************************************
;
;  Description:
;	Copy String From External Memory To Internal Memory
;
;  Entry Requirements:
;	DPTR Points To Source In External Memory
;	R1 Points To Destination In Internal Memory
;	R0 Contains Number Of Bytes To Copy
;
;  On Exit:
;	DPTR = Points To End Of Source + 1
;	R1 Points To End Of Destination + 1
;	R0 = 0
;
;  Affected:
;	PSW.Z, PSW.P, Acc, R0, R1, DPTR
;
;  Stack:
;	0 Bytes, Not Including Space Used By Called Routines
;
;  Comments:
;	None
;
UTIL_COPYXTOI	proc
l?p1		movx	a,@dptr			; Get Source Byte
		mov	@r1,a			; Store To Destination
		inc	dptr			; Next Source Byte
		inc	r1			; Next Destination Byte
		djnz	r0,l?p1			; Do For R0 Bytes
		ret				; Return To Caller
		endproc
;
;****************************************************************************
;
;  Description:
;	Copy Bytes From Internal Memory To External Memory
;
;  Entry Requirements:
;	DPTR Points To Destination In External Memory
;	R1 Points To Source In Internal Memory
;	R0 Contains Number Of Bytes To Copy
;
;  On Exit:
;	DPTR = Points To End Of Destination + 1
;	R1 Points To End Of Source + 1
;	R0 = 0
;
;  Affected:
;	PSW.Z, PSW.P, Acc, R0, R1, DPTR
;
;  Stack:
;	0 Bytes, Not Including Space Used By Called Routines
;
;  Comments:
;	None
;
UTIL_COPYITOX	proc
l?p1		mov	a,@r1			; Get Source Byte
		movx	@dptr,a			; Store To Destination
		inc	r1			; Next Source Byte
		inc	dptr			; Next Destination Byte
		djnz	r0,l?p1			; Do For R0 Bytes
		ret				; Return To Caller
		endproc
;
;****************************************************************************
;
;  Description:
;	Copy Bytes From Code Memory To External Memory
;
;  Entry Requirements:
;	DPTR Points To Destination In External Memory
;	R6/7 Points To Source In Code Memory
;	R0/1 Contains The Length
;
;  On Exit:
;	DPTR = Points To End Of Destination + 1
;	R6/7 = Points To End Of Source + 1
;	R0/1 = 0
;
;  Affected:
;	PSW.Z, PSW.P, Acc, DPTR, R0, R1, R6, R7
;
;  Stack:
;	2 Bytes, Not Including Space Used By Called Routines
;
;  Comments:
;	None
;
UTIL_COPYCTODL	proc
		call	UTIL_DPTRR01		; DPTR=Len, R01=Src
		call	UTIL_DPTR2C		; 2's Complement
		call	UTIL_DPTRR01		; DPTR=Src, R01=Len
;
l?p1		call	UTIL_DPTRR67		; DPTR=Src, R67=Dest
		clr	a			; Clear For MOVC
		movc	a,@a+dptr		; Get Character
		inc	dptr			; Next Source Location
		call	UTIL_DPTRR67		; DPTR=Dest, R67=Src
		movx	@dptr,a			; Store It
		inc	dptr			; Next Destination Location
		call	UTIL_DPTRR01		; DPTR=Len, R01=Dest
		inc	dptr			; Increment Length
		mov	a,dph			; Get High
		orl	a,dpl			; OR In Low
		call	UTIL_DPTRR01		; DPTR=Dest, R01=Len
		jnz	l?p1			; Repeat Until 0
		ret				; Return To Caller
		endproc
;
;****************************************************************************
;
;  Description:
;	Copies ETX Terminated String From Code Memory To External Memory.
;	ETX Is Copied.
;
;  Entry Requirements:
;	DPTR Points To Destination In External Memory
;	R6/7 Points To Source In Code Memory
;
;  On Exit:
;	DPTR = Points To Source ETX + 1
;	R6/7 = Points To Destination ETX + 1
;
;  Affected:
;	PSW.Z, PSW.P, Acc, DPTR, R6, R7
;
;  Stack:
;	2 Bytes, Not Including Space Used By Called Routines
;
;  Comments:
;	None
;
UTIL_COPYCTODZ	proc
l?p1		call	UTIL_DPTRR67		; DPTR=Src, R67=Dest
		clr	a			; Clear For MOVC
		movc	a,@a+dptr		; Get Character
		inc	dptr			; Next Source Location
		call	UTIL_DPTRR67		; DPTR=Dest, R67=Src
		movx	@dptr,a			; Store It
		inc	dptr			; Next Destination Location
		cjne	a,#ETX,l?p1		; If ETX, Exit Copy
		ret				; Return To Caller
		endproc
;
;****************************************************************************
;
;  Description:
;	Copy Bytes From External Memory To External Memory
;
;  Entry Requirements:
;	DPTR Points To Destination In External Memory
;	R6/7 Points To Source In External Memory
;	R0/1 Contains The Length
;
;  On Exit:
;	DPTR = Points To End Of Destination + 1
;	R6/7 = Points To End Of Source + 1
;	R0/1 = 0
;
;  Affected:
;	PSW.Z, PSW.P, Acc, DPTR, R0, R1, R6, R7
;
;  Stack:
;	2 Bytes, Not Including Space Used By Called Routines
;
;  Comments:
;	None
;
UTIL_COPYDTODL	proc
		call	UTIL_DPTRR01		; DPTR=Len, R01=Src
		call	UTIL_DPTR2C		; 2's Complement
		call	UTIL_DPTRR01		; DPTR=Src, R01=Len
;
l?p1		call	UTIL_DPTRR67		; DPTR=Src, R67=Dest
		movx	a,@dptr			; Get Character
		inc	dptr			; Next Source Location
		call	UTIL_DPTRR67		; DPTR=Dest, R67=Src
		movx	@dptr,a			; Store It
		inc	dptr			; Next Destination Location
		call	UTIL_DPTRR01		; DPTR=Len, R01=Dest
		inc	dptr			; Increment Length
		mov	a,dph			; Get High
		orl	a,dpl			; OR In Low
		call	UTIL_DPTRR01		; DPTR=Dest, R01=Len
		jnz	l?p1			; Repeat Until 0
		ret				; Return To Caller
		endproc
;
;****************************************************************************
;
;  Description:
;	Copies ETX Terminated String From External Memory To External Memory.
;	ETX Is Copied.
;
;  Entry Requirements:
;	DPTR Points To Destination In External Memory
;	R6/7 Points To Source In Code Memory
;
;  On Exit:
;	DPTR = Points To Source ETX + 1
;	R6/7 = Points To Destination ETX + 1
;
;  Affected:
;	PSW.Z, PSW.P, Acc, DPTR, R6, R7
;
;  Stack:
;	2 Bytes, Not Including Space Used By Called Routines
;
;  Comments:
;	None
;
UTIL_COPYDTODZ	proc
l?p1		call	UTIL_DPTRR67		; DPTR=Src, R67=Dest
		movx	a,@dptr			; Get Character
		inc	dptr			; Next Source Location
		call	UTIL_DPTRR67		; DPTR=Dest, R67=Src
		movx	@dptr,a			; Store It
		inc	dptr			; Next Destination Location
		cjne	a,#ETX,l?p1		; If ETX, Exit Copy
		ret				; Return To Caller
		endproc
;
;****************************************************************************
;
;  Description:
;	Search Data Memory DPTR Points To Until A 0x00 Is Found, Or R0 Is 0,
;	And Replace With An ETX.  DPTR Points To Data, R0 Contains Maximum 
;	Length To Search
;
;  Entry Requirements:
;	DPTR Points To A 0 Terminated String In External Memory
;	R0 Contains Maximum Length To Search
;
;  On Exit:
;	R0 = 0
;
;  Affected:
;	PSW.Z, PSW.P, Acc, DPTR, R0
;
;  Stack:
;	2 Bytes, Not Including Space Used By Called Routines
;
;  Comments:
;	None
;
UTIL_PUT_ETX	proc
		push	dph			; Save DPH
		push	dpl			; Save DPL
;
l?p1		movx	a,@dptr			; Get Character
		jz	l?p2			; If 0, Replace And Exit
		inc	dptr			; Next Location
		djnz	r0,l?p1			; Repeat For R0 Bytes
;
l?p2		mov	a,#ETX			; ETX Character
		movx	@dptr,a			; Store It
;
		pop	dpl			; Recover DPL
		pop	dph			; Recover DPH
		ret				; Return To Caller
		endproc
;
;****************************************************************************
;
;  Description:
;	If An ETX Is Found, Return With DPTR Pointing To It, And CY == 0.  If 
;	No ETX Found, Return DPTR Pointing At One Byte Past End, And CY == 1.
;
;  Entry Requirements:
;	DPTR Points To External Memory
;	R0 Contains Maximum Length To Search
;
;  On Exit:
;	CY == 0 Indicates ETX Found, DPTR Points To ETX
;	CY == 1 Indicated ETX Not Found, DPTR = DPTR + R0
;
;  Affected:
;	PSW.CY, PSW.Z, PSW.P, Acc, DPTR, R0
;
;  Stack:
;	1 Bytes, Not Including Space Used By Called Routines
;
;  Comments:
;	None
;
UTIL_FIND_ETX	proc
		push	0			; Save R0
;
l?p1		movx	a,@dptr			; Get Character
		cjeq	a,#ETX,l?p2		; If ETX, Skip
		inc	dptr			; Next Location
		djnz	r0,l?p1			; Repeat Up To R0
;
		pop	0			; Recover R0
		setb	c			; ETX Not Found
		ret				; Return To Caller
;
l?p2		pop	0			; Recover R0
		clr	c			; ETX Found
		ret				; Return To Caller
		endproc
;
;****************************************************************************
;
;  Description:
;	Removes Trailing Spaces From A String In External Memory That Is
;	<= R0 In Length By Placing An ETX Character At The First Non-Space
;	Character From The End.
;
;  Entry Requirements:
;	DPTR Points To String In External Memory
;	R0 Contains Index Into String To Start Search From
;
;  On Exit:
;	DPTR Points To Location ETX Stored To
;	Acc = ETX
;
;  Affected:
;	PSW.Z, PSW.P, Acc, DPTR
;
;  Stack:
;	5 Bytes, Not Including Space Used By Called Routines
;
;  Comments:
;	If No Spaces Are Found, An ETX Is Stored To The First Position In
;	The String.
;
UTIL_TRIM	proc
		push	0			; Save R0
		push	dph			; Save DPH
		push	dpl			; Save DPL
;
		mov	a,r0			; Get Length To Acc
		dec	a			; Make 0 Based, Not 1
		call	UTIL_ADCAD		; Point To Last Byte
l?p1		movx	a,@dptr			; Get Character From Buffer
		cjne	a,#SPACE,l?p2		; If Not Space, Exit
		call	UTIL_DPTRDEC		; Next Location Back
		djnz	r0,l?p1			; Repeat R0 Times
;
		pop	dpl			; Recover Buffer Start Low
		pop	dph			; Recover Buffer Start High
		mov	a,#ETX			; ETX Character
		movx	@dptr,a			; Store Terminator
		pop	0			; Recover R0
		ret				; Return To Caller
;
;  Now Get The Modem Index Number From The Modem Buffer, And Append It
;
l?p2		inc	dptr			; 1st Char Past Non-Space
		mov	a,#ETX			; ETX Character
		movx	@dptr,a			; Store Terminator
;
		pop	dpl			; Recover DPL
		pop	dph			; Recover DPH
		pop	0			; Recover R0
		ret				; Return To Caller
		endproc
;
;****************************************************************************
;
;  Description:
;	DPTR Points To A String In External RAM To Determine The Length Of.  
;	The String Must Be 0 Terminated, And Less Than 256 Bytes In Length.  
;	The Length Is Returned In Acc.
;
;  Entry Requirements:
;	DPTR Points To An ETX Terminated String In External Memory
;
;  On Exit:
;	DPTR Points To ETX
;	Acc Has Length Of String
;
;  Affected:
;	PSW.CY, PSW.Z, PSW.P, Acc
;
;  Stack:
;	3 Bytes, Not Including Space Used By Called Routines
;
;  Comments:
;	If String Is Longer Than 255 Bytes, Acc Will Report Length Mod 256
;
UTIL_STRLEN	proc
		push	b			; Save B
		push	dph			; Save DPH
		push	dpl			; Save DPL
		mov	b,#0			; Clear Length Counter
;
l?p1		movx	a,@dptr			; Get Byte
		cjeq	a,#ETX,l?p2		; If ETX, End Found
		inc	dptr			; Next Location
		inc	b			; Increment Length
		jmp	l?p1			; Repeat Until 0x00
;
l?p2		mov	a,b			; Get Length To A
		pop	dpl			; Recover DPL
		pop	dph			; Recover DPH
		pop	b			; Recover B
		ret				; Return To Caller
		endproc
;
;****************************************************************************
;
;  Data Area
;
		seg	xdata
X_BUFFER	ds	7
;
;****************************************************************************
;
		end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -