iar_stack.s51

来自「8051试验程序 基础教材」· S51 代码 · 共 1,666 行 · 第 1/3 页

S51
1,666
字号
	ADD	A,XSP_L
	MOV	?DPL5,A
	CLR	A
	ADDC	A,XSP_H
	MOV	?DPH5,A
#if (defined (__EXTENDED_DPTR__ ))
	MOV	?DPX5,#BYTE3(sfb(XSTACK))
#endif
	RET
#endif

#if (__NUMBER_OF_DPTRS__ > 6)
?XSTACK_DISP6_8:
	ADD	A,XSP_L
	MOV	?DPL6,A
	CLR	A
	ADDC	A,XSP_H
	MOV	?DPH6,A
#if (defined (__EXTENDED_DPTR__ ))
	MOV	?DPX6,#BYTE3(sfb(XSTACK))
#endif
	RET
#endif

#if (__NUMBER_OF_DPTRS__ > 7)
?XSTACK_DISP7_8:
	ADD	A,XSP_L
	MOV	?DPL7,A
	CLR	A
	ADDC	A,XSP_H
	MOV	?DPH7,A
#if (defined (__EXTENDED_DPTR__ ))
	MOV	?DPX7,#BYTE3(sfb(XSTACK))
#endif
	RET
#endif

#else
#error "You must define __DPTR_SHADOWED__ or __DPTR_SEPARATE__"
#endif

    cfi ENDBLOCK ?XSTACK_DISP8
	ENDMOD

;-----------------------------------------------------------------------------
;
;	Function: ?XSTACK_DISP16
;
;	Description:
;	       Get a pointer to a specific object on the stack at offset X.
;
;              FFFF
;               :
;		+------+
;		| xxxx | <-- post DPTRnr
;		|   :  | -
;		|   :  |  |
;		|   :  |  | pre DPTRnr
;		|   :  |  |
;		|   :  | -  <-- XSP
;		+------+
;               :
;               0
;
;	Register input:
;		DPTRnr = 16-bit positive offset
;               DPSEL  = DPTRnr (where number same as entry nr, only for shadowed)
;
;	Register output:
;		DPTRnr = XSP + DPTRnr (pre)
;               DPSEL  = Same as input
;
;       Multiple DPTR
;               Shadowed:       Requires DPTR: none     Requires DPS: DPTRx
;                               Modifies DPTR: DPTRx    Returns  DPS: DPTRx
;
;               Separate:       Requires DPTR: none     Requires DPS: none
;                               Modifies DPTR: DPTRx    Returns  DPS: none
;
;	Stack usage: 2
;
;-----------------------------------------------------------------------------
        MODULE  ?XSTACK_DISP16
        RSEG    XSTACK:NOROOT:XDATA
	RSEG	RCODE:CODE:NOROOT
	EXTERN  ?XSP
	EXTERNS_FOR_ALL_DPTR_SYMBOLS()
	PUBLIC  ?XSTACK_DISP0_16

#if (__NUMBER_OF_DPTRS__ > 1)
	PUBLIC	?XSTACK_DISP1_16
#endif
#if (__NUMBER_OF_DPTRS__ > 2)
	PUBLIC	?XSTACK_DISP2_16
#endif
#if (__NUMBER_OF_DPTRS__ > 3)
	PUBLIC	?XSTACK_DISP3_16
#endif
#if (__NUMBER_OF_DPTRS__ > 4)
	PUBLIC	?XSTACK_DISP4_16
#endif
#if (__NUMBER_OF_DPTRS__ > 5)
	PUBLIC	?XSTACK_DISP5_16
#endif
#if (__NUMBER_OF_DPTRS__ > 6)
	PUBLIC	?XSTACK_DISP6_16
#endif
#if (__NUMBER_OF_DPTRS__ > 7)
	PUBLIC	?XSTACK_DISP7_16
#endif

    cfi BLOCK ?XSTACK_DISP16 USING cfi_common
    cfi NOFUNCTION

#if ( (__NUMBER_OF_DPTRS__ == 1) || ((__NUMBER_OF_DPTRS__ > 1) && (defined ( __DPTR_SHADOWED__ ))))
?XSTACK_DISP0_16:
?XSTACK_DISP1_16:
?XSTACK_DISP2_16:
?XSTACK_DISP3_16:
?XSTACK_DISP4_16:
?XSTACK_DISP5_16:
?XSTACK_DISP6_16:
?XSTACK_DISP7_16:
	XCH	A,DPL
	ADD	A,XSP_L
	XCH	A,DPL
	XCH	A,DPH
	ADDC	A,XSP_H
	XCH	A,DPH
#if (defined (__EXTENDED_DPTR__ ))
	MOV	?DPX,#BYTE3(sfb(XSTACK))
#endif
	RET

#elif __DPTR_SEPARATE__

?XSTACK_DISP0_16:
	XCH	A,DPL
	ADD	A,XSP_L
	XCH	A,DPL
	XCH	A,DPH
	ADDC	A,XSP_H
	XCH	A,DPH
#if (defined (__EXTENDED_DPTR__ ))
	MOV	?DPX,#BYTE3(sfb(XSTACK))
#endif
	RET

#if (__NUMBER_OF_DPTRS__ > 1)
?XSTACK_DISP1_16:
	XCH	A,?DPL1
	ADD	A,XSP_L
	XCH	A,?DPL1
	XCH	A,?DPH1
	ADDC	A,XSP_H
	XCH	A,?DPH1
#if (defined (__EXTENDED_DPTR__ ))
	MOV	?DPX1,#BYTE3(sfb(XSTACK))
#endif
	RET
#endif

#if (__NUMBER_OF_DPTRS__ > 2)
?XSTACK_DISP2_16:
	XCH	A,?DPL2
	ADD	A,XSP_L
	XCH	A,?DPL2
	XCH	A,?DPH2
	ADDC	A,XSP_H
	XCH	A,?DPH2
#if (defined (__EXTENDED_DPTR__ ))
	MOV	?DPX2,#BYTE3(sfb(XSTACK))
#endif
	RET
#endif

#if (__NUMBER_OF_DPTRS__ > 3)
?XSTACK_DISP3_16:
	XCH	A,?DPL3
	ADD	A,XSP_L
	XCH	A,?DPL3
	XCH	A,?DPH3
	ADDC	A,XSP_H
	XCH	A,?DPH3
#if (defined (__EXTENDED_DPTR__ ))
	MOV	?DPX3,#BYTE3(sfb(XSTACK))
#endif
	RET
#endif

#if (__NUMBER_OF_DPTRS__ > 4)
?XSTACK_DISP4_16:
	XCH	A,?DPL4
	ADD	A,XSP_L
	XCH	A,?DPL4
	XCH	A,?DPH4
	ADDC	A,XSP_H
	XCH	A,?DPH4
#if (defined (__EXTENDED_DPTR__ ))
	MOV	?DPX4,#BYTE3(sfb(XSTACK))
#endif
	RET
#endif

#if (__NUMBER_OF_DPTRS__ > 5)
?XSTACK_DISP5_16:
	XCH	A,?DPL5
	ADD	A,XSP_L
	XCH	A,?DPL5
	XCH	A,?DPH5
	ADDC	A,XSP_H
	XCH	A,?DPH5
#if (defined (__EXTENDED_DPTR__ ))
	MOV	?DPX5,#BYTE3(sfb(XSTACK))
#endif
	RET
#endif

#if (__NUMBER_OF_DPTRS__ > 6)
?XSTACK_DISP6_16:
	XCH	A,?DPL6
	ADD	A,XSP_L
	XCH	A,?DPL6
	XCH	A,?DPH6
	ADDC	A,XSP_H
	XCH	A,?DPH6
#if (defined (__EXTENDED_DPTR__ ))
	MOV	?DPX6,#BYTE3(sfb(XSTACK))
#endif
	RET
#endif

#if (__NUMBER_OF_DPTRS__ > 7)
?XSTACK_DISP7_16:
	XCH	A,?DPL7
	ADD	A,XSP_L
	XCH	A,?DPL7
	XCH	A,?DPH7
	ADDC	A,XSP_H
	XCH	A,?DPH7
#if (defined (__EXTENDED_DPTR__ ))
	MOV	?DPX7,#BYTE3(sfb(XSTACK))
#endif
	RET
#endif

#else
#error "You must define __DPTR_SHADOWED__ or __DPTR_SEPARATE__"
#endif

    cfi ENDBLOCK ?XSTACK_DISP16
	ENDMOD

;-----------------------------------------------------------------------------
;
;	Functions: Call function indirect.
;
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
;	Function: ?CALL_IND
;
;	Description:
;		Emulates 'CALL	@DPTR'
;
;	Register input:
;		DPTR0 = Function address to call
;
;	Register output:
;		A = Zero
;
;       Multiple DPTR
;               Shadowed:       Requires DPTR: 0        Requires DPS: 0
;                               Modifies DPTR: none     Returns  DPS: 0
;
;               Separate:       Requires DPTR: 0        Requires DPS: 0
;                               Modifies DPTR: none     Returns  DPS: 0
;
;	Stack usage: NA
;
;-----------------------------------------------------------------------------
	MODULE	?CALL_IND
	RSEG	RCODE:CODE:NOROOT
	PUBLIC	?CALL_IND
    cfi BLOCK ?CALL_IND USING cfi_common
    cfi NOFUNCTION
?CALL_IND:
	CLR	A
	JMP	@A+DPTR
    cfi ENDBLOCK ?CALL_IND
	ENDMOD


;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
;	Function: ?POP_BIT_ISP
;
;	Description:
; 		Restore spilled bit from idata stack.
;
;	Register input:
;		NONE
;
;	Register output:
;		C = restored bit
;
;	Stack usage: NA
;
;-----------------------------------------------------------------------------
	MODULE	?POP_BIT_ISP
	RSEG	RCODE:CODE:NOROOT
	PUBLIC	?POP_BIT_ISP

    cfi BLOCK ?POP_BIT_ISP USING cfi_common
    cfi NOFUNCTION

?POP_BIT_ISP:
      	PUSH	A
    cfi CFA_MACHINE MACHINE_SP - 3
      	MOV 	A,R0
      	PUSH	A
    cfi CFA_MACHINE MACHINE_SP - 4
     	MOV 	A,SP
#if (__CORE__ == __CORE_EXTENDED1__)
      	ADD 	A,#-5 	; 2 saved bytes and ret addr (3).
#else
      	ADD 	A,#-4 	; 2 saved bytes and ret addr (2).	
#endif
      	MOV 	R0,A
    	MOV 	A,@R0
   	MOV	C,ACC.7
      	POP	A
    cfi CFA_MACHINE MACHINE_SP - 3
     	MOV 	R0,A
  	POP 	A
    cfi CFA_MACHINE MACHINE_SP - 2
	RET
    cfi ENDBLOCK ?POP_BIT_ISP
	ENDMOD


;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
;	Function: ?POP_BIT_ESP
;
;	Description:
; 		Restore spilled bit from extended stack.
;
;	Register input:
;		NONE
;
;	Register output:
;		C = restored bit
;
;       Multiple DPTR
;               Shadowed:       Requires DPTR: none     Requires DPS: none
;                               Modifies DPTR: none     Returns  DPS: none
;
;               Separate:       Requires DPTR: none     Requires DPS: none
;                               Modifies DPTR: none     Returns  DPS: 0
;
;	Stack usage: ueage 7 bytes, stack after ret -1
;
;	NOTE: Find a better way and make me happy!
;
;-----------------------------------------------------------------------------
	MODULE	?POP_BIT_ESP
	RSEG	EXT_STACK:NOROOT:XDATA
	RSEG	RCODE:CODE:NOROOT
	PUBLIC	?POP_BIT_ESP
	EXTERNS_FOR_ALL_DPTR_SYMBOLS()
	EXTERN	?ESP
    cfi BLOCK ?POP_BIT_ESP USING cfi_common
    cfi NOFUNCTION

?POP_BIT_ESP:
#if ((__NUMBER_OF_DPTRS__ > 1) && defined(__DPTR_SEPARATE__ ))
	SELECT_DPTR0()
#endif
      	PUSH	A
    cfi CFA_MACHINE MACHINE_SP - 3
	MOV	A,R0
      	PUSH	A
    cfi CFA_MACHINE MACHINE_SP - 4
	MOV	A,R1
      	PUSH	A
    cfi CFA_MACHINE MACHINE_SP - 5
	MOV	A,R2
      	PUSH	A
    cfi CFA_MACHINE MACHINE_SP - 6
	MOV	A,R3
      	PUSH	A
    cfi CFA_MACHINE MACHINE_SP - 7
#if (__CORE__ == __CORE_EXTENDED1__)
	MOV	A,R4
      	PUSH	A
    cfi CFA_MACHINE MACHINE_SP - 8
#endif
	PUSH	DPL
    cfi CFA_MACHINE MACHINE_SP - 8
	PUSH	DPH
    cfi CFA_MACHINE MACHINE_SP - 9

#ifdef __EXTENDED_DPTR__
      	PUSH	?DPX
    cfi CFA_MACHINE MACHINE_SP - 10
	MOV     ?DPX,#BYTE3(sfb(EXT_STACK))
#endif


;; Get DPTR to point to element to unspill
;; subtract size of spilled registers and the return value
;;
	MOV	A,SP
	CLR	C

#if ((__CORE__ == __CORE_EXTENDED1__) && (defined(__EXTENDED_DPTR__)))
	SUBB	A,#12
#elif (__CORE__ == __CORE_EXTENDED1__)
	SUBB	A,#11
#elif (defined(__EXTENDED_DPTR__))
	SUBB	A,#10
#else
	SUBB	A,#9
#endif
	MOV	DPL,A
	CLR	A
        MOV     A,?ESP
	SUBB	A,#0
	ANL	A,#0x03         ; Maks out relevant ESP bits.
	ORL     A,#(HIGH(sfb(EXT_STACK)) & 0xFC)
	MOV	DPH,A

;; We have to rearange the stack, save RET, ACC, and C to
;; temp registers
;;
	MOVX	A,@DPTR
	MOV	R0,A		; R0 = Cy
	INC 	DPTR

	MOVX	A,@DPTR
	MOV	R2,A		; R2 = RET1
	INC 	DPTR

	MOVX	A,@DPTR
	MOV	R3,A		; R3 = RET2
	INC 	DPTR

#if (__CORE__ == __CORE_EXTENDED1__)
	MOVX	A,@DPTR
	MOV	R4,A		; R4 = RET3
	INC 	DPTR
#endif

	MOVX	A,@DPTR
	MOV	R1,A		; R1 = Acc


;; Make DPTR once again point to the value to unspill, 
;; stack -1 from original ret value
;;
	CLR	C
	MOV	A,DPL
#if (__CORE__ == __CORE_EXTENDED1__)
	SUBB	A,#4
#else
	SUBB	A,#3
#endif
	MOV	DPL,A
	MOV	A,DPH
	SUBB	A,#0
	MOV	DPH,A


;; rearange stack, we want RET at bottom, followed by A and then C,
;; value to restore
;;
	MOV	A,R2
	MOVX	@DPTR,A		; RET -> stack
	INC	DPTR

	MOV	A,R3
	MOVX	@DPTR,A
	INC	DPTR

#if (__CORE__ == __CORE_EXTENDED1__)
	MOV	A,R4
	MOVX	@DPTR,A
	INC	DPTR
#endif

	MOV	A,R1		; ACC -> stack
	MOVX	@DPTR,A
	INC	DPTR

	MOV	A,R0		; C -> stack
	MOVX	@DPTR,A

;; Make DPTR point to top of stack
;;
	MOV	A,DPL
#if ((__CORE__ == __CORE_EXTENDED1__) && (defined(__EXTENDED_STACK__)))
	ADD	A,#8
#elif (__CORE__ == __CORE_EXTENDED1__)
	ADD	A,#7
#elif (defined(__EXTENDED_DPTR__))
	ADD	A,#7
#else
	ADD	A,#6
#endif
	MOV	DPL,A
	CLR	A
	ADDC	A,DPH
	MOV	DPH,A


;; Restore saved values
;;

#ifdef __EXTENDED_DPTR__
	POP	?DPX		; restore DPTR
    cfi CFA_MACHINE MACHINE_SP - 11
#endif
	POP	DPH
    cfi CFA_MACHINE MACHINE_SP - 10
	POP	DPL
    cfi CFA_MACHINE MACHINE_SP - 9
#if (__CORE__ == __CORE_EXTENDED1__)
	POP	A
    cfi CFA_MACHINE MACHINE_SP - 8
	MOV	R4,A		; restore (R4),R3,R2,R1,R0
#endif
	POP	A
    cfi CFA_MACHINE MACHINE_SP - 7
	MOV	R3,A
	POP	A
    cfi CFA_MACHINE MACHINE_SP - 6
	MOV	R2,A
	POP	A
    cfi CFA_MACHINE MACHINE_SP - 5
	MOV	R1,A
	POP	A
    cfi CFA_MACHINE MACHINE_SP - 4
	MOV	R0,A
	POP	A		; value to restore
    cfi CFA_MACHINE MACHINE_SP - 3
   	MOV	C,ACC.7
	POP	A		; restore A
    cfi CFA_MACHINE MACHINE_SP - 2
	RET
    cfi ENDBLOCK ?POP_BIT_ESP
	ENDMOD

	END

⌨️ 快捷键说明

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