USING Assembler Statement
Arguments USING expression
Description The USING statement specifies the register bank (0-3) expression to use for encoding the >AR0-AR7 registers. The register bank selected is noted in the object file and the memory area is reserved by the linker.
Some 8051 instructions (like PUSH and POP) allow only absolute addresses to be used. The assembler replaces absolute registers (AR0-AR7) with the physical address of the register in the current register bank. So, while the instruction PUSH R0 is not valid, PUSH AR0 is valid. However, the assembler must know which register bank is used so that the correct physical address is calculated. This is the purpose for the USING statement.
The USING statement does not generate any code to switch the current register bank. The assembler program must select the correct register bank. For example, the following code selects register bank 2:
PUSH
PSW
; save the current register bank
MOV
PSW, #(2 SHL 3)
; set register bank 2
.
.
.
POP
PSW
; restore saved register bank
The physical address is calculated as follows:
(register bank × 8) + register
下面写了个例子
程序代码:
ORG 0
MOV SP,#2FH
;=======================================================
PUSH PSW ; save the current register bank
MOV PSW, #(2 SHL 3) ; set register bank 2
MOV R2,#12H ; 写入R2到bank 2
MOV R7,#34H ; 写入R7到bank 2
;-----------------------
USING 2 ; select register bank 2
PUSH AR2 ; push R2 in bank 2 (address 12h)
PUSH AR7 ; push R7 in bank 2 (address 17h)
POP AR7
POP AR2
;-----------------------
POP PSW ; restore saved register bank
;=======================================================
PUSH PSW ; save the current register bank
MOV PSW, #(3 SHL 3) ; set register bank 3
MOV R2,#12H ; 写入R2到bank 3
MOV R7,#34H ; 写入R7到bank 3
;-----------------------
USING 3 ; select register bank 3
PUSH AR2 ; push R2 in bank 3 (address 1Ah)
PUSH AR7 ; push R7 in bank 3 (address 1Fh)
POP AR7
POP AR2
;-----------------------
POP PSW ; restore saved register bank
;=======================================================
;以下也是可行的
MOV 0Ah,#12H ;间接方式 R2 bank 1
MOV 0FH,#34H ;间接方式 R7 bank 1
PUSH 0Ah
PUSH 0FH
POP 0FH
POP 0Ah
AJMP $
END