beautypg.com

10 register protection – Texas Instruments MSC1210 User Manual

Page 122

background image

Register Protection

10-16

10.10 Register Protection

One very important rule applies to all interrupt handlers: interrupts must leave
the processor in the same state as it was in when the interrupt initiated. Re-
member, the idea behind interrupts is that the main program is not aware that
they are executing in the background. However, consider the following code:

CLR C

;Clear carry

MOV A,#25h

;Load the accumulator with 25h

ADDC A,#10h ;Add 10h, with carry

After the above three instructions are executed, the accumulator will contain
a value of 35

H

.

However, what would happen if an interrupt occurred right after the MOV in-
struction? During this interrupt, the carry bit was set and the value of the accu-
mulator changed to 40

H

. When the interrupt finished and control was passed

back to the main program, the ADDC would add 10

H

to 40

H

, and also add an

additional 01

H

because the carry bit is set. The accumulator will contain the

value 51

H

at the end of execution.

In this case, the main program has seemingly calculated the wrong answer. How
can 25

H

+ 10

H

yield 51

H

as a result? It does not make sense. A developer that

was unfamiliar with interrupts would be convinced that the microcontroller was
damaged in some way, provoking problems with mathematical calculations.

What has happened, in reality, is the interrupt did not protect the registers it
used. Restated: an interrupt must leave the processor in the same state as it
was in when the interrupt initiated.

This means if an interrupt uses the accumulator, it must insure that the value
of the accumulator is the same at the end of the interrupt as it was at the begin-
ning. This is generally accomplished with a PUSH and POP sequence at the
beginning and end of each interrupt handler. For example:

INTERRUPT_HANDLER:

PUSH ACC

;Push the initial value of accumulator
;onto stack

PUSH PSW

;Push the initial value of PSW SFR onto stack

MOV A,#0FFh

;Use accumulator & PSW for whatever you want

ADD A,#02h

;Use accumulator & PSW for whatever you want

POP PSW

;Restore the initial value of the PSW from
;the stack

POP ACC

;Restore initial value of the accumulator
;from stack