summaryrefslogtreecommitdiffstats
path: root/asm2.c
blob: 7621b7838867075beffce0bcc62ca63fd5c2523b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * So far I cannot figure out how I used to compile this :/
 * asm(".intel_syntax prefix");
 *
 */

#include <stdio.h>

void asm_summation(int *n)
{
    asm("xor eax, eax  \n" /* summation */
        "mov ecx, 0    \n" /* counter */
        "mov edx, %0   \n" /* end */
        "_loop:        \n"
        "add eax, ecx  \n"
        "inc ecx       \n"
        "cmp ecx, edx  \n"
        "jle _loop     \n"
        "mov %0, eax   \n" : "=r" (*n) : "r" (*n) : "eax");
}

void asm_summation2(int *n)
{
    if (*n <= 1)
    {
        *n = 0;
        return;
    }

    asm("xor eax, eax  \n" /* summation */
        "mov ecx, %0   \n" /* counter */
        "_loop2:       \n"
        "add eax, ecx  \n"
        "loop _loop2   \n"
        "mov %0, eax   \n" : "=r" (*n) : "r" (*n) : "eax");
}

void asm_rotate(int *n)
{
    asm("mov eax, %0   \n"
        "rol eax, 1    \n"
        "mov %0, eax   \n" : "=r" (*n) : "r" (*n) : "eax");
}

int main(int argc, char *argv[])
{
    int i = 12;



    /* output, input (r) = register, clobbered */
    asm("mov eax, %0\n" : : "r" (i) : "eax");
    printf("i is: %d\n", i);

    int j = -1;
    asm_summation2(&j);
    printf("summation is %d\n", j);

    int k = 2;
    asm_rotate(&k);
    printf("k = %d\n", k);

    return 0;
}