Re: Teaching Assembly Language Programming



On Jun 21, 2:14 pm, zjrm <zjboyguard-newsg...@xxxxxxxxx> wrote:
After reading your response I finally went out and took a quick look
at the description of the "Test and Set"-ish instructions in the Intel
IS Reference. My naive expectation was that the instructions would be
implicitly atomic. However, this being the x86 IAS, I should have
known better. Apparently you can make *any* of the instructions ADD,
ADC, AND, BTC, BTR, BTS, CMPXCHG, CMPXCH8B, DEC, INC, NEG, NOT, OR,
SBB, SUB, XOR, XADD, and XCHG atomic by throwing the LOCK prefix in
front of it. Why and under what circumstance you might want to do this
with all of these instructions was of course not covered since that's
not the type of detail a reference manual goes into.


The classic test-and-set in x86 is done with xchg, which, BTW, is
always locked (at least on anything more recent than a 80186) when
used with a memory operand.

The other atomic instructions can be very useful with lock-free
techniques. Not that you can't build everything starting with spin-
locks and a test-and-set. For example, let's say you're keeping a
count of some event. With only a mutex or a spin lock, the sequence
is acquire lock, increment counter, release lock. If you don't do
that, two inc instructions running on separate processors can step on
each other (IOW, both read the same old value, both increment that,
and then both write their updated value back to memory, which results
in a lost update). With an atomic increment, you can just do the
increment without the overhead of acquiring a lock or mutex.

Compare and swap (CMPXCHG), for example, can be used in a loop to
update a share data item (perhaps a pointer into a linked list, or
anything else). The trick is that CMPXCHG lets you see if the state
was still the same as when you started the update, and updates it only
if it is. If it isn't (because someone else updated it), you can then
retry. For example, lets say you have a pointer to the first element
of a linked list, and you want to add an item to that linked list. So
you get the first item pointer, build your new item with that its
"next" pointer pointing to the old "first" item, and then use CMPXCHG
to update the first item pointer top point to the new item, but only
if the first pointer is still the same as when you started. If not,
because someone else inserted (or removed) an item in the list, you
can retry the operation by adjusting the pointer in the new item
you're adding.

Compare and swap can be used to implement most of the other locked
operations, but if the task fits, the direct operations are usually
faster and simpler. For example, an atomic increment done with
compare-and-swap:


counter dd 0

mov eax,counter ;assumes aligned
retry:
mov ebx,eax
inc ebx
lock cmpxchg counter,ebx
jnz retry


While that works fine, a simple locked inc would be better.

.



Relevant Pages

  • Re: page 120 K&R
    ... The C abstract machine also has some hoops to jump through, ... converting the pointer to the function to a function designator. ... You cannot increment a function ... :-) If instructions are one byte long, ...
    (comp.lang.c)
  • Re: Off Topic: use delete to destroy primitive/object types but memory is not freed
    ... Each C pointer is a descriptor that names an address ... upon loading a segment register. ... Part requires that the compiler emit instructions whose sole ...
    (comp.lang.c)
  • Re: Compile Barrier
    ... > Barrier addressing issue with instructions reordering (pipelining feature on ... So a barrier is a mechanism for a programmer ... To address this issue compiler writes recognize some functions ... > - a will be written to the main memory before the lock is released ...
    (microsoft.public.win32.programmer.kernel)
  • Re: More on Canon Rebel XT noise at high ISO - 2 main new data points
    ... circumstance where you need to take an exposure lock the English used to describe operation of it is clear, concise, and has no evidence of the poor 'literal' translation that I have seen in some instructions. ... it is more profitable and that no one else that because Canon 'must be right' then you would probably have received less references to your stupidity, it was only quite recently that you understood the implication of the function described on page 101 of the camera instruction manual ("FE (flash exposure) lock obtains and locks the correct flash exposure reading for any part of the subject"). ...
    (rec.photo.digital.slr-systems)
  • Re: WaitForSingleObject() will not deadlock
    ... I'd like to see the EXACT SEQUENCE OF INSTRUCTIONS issued in the locking sequence, ... My issue about the 2 CPU clock cycles is that once the lock is set, ... cycle detection using non-recursive mutex: ...
    (microsoft.public.vc.mfc)