Moving multi-byte widgets.

From: R. Clayton <rclayton_at_monmouth.edu>
Date: Tue, 30 Sep 2014 23:44:31 -0400
This note expands a bit on moving multi-byte widgets.

This code

  $ cat t.c
  struct point {
    double x, y, z;
    };

  struct point q;

  int 
  main() {

    struct point a;

    a = q;
    q = a;

    return 0;
    }

  $

defines a widget (a 3D point) as three double-precision floating point numbers.
At 8 bytes per number, a point is 24 bytes.  It also copies points back and
forth (ignore the fact that q is undefined).  The resulting assembly code is

  $ gcc -S t.c

  $ cat -n t.s
       1          .file   "t.c"
       2          .comm   q,24,8
       3          .text
       4          .globl  main
       5          .type   main, @function
       6  main:
       7  .LFB2:
       8          .cfi_startproc
       9          pushl   %ebp
      10          .cfi_def_cfa_offset 8
      11          .cfi_offset 5, -8
      12          movl    %esp, %ebp
      13          .cfi_def_cfa_register 5
      14          subl    $32, %esp
      15          movl    q, %eax
      16          movl    %eax, -24(%ebp)
      17          movl    q+4, %eax
      18          movl    %eax, -20(%ebp)
      19          movl    q+8, %eax
      20          movl    %eax, -16(%ebp)
      21          movl    q+12, %eax
      22          movl    %eax, -12(%ebp)
      23          movl    q+16, %eax
      24          movl    %eax, -8(%ebp)
      25          movl    q+20, %eax
      26          movl    %eax, -4(%ebp)
      27          movl    -24(%ebp), %eax
      28          movl    %eax, q
      29          movl    -20(%ebp), %eax
      30          movl    %eax, q+4
      31          movl    -16(%ebp), %eax
      32          movl    %eax, q+8
      33          movl    -12(%ebp), %eax
      34          movl    %eax, q+12
      35          movl    -8(%ebp), %eax
      36          movl    %eax, q+16
      37          movl    -4(%ebp), %eax
      38          movl    %eax, q+20
      39          movl    $0, %eax
      40          leave
      41          .cfi_restore 5
      42          .cfi_def_cfa 4, 4
      43          ret
      44          .cfi_endproc
      45  .LFE2:
      46          .size   main, .-main
      47          .ident  "GCC: (Debian 4.9.1-14) 4.9.1"
      48          .section        .note.GNU-stack,"",@progbits

  $ 

The assignment "a = q" occurs at lines 15 to 26; the assignment "q = a" occurs
at lines 27 to 38.  The assignment is done four bytes at a time in two
instructions (the first instruction copies four bytes from the source to the
eax register, and the second instruction copies eax to the destination), for a
total of six moves (15-16, 17-18, 19-20, 21-22, 23-24, and 25-26 for a = q).

It's not to hard to imagine the havoc that would occur if one thread executed
lines 15 to 26, another thread executed lines 27 to 38, and one thread context
switched to the other in the middle of execution.
Received on Tue Sep 30 2014 - 23:47:33 EDT

This archive was generated by hypermail 2.2.0 : Thu Oct 02 2014 - 14:52:52 EDT