The LOCK prefix

Assert the LOCK# signal for the duration of the accompanying instruction.

Format

PrefixDescription
LOCKAssert the lock# signal for the duration of the accompanying instruction.

Asserts the processor bus lock signal during execution of the accompanying instruction (turning the instruction into an atomic one). In a multiprocessor environment the LOCK# signal guarantees that the processor has the exclusive right to use any shared memory while the signal is asserted.

The lock prefix can be added only to the following instructions and only to those forms of the instructions in which the destination operand is a memory operand: add, adc, and, dec, inc, neg, not, or, sbb, sub, xor and xchg.

Usage example

Atomic increment of the value of the variable value by 1.

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    value dw 4
data ends

code segment public           ; Start of the code segment
    assume ds:data, cs:code

    main proc                 ; Start of the main procedure
        mov     ax, seg data  ; Get the data segment address and store it in the ax register
        mov     ds, ax        ; Pass the data segment address from ax to ds

   lock add     value, 1      ; Atomically add 1 to the variable value
        mov     ax, value     ; Load the value of value into ax (now 5)

        mov     ax, 4C00h     ; Copy the process termination function number into ah and the value into al
        int     21h           ; Call the interrupt
    main endp                 ; End of the main procedure

code ends                     ; End of the code segment
end main                      ; End of file. The main procedure is the entry point

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".

Processor status flags

CF
?
PF
?
AF
?
ZF
?
SF
?
TF
No
IF
No
DF
No
OF
?

? — Depends on the locked instruction.