iar_stack.s51

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

S51
1,666
字号
/*******************************************************************************
 *
 * Functions to get stackpointers with offset and functions to allocate stack.
 *
 * Copyright 2003-2005 IAR Systems. All rights reserved.
 *
 * $Revision: 4510 $
 *
 ******************************************************************************/

#include "iar_common.h"

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
;	Function: ?ALLOC_EXT_STACK8
;
;	Description:
;		Allocate an area of size 1 to 256 bytes on the extended stack.
;
;
;              3FF
;               :
;		+------+
;		| xxxx | <-- post ESP
;		+ - - -+
;		|   :  | - - - - - - - - 
;		|   :  |                 |
;		|   :  |                 |
;		|   :  | <-- pre ESP     |
;		+ - - -+                 | To be deallocated
;		| RetX |                 |
;		+ - - -+                 |
;		| RetH |                 |
;		+ - - -+                 |
;		| RetL | - - - - - - - - 
;		+------+
;               :
;               0
;
;
;	Register input:
;		A      : Number of bytes to allocate.
;
;	Register output:
;		ESP    = ESP + A
;		DPTR0  = Ret addr
;		A      = Undefined.
;		R0      = Undefined.
;		CY     = Undefined.
;
;       Multiple DPTR
;               Shadowed:       Requires DPTR: DPTRx    Requires DPS: DPTRx
;                               Modifies DPTR: DPTRx    Returns  DPS: DPTRx
;
;               Separate:       Requires DPTR: 0        Requires DPS: 0
;                               Modifies DPTR: 0        Returns  DPS: 0
;
;	Stack usage: 3
;
;-----------------------------------------------------------------------------
	MODULE	?ALLOC_EXT_STACK8
	RSEG	RCODE:CODE:NOROOT
	PUBLIC	?ALLOC_EXT_STACK8
	EXTERNS_FOR_ALL_DPTR_SYMBOLS()
	EXTERN	?ESP

    cfi BLOCK ?ALLOC_EXT_STACK8 USING cfi_common
    cfi NOFUNCTION

?ALLOC_EXT_STACK8:
?ALLOC_EXT_STACK8_CFI_INVALID_BEGINS:
    cfi invalid
#if (__CORE__ == __CORE_EXTENDED1__)
	POP	?DPX
    cfi CFA_MACHINE MACHINE_SP - 2
    cfi ?RET_EXT DPX0
#elif defined(__EXTENDED_DPTR__)
        MOV     ?DPX,#0
#endif
	POP	DPH
    cfi CFA_MACHINE MACHINE_SP - 1
    cfi ?RET_HIGH DPH0
	POP	DPL             ; DPTR0 = Ret addr
    cfi CFA_MACHINE MACHINE_SP - 0
    cfi ?RET_LOW DPL0

	ADD	A,SP
	MOV	R0,A
	CLR	A
	;; Preserve the Interrupt Enable register and disable all interrupts
	;; Interrupts disabled for max 7 cycles
	XCH     A, 0xA8 /* IE */
	XCH	A, R0
	MOV	SP,A
	JNC	NoBorrow
	INC	?ESP
NoBorrow:
	;; Enable interrupts
	MOV	0xA8 /* IE */,R0

	CLR	A
	JMP	@A+DPTR

?ALLOC_EXT_STACK8_CFI_INVALID_ENDS:
    cfi valid
    cfi ENDBLOCK ?ALLOC_EXT_STACK8
	ENDMOD

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
;	Function: ?ALLOC_EXT_STACK16
;
;	Description:
;		Allocate an area of size 1 to 0xFFFF bytes on the extended stack.
;
;	Register input:
;		A      : Number of bytes to allocate (Low byte).
;		R0     : Number of bytes to allocate (High byte).
;
;	Register output:
;		ESP    = ESP + R0:A
;		DPTR0  = Ret addr
;		A      = Undefined.
;		R0     = Undefined.
;		B      = Undefined.
;		CY     = Undefined.
;
;       Multiple DPTR
;               Shadowed:       Requires DPTR: DPTRx    Requires DPS: DPTRx
;                               Modifies DPTR: DPTRx    Returns  DPS: DPTRx
;
;               Separate:       Requires DPTR: 0        Requires DPS: 0
;                               Modifies DPTR: 0        Returns  DPS: 0
;
;	Stack usage: 3
;
;-----------------------------------------------------------------------------
	MODULE	?ALLOC_EXT_STACK16
	RSEG	RCODE:CODE:NOROOT
	PUBLIC	?ALLOC_EXT_STACK16
	EXTERNS_FOR_ALL_DPTR_SYMBOLS()
	EXTERN	?ESP

    cfi BLOCK ?ALLOC_EXT_STACK16 USING cfi_common
    cfi NOFUNCTION

?ALLOC_EXT_STACK16:
?ALLOC_EXT_STACK16_CFI_INVALID_BEGINS:
    cfi invalid
#if (__CORE__ == __CORE_EXTENDED1__)
	POP	?DPX
    cfi CFA_MACHINE MACHINE_SP - 2
    cfi ?RET_EXT DPX0
#elif defined(__EXTENDED_DPTR__)
        MOV ?DPX,#0
#endif
	POP	DPH
    cfi CFA_MACHINE MACHINE_SP - 1
    cfi ?RET_HIGH DPH0
	POP	DPL             ; DPTR0 = Ret addr
    cfi CFA_MACHINE MACHINE_SP - 0
    cfi ?RET_LOW DPL0

	ADD	A,SP
	MOV	B,A
	CLR	A
	;; Preserve the Interrupt Enable register
	;; Disable interrupts, max 7 cycles
	XCH	A,0xA8 /* IE */    
	XCH	A,B
	MOV     SP,A
	MOV	A,R0
	ADDC	A,?ESP
	MOV	?ESP,A
	;; Restore interrupts
	MOV	0xA8 /* IE */,B    

	CLR	A
	JMP	@A+DPTR
?ALLOC_EXT_STACK16_CFI_INVALID_ENDS:
    cfi valid
    cfi ENDBLOCK ?ALLOC_EXT_STACK16
	ENDMOD


;-----------------------------------------------------------------------------
;
;	Function: ?DEALLOC_EXT_STACK8
;
;	Description:
;		Deallocate an area of size 1 to 255 bytes on the extended stack.
;
;              3FF
;               :
;		+------+
;		| xxxx | <-- pre ESP
;		+ - - -+
;		| RetX |
;		+ - - -+
;		| RetH |
;		+ - - -+
;		| RetL |
;		+ - - -+ -
;		|   :  |  |
;		|   :  |  | To be deallocated
;		|   :  |  |
;		|   :  |  |
;		+ - - -+ -
;		| xxxx | <-- post ESP
;		+------+
;               :
;               0
;
;	Register input:
;		A      : -(Number of bytes to allocate)
;
;	Register output:
;		ESP    = ESP - A
;		DPTR0  = Ret addr
;		A      = Undefined
;		R0     = Undefined
;		B.0    = Undefined
;
;       Multiple DPTR
;               Shadowed:       Requires DPTR: DPTRx    Requires DPS: DPTRx
;                               Modifies DPTR: DPTRx    Returns  DPS: DPTRx
;
;               Separate:       Requires DPTR: 0        Requires DPS: 0
;                               Modifies DPTR: 0        Returns  DPS: 0
;
;	Stack usage: 3
;
;-----------------------------------------------------------------------------
	MODULE	?DEALLOC_EXT_STACK8
	RSEG	RCODE:CODE:NOROOT
	PUBLIC	?DEALLOC_EXT_STACK8
	EXTERN	?ESP
	EXTERNS_FOR_ALL_DPTR_SYMBOLS()

    cfi BLOCK ?DEALLOC_EXT_STACK8 USING cfi_common
    cfi NOFUNCTION

?DEALLOC_EXT_STACK8:
?DEALLOC_EXT_STACK8_CFI_INVALID_BEGINS:
    cfi invalid
#if (__CORE__ == __CORE_EXTENDED1__)
	POP	?DPX
    cfi CFA_MACHINE MACHINE_SP - 2
    cfi ?RET_EXT DPX0
#elif defined(__EXTENDED_DPTR__)
        MOV ?DPX,#0
#endif
	POP	DPH
    cfi CFA_MACHINE MACHINE_SP - 1
    cfi ?RET_HIGH DPH0
	POP	DPL             ; DPTR0 = Ret addr
    cfi CFA_MACHINE MACHINE_SP - 0
    cfi ?RET_LOW DPL0

	MOV	B.0,C              ; Preserve carry flag.
	ADD	A,SP
	MOV	R0,A
	CLR	A
	;; Preserve the Interrupt Enable register
	;; Disable interrupts, max 7 cycles		
	XCH	A, 0xA8/* IE */    
	XCH	A, R0
	MOV	SP,A
	JC	NoBorrow
	DEC	?ESP
NoBorrow:
	;; Restore interrupts
	MOV	0xA8/* IE */,R0
	MOV	C,B.0
	
	CLR	A
	JMP	@A+DPTR
?DEALLOC_EXT_STACK8_CFI_INVALID_ENDS:
    cfi valid
    cfi ENDBLOCK ?DEALLOC_EXT_STACK8
	ENDMOD

;-----------------------------------------------------------------------------
;
;	Function: ?DEALLOC_EXT_STACK16
;
;	Description:
;		Deallocate an area of size 1 to 0xFFFF bytes on the extended stack.
;
;	Register input:
;		A      : -(Number of bytes to allocate) (Low byte).
;		R0     : -(Number of bytes to allocate) (High byte).
;
;	Register output:
;		ESP    = ESP - R0:A
;		DPTR   = Ret addr
;		A      = Undefined
;		R0     = Undefined
;		B      = Undefined
;
;       Multiple DPTR:
;               Shadowed:       Requires DPTR: DPTRx    Requires DPS: DPTRx
;                               Modifies DPTR: DPTRx    Returns  DPS: DPTRx
;
;               Separate:       Requires DPTR: 0        Requires DPS: 0
;                               Modifies DPTR: 0        Returns  DPS: 0
;
;	Stack usage: 3
;
;-----------------------------------------------------------------------------
	MODULE	?DEALLOC_EXT_STACK16
	RSEG	RCODE:CODE:NOROOT
	PUBLIC	?DEALLOC_EXT_STACK16
	EXTERN	?ESP
	EXTERNS_FOR_ALL_DPTR_SYMBOLS()

    cfi BLOCK ?DEALLOC_EXT_STACK16 USING cfi_common
    cfi NOFUNCTION

?DEALLOC_EXT_STACK16:
?DEALLOC_EXT_STACK16_CFI_INVALID_BEGINS:
    cfi invalid
#if (__CORE__ == __CORE_EXTENDED1__)
	POP	?DPX
    cfi CFA_MACHINE MACHINE_SP - 2
    cfi ?RET_EXT DPX0
#elif defined(__EXTENDED_DPTR__)
        MOV     ?DPX,#0
#endif
	POP	DPH
    cfi CFA_MACHINE MACHINE_SP - 1
    cfi ?RET_HIGH DPH0
	POP	DPL                  ; DPTR = Ret addr
    cfi CFA_MACHINE MACHINE_SP - 0
    cfi ?RET_LOW DPL0

	ADD	A,SP
	MOV	B,A
	CLR	A
	;; Preserve the Interrupt Enable register
	;; Disable interrupts, max 10 cycles
	XCH	A,0xA8 /* IE */
	XCH	A,B
	MOV	0xA8.0,C	 ; Preserve carry flag.
	MOV	SP,A
	MOV	A,R0
	ADDC	A,?ESP
	MOV	?ESP,A
	MOV	C,0xA8.0	; Restore carry flag
	;; Restore interrupts
	MOV	0xA8 /* IE */,B 
	
	CLR	A
	JMP	@A+DPTR
?DEALLOC_EXT_STACK16_CFI_INVALID_ENDS:
    cfi valid
    cfi ENDBLOCK ?DEALLOC_EXT_STACK16
	ENDMOD

;-----------------------------------------------------------------------------
;
;	Functions: ?EXT_STACK_DISP8
;
;	Description:
;		Get a pointer to a specific object on the stack at offset X.
;
;              3FF
;               :
;		+------+
;		| xxxx | <-- ESP
;		+ - - -+
;		| RetX | -
;		+ - - -+  |
;		| RetH |  |
;		+ - - -+  |
;		| RetL |  | Displacement (A)
;		+ - - -+  |
;		|   :  |  |
;		|   :  | -  <-- DPTR
;		+------+
;               :
;               0
;
;	Register input:
;		A     = 8-bit offset from ESP - 3.
;
;	Register output:
;		DPTRx  = EXT_STACK + 0000(A)
;		A      = DPH
;
;       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	?EXT_STACK_DISP8
	EXTERNS_FOR_ALL_DPTR_SYMBOLS()
	EXTERN	?ESP
	PUBLIC	?EXT_STACK_DISP0_8
#if (__NUMBER_OF_DPTRS__ > 1)
	PUBLIC	?EXT_STACK_DISP1_8
#endif
#if (__NUMBER_OF_DPTRS__ > 2)
	PUBLIC	?EXT_STACK_DISP2_8
#endif
#if (__NUMBER_OF_DPTRS__ > 3)
	PUBLIC	?EXT_STACK_DISP3_8
#endif
#if (__NUMBER_OF_DPTRS__ > 4)
	PUBLIC	?EXT_STACK_DISP4_8
#endif
#if (__NUMBER_OF_DPTRS__ > 5)
	PUBLIC	?EXT_STACK_DISP5_8
#endif
#if (__NUMBER_OF_DPTRS__ > 6)
	PUBLIC	?EXT_STACK_DISP6_8
#endif
#if (__NUMBER_OF_DPTRS__ > 7)
	PUBLIC	?EXT_STACK_DISP7_8
#endif
	RSEG	EXT_STACK:NOROOT:XDATA
	RSEG	RCODE:CODE:NOROOT
    cfi BLOCK ?EXT_STACK_DISP8 USING cfi_common
    cfi NOFUNCTION

#if ( (__NUMBER_OF_DPTRS__ == 1) || ((__NUMBER_OF_DPTRS__ > 1) && (defined ( __DPTR_SHADOWED__ ))))
?EXT_STACK_DISP0_8:
?EXT_STACK_DISP1_8:
?EXT_STACK_DISP2_8:
?EXT_STACK_DISP3_8:
?EXT_STACK_DISP4_8:
?EXT_STACK_DISP5_8:
?EXT_STACK_DISP6_8:
?EXT_STACK_DISP7_8:
	ADD	A,SP
	MOV	DPL,A
	CLR	A
	ADDC	A,?ESP
	ANL	A,#0x03         ; Maks out relevant ESP bits.
	ORL     A,#(HIGH(sfb(EXT_STACK)) & 0xFC)
	MOV	DPH,A
#ifdef __EXTENDED_DPTR__
	MOV     ?DPX,#BYTE3(sfb(EXT_STACK))
#endif
	RET

#elif __DPTR_SEPARATE__

?EXT_STACK_DISP0_8:
	ADD	A,SP
	MOV	DPL,A
	CLR	A
	ADDC	A,?ESP
	ANL	A,#0x03         ; Maks out relevant ESP bits.
	ORL     A,#(HIGH(sfb(EXT_STACK)) & 0xFC)
	MOV	DPH,A
#ifdef __EXTENDED_DPTR__
	MOV     ?DPX,#BYTE3(sfb(EXT_STACK))
#endif
	RET

EXT_STACK_DISP8 MACRO nr
	ADD	A,SP
	MOV	?DPL\1,A
	CLR	A
	ADDC	A,?ESP
	ANL	A,#0x03         ; Maks out relevant ESP bits.
	ORL     A,#(HIGH(sfb(EXT_STACK)) & 0xFC)
	MOV	?DPH\1,A
#ifdef __EXTENDED_DPTR__
	MOV     ?DPX\1,#BYTE3(sfb(EXT_STACK))
#endif
	RET

        ENDM

#if (__NUMBER_OF_DPTRS__ > 1)
?EXT_STACK_DISP1_8:
	EXT_STACK_DISP8 1
#endif
#if (__NUMBER_OF_DPTRS__ > 2)
?EXT_STACK_DISP2_8:
	EXT_STACK_DISP8 2
#endif
#if (__NUMBER_OF_DPTRS__ > 3)
?EXT_STACK_DISP3_8:
	EXT_STACK_DISP8 3
#endif
#if (__NUMBER_OF_DPTRS__ > 4)
?EXT_STACK_DISP4_8:
	EXT_STACK_DISP8 4
#endif
#if (__NUMBER_OF_DPTRS__ > 5)
?EXT_STACK_DISP5_8:
	EXT_STACK_DISP8 5
#endif
#if (__NUMBER_OF_DPTRS__ > 6)
?EXT_STACK_DISP6_8:
	EXT_STACK_DISP8 6
#endif
#if (__NUMBER_OF_DPTRS__ > 7)
?EXT_STACK_DISP7_8:
	EXT_STACK_DISP8 7
#endif

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

    cfi ENDBLOCK ?EXT_STACK_DISP8
	ENDMOD

;-----------------------------------------------------------------------------
;
;	Function: ?EXT_STACK_DISP16
;
;	Description:
;		Get a pointer to a specific object on the stack at offset X.
;
;              3FF
;               :
;		+------+
;		| xxxx | <-- ESP
;		+ - - -+
;		| RetX | -
;		+ - - -+  |
;		| RetH |  |
;		+ - - -+  |
;		| RetL |  | Displacement (DPTR)
;		+ - - -+  |
;		|   :  |  |
;		|   :  | -  <-- DPTR
;		+------+
;               :
;               0
;
;	Register input:
;		DPTRx = 10-bit offset from ESP - 3
;
;	Register output:
;		DPTRx = ESP - DPTRx
;
;       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
;

⌨️ 快捷键说明

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