What assumption does this code make? Is it acceptable?

This is a snippet from a general-purpose library function that is (effectively) non-application specific and, by design, can make no assumptions about the context of the calling process.

Hint: The assumption the code makes has to do with interrupts. Do you see a problem?

_GenericFunctionA:
    MOV   A, R7
    RL    A
    ADD   A, #array_base
    MOV   R0, A
    CLR   EA
    MOV   A, @R0
    JNB   ACC.BIT_X, ?C0026
    JNB   ACC.BIT_Y, ?C0026
    SETB  ACC.BIT_Z
?C0026:
    SETB  ACC.BIT_N
    XCH   A, @R0
    SETB  EA
    JB    ACC.BIT_N, ?C0027
    MOV   R7, #0
    RET
?C0027:
    MOV   R7, #1
    RET

Parents
  • The assumption is that interrupts are enabled. This may not be the case. The following is the Keil C51 function prefix to save and disable EA state:

    ;On function entry, this code saves
    ;the state of EA and clears EA.
    0000 D3                SETB    C
    0001 10AF01            JBC     EA,?C0004
    0004 C3                CLR     C
    0005         ?C0004:
    0005 C0D0              PUSH    PSW
    

    and the code to restore the EA state:

    ;On function exit, this code restores
    ;the state of EA.
    0020 D0D0              POP     PSW
    0022 92AF              MOV     EA,C
    

    Jon

Reply
  • The assumption is that interrupts are enabled. This may not be the case. The following is the Keil C51 function prefix to save and disable EA state:

    ;On function entry, this code saves
    ;the state of EA and clears EA.
    0000 D3                SETB    C
    0001 10AF01            JBC     EA,?C0004
    0004 C3                CLR     C
    0005         ?C0004:
    0005 C0D0              PUSH    PSW
    

    and the code to restore the EA state:

    ;On function exit, this code restores
    ;the state of EA.
    0020 D0D0              POP     PSW
    0022 92AF              MOV     EA,C
    

    Jon

Children
More questions in this forum