summaryrefslogtreecommitdiff
path: root/3
diff options
context:
space:
mode:
authorMike Vink <mike@pionative.com>2024-07-09 09:06:58 +0200
committerMike Vink <mike@pionative.com>2024-07-09 09:06:58 +0200
commit7ae6aa6a64f3f697edb90bb573862bf31dd14e69 (patch)
treee80f43394fd5814e30f98a7faa189f8f49424681 /3
parent8092f4c334db547ced59d6f439b558dad35e1ab2 (diff)
chapter 3: reading excercises + attacklab
Diffstat (limited to '3')
-rw-r--r--3/11_floating_point/50_fcvt2.c27
-rw-r--r--3/11_floating_point/51_cvt.c18
-rw-r--r--3/11_floating_point/52_args.c20
-rw-r--r--3/11_floating_point/53_funct1.c29
-rw-r--r--3/11_floating_point/54_funct2.c16
-rw-r--r--3/11_floating_point/55_lc3.c7
-rw-r--r--3/11_floating_point/56_simplefun.c25
-rw-r--r--3/11_floating_point/57_funct3.c27
8 files changed, 169 insertions, 0 deletions
diff --git a/3/11_floating_point/50_fcvt2.c b/3/11_floating_point/50_fcvt2.c
new file mode 100644
index 0000000..052462e
--- /dev/null
+++ b/3/11_floating_point/50_fcvt2.c
@@ -0,0 +1,27 @@
+#include <stdio.h>
+
+// ip = rdi
+// fp = rsi
+// dp = rdx
+// l = rcx
+// fcvt:
+// movl (%rdi), %eax eax<-int value of ip to eax
+// vmovss (%rsi), %xmm0 xmm0<- move int value to float (vmov signed single?)
+// vcvttsd2si (%rdx), %r8d r8d<- double value to signed integer?
+// movl %r8d, (%rdi) *ip<- set pointer to truncated value of rdx
+// vcvttsd2ss %eax, %xmm1, %xmm1 xmm1<- convert int to float
+// vmovss %xmm1, (%rsi) fp<-*ip
+// vcvtsi2sdq %rcx, %xmm1, %xmm1 xmm1<- convert long to double
+// vmovsd %xmm1, (%rdx) dp<- l
+// vunpcklps %xmm0, %xmm0, %xmm0
+// vcvtps2pd %xmm0, %xmm0
+// ret
+double fcvt2(int *ip, float *fp, double *dp, long l)
+{
+ int i = *ip; float f = *fp; double d = *dp;
+
+ *ip = (int) d;
+ *fp = (float) i;
+ *dp = (double) l;
+ return (double) f;
+}
diff --git a/3/11_floating_point/51_cvt.c b/3/11_floating_point/51_cvt.c
new file mode 100644
index 0000000..053a657
--- /dev/null
+++ b/3/11_floating_point/51_cvt.c
@@ -0,0 +1,18 @@
+typedef int dest_t
+typedef int src_t
+
+dest_t cvt(src_t x)
+{
+ dest_t y = (dest_t) x;
+ return y;
+}
+
+// T_x T_y Instruction(s)
+//
+// long double vcvtsi2sdq %rdi, %xmm0
+// double int vcvttsd2si %xmm0, %rdi
+// double float vmovddup %xmm0, %xmm0
+// vcvtpd2psx %xmm0, %xmm0
+// long float vcvtsi2ssq %rdi, %xmm0
+// float long vcvttss2siq %xmm0, %rdi
+//
diff --git a/3/11_floating_point/52_args.c b/3/11_floating_point/52_args.c
new file mode 100644
index 0000000..a21a235
--- /dev/null
+++ b/3/11_floating_point/52_args.c
@@ -0,0 +1,20 @@
+double g1(double a, long b, float c, int d);
+// a in xmm0
+// b in %rdi
+// c in xmm1
+// d in %rsi
+double g2(int a, double *b, float *c, long d);
+// a in %edi
+// b in %rsi
+// c in %rdx
+// d in %rcx
+double g3(double *a, double b, int c, float d);
+// a in %rdi
+// b in %rsi
+// c in %edx
+// d in %xmm0
+double g4(float a, int *b, float c, double d);
+// a in %xmm0
+// b in %rdi
+// c in %xmm1
+// d in %rsi
diff --git a/3/11_floating_point/53_funct1.c b/3/11_floating_point/53_funct1.c
new file mode 100644
index 0000000..8147b62
--- /dev/null
+++ b/3/11_floating_point/53_funct1.c
@@ -0,0 +1,29 @@
+typedef arg1_t int;
+typedef arg2_t double;
+typedef arg3_t double;
+
+typedef arg4_t double;
+double funct1(arg1_t p, arg2_t q, arg3_t r, double s)
+{
+ return p/(q+r) - s;
+}
+// funct 1:
+// vcvtsi2ssq %rsi, %xmm2, %xmm2 convert long to double?
+// vaddss %xmm0, %xmm2, %xmm0 xmm0 <- xmm2 + xmm0 (q+r)
+// vcvtsi2ss %edi, %xmm2, %xmm2 convert to float (p)
+// vdivss %xmm0, %xmm2, %xmm0 xmm0 <- xmm2 (p float) / xmm0 (q+r float)
+// vunpcklps %xmm0, %xmm0, %xmm0 convert float to double?
+// vcvtps2pd %xmm0, %xmm0 convert float to double?
+// vsubsd %xmm1, %xmm0, %xmm0 xmm0 <- xmm0 - (double) s?
+// ret
+
+// s must be in xmm1, it's only used at the end to subtract from xmm0, it must also be double since vsubsd is used
+//
+// p q r q+r p/(q+r)
+// rdi, int, since it's xmm0, float rsi, long float, xmm0 float, xmm0
+// converted
+// on line 3
+//
+// rdi, int, since it's rsi, long xmm0, float float, xmm0 float, xmm0
+// converted
+// on line 3
diff --git a/3/11_floating_point/54_funct2.c b/3/11_floating_point/54_funct2.c
new file mode 100644
index 0000000..66c26c8
--- /dev/null
+++ b/3/11_floating_point/54_funct2.c
@@ -0,0 +1,16 @@
+double funct2(double w, int x, float y, long z)
+{
+ return x*y - w/z;
+}
+
+// double funct2(double w, int x, float y, long z)
+// w in xmm0, x in edi, y in xmm1, z in rsi
+// funct2:
+// vcvtsi2ss %edi, %xmm2, %xmm2 // convert int to float
+// vmulss %xmm1, %xmm2, %xmm1 // xmm1<- x*y, multiply sencond and third float
+// vunpcklps %xmm1, %xmm1, %xmm1 // do convert float to double?
+// vcvtps2pd %xmm1, %xmm2 // do convert float to double?
+// vcvtsi2sdq %rsi, %xmm1, %xmm1 // xmm1 <- z, convert long to double?
+// vdivsd %xmm1, %xmm0, %xmm0 // xmm0<- w/z
+// vsubsd %xmm0, %xmm2, %xmm0 // xmm0<- (x*y) - w/z
+// ret
diff --git a/3/11_floating_point/55_lc3.c b/3/11_floating_point/55_lc3.c
new file mode 100644
index 0000000..31cd59e
--- /dev/null
+++ b/3/11_floating_point/55_lc3.c
@@ -0,0 +1,7 @@
+// 0000000000000404
+// 0000000000000000000000000000000000000000000000000000 01000000010 0
+//
+// V= 2^(1028 - 1023) * (1 + f)
+// V=2^5
+// V=32
+print("{:064x}".format(1077936128))
diff --git a/3/11_floating_point/56_simplefun.c b/3/11_floating_point/56_simplefun.c
new file mode 100644
index 0000000..66569fa
--- /dev/null
+++ b/3/11_floating_point/56_simplefun.c
@@ -0,0 +1,25 @@
+#define EXPR
+
+double simplefun(double x) {
+ return EXPR(x);
+}
+// A
+// vmovsd .LC1(%rip), %xmm1
+// vandpd %xmm1, xmm0, xmm0
+// .LC1:
+// 1111111111111111111111111111111111111111111111111111 11111111111 0
+//
+// Checks if any bit is set in the arg by anding with all ones
+//
+//
+// B
+// vxorpd %xmm0, %xmm0, %xmm0
+//
+// Sets the argument to zero
+//
+// C
+// vmovsd .LC2(%rip), %xmm1
+// vxorpd xmm1, %xmm0, %xmm0
+// 10000000000000000000000000000000 00000000000000000000000000000000
+//
+// Flipping the sign bit
diff --git a/3/11_floating_point/57_funct3.c b/3/11_floating_point/57_funct3.c
new file mode 100644
index 0000000..5397330
--- /dev/null
+++ b/3/11_floating_point/57_funct3.c
@@ -0,0 +1,27 @@
+double funct3(int *ap, double b, long c, float *dp) {
+ int a = *ap;
+ float d = *dp;
+ if (b <= a) {
+ return (c * d);
+ }
+ return (c + 2 * d);
+}
+
+// ap in rdi, b in xmm0, c in rsi, dp in rdx
+// funct3:
+// vmovss (%rdx), %xmm1 // y <- dp
+// vcvtsi2sd (%rdi), %xmm2, %xmm2 // z <- (double a)
+// vucomisd %xmm2, %xmm0 // compare: (<= b z )
+// jbe .L8 // jump to .L8: if <
+// vcvtsi2ssq %rsi, %xmm0, %xmm0 // x <- (float) c
+// vmulss %xmm1, %xmm0, %xmm1 // y <- (* x y)
+// vunpcklps %xmm1, %xmm1, %xmm1 // float->double
+// vcvtps2pd %xmm1, %xmm0 // float->double
+// ret
+// .L8:
+// vaddss %xmm1, %xmm1, %xmm1 // y <- (+ y y )
+// vcvtsi2ssq %rsi, %xmm0, %xmm0 // x <- (float c)
+// vaddss %xmm1, %xmm0, %xmm0 // x <- x + y
+// vunpcklps %xmm0, %xmm0, %xmm0 // float->double
+// vcvtps2pd %xmm0, %xmm0 // float->double
+// ret