summaryrefslogtreecommitdiffstats
path: root/asm2.c
diff options
context:
space:
mode:
Diffstat (limited to 'asm2.c')
-rw-r--r--asm2.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/asm2.c b/asm2.c
new file mode 100644
index 0000000..7621b78
--- /dev/null
+++ b/asm2.c
@@ -0,0 +1,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;
+}
+