vimja

at&t inline assembly example

Oct 9th, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.44 KB | None | 0 0
  1. #include <stdio.h>
  2. unsigned short len;
  3. unsigned short width;
  4. // On a 64Bit System (w/ 64bit OS) int is 4Byte, thus long enough
  5. unsigned int area;
  6.  
  7. int main()
  8. {
  9.  
  10.   // Figure out the size of a short
  11.   //printf("sizeof(short) == %lu Byte\n", sizeof(short));
  12.   // 2 Byte
  13.  
  14.   // Figure out the size of a int
  15.   //printf("sizeof(int) == %lu Byte\n", sizeof(int));
  16.   // 4 Byte
  17.  
  18.   // Figure out the size of a long
  19.   //printf("sizeof(long) == %lu Byte\n", sizeof(long));
  20.   // 8 Byte
  21.  
  22. /*
  23.   __asm__ __volatile__
  24.     (
  25.      "movw $10000, (width)\n\t"
  26.      "movw $5000, (len)\n\t"
  27.      "movw (width), %%ax\n\t"
  28.      // Multiply. Result goes to DX:AX
  29.      "mulw (len)\n\t"
  30.      // Delete everyting in edx, except for the 16 lowest Bit
  31.      "and $0x0ffff, %%edx\n\t"
  32.      "movl %%edx, (area)\n\t"
  33.      "movw %%ax, %%bx\n\t"
  34.      // Move the upper 16 Bit of the result into eax
  35.      "movl (area), %%eax\n\t"
  36.      // Shift them 16 Bit to the left
  37.      "movl $0x10000, %%ecx\n\t"
  38.      "mull %%ecx\n\t"
  39.      // the result ends up in dx:ax
  40.      // Now add the pieces together
  41.      "add %%bx, %%ax\n\t"
  42.      "mov %%eax, (area)"
  43.      :
  44.      :
  45.      : "%eax", "%ecx", "%edx"
  46.     );
  47. */
  48.   __asm__ __volatile__
  49.     (
  50.      "movw $10000, (width)\n\t"
  51.      "movw $5000, (len)\n\t"
  52.      "movw (width), %%ax\n\t"
  53.      // Multiply. Result goes to DX:AX
  54.      "mulw (len)\n\t"
  55.      // only copy dx (the upper 16 Bit of the result)
  56.      "movw %%dx, (area)\n\t"
  57.      // Copy the lower 16 Bit of the result to bx
  58.      "movw %%ax, %%bx\n\t"
  59.      // Move the upper 16 Bit of the result into eax
  60.      "movl (area), %%eax\n\t"
  61.      // Now shift them so that they're in the right position
  62.      "sal $16, %%eax\n\t"
  63.      // Now add the pieces together
  64.      "add %%bx, %%ax\n\t"
  65.      "mov %%eax, (area)"
  66.      : /* no output registers */
  67.      : /* no input registers */
  68.      : "%eax", "%ecx"
  69.     );
  70.  
  71.   printf("width is now: %d \n", width);
  72.   printf("len is now: %d \n", len);
  73.   printf("area is now: %d \n", area);
  74.  
  75.   return 0;
  76. }
  77. /*
  78.                 mov width, 10000
  79.                 mov len, 5000
  80.                 mov ax, width
  81.                 mul len
  82.                 // make sure edx is clean, except for the value in dx
  83.                 and edx, 0ffffh
  84.                 mov area, edx
  85.                 mov bx, ax
  86.                 mov eax, area
  87.                 mov ecx, 10000h
  88.                 mul ecx
  89.                 add ax, bx
  90.                 mov area, eax
  91. */
Advertisement
Add Comment
Please, Sign In to add comment