Bladeren bron

R2023.05.1 - aktualizacja wykładu

Jan Potocki 1 jaar geleden
bovenliggende
commit
9a28acb0c9
5 gewijzigde bestanden met toevoegingen van 154 en 8 verwijderingen
  1. 12
    3
      fibb_tsc/Makefile
  2. 50
    0
      fibb_tsc/fibb_asm.s
  3. 47
    0
      fibb_tsc/fibb_bin.s
  4. 7
    5
      fibb_tsc/fibb_c.c
  5. 38
    0
      fibb_tsc/fibb_inline.c

+ 12
- 3
fibb_tsc/Makefile Bestand weergeven

@@ -1,13 +1,22 @@
1
-all: fibb_32 fibb_64 second
1
+all: fibb_32 fibb_64 fibb_32_inline fibb_asm fibb_bin second
2 2
 
3 3
 fibb_32: fibb_c.c fibb32.s timestamp32.s
4 4
 	gcc -m32 fibb32.s timestamp32.s fibb_c.c -o fibb_32
5 5
 
6 6
 fibb_64: fibb_c.c fibb64.s timestamp64.s
7
-	gcc -no-pie fibb64.s timestamp64.s fibb_c.c -o fibb_64
7
+	gcc fibb64.s timestamp64.s fibb_c.c -o fibb_64
8
+
9
+fibb_32_inline: fibb_inline.c fibb32.s
10
+	gcc -m32 fibb32.s fibb_inline.c -o fibb_32_inline
11
+
12
+fibb_asm: fibb_asm.s fibb64.s
13
+	gcc fibb64.s fibb_asm.s -o fibb_asm
14
+
15
+fibb_bin: fibb_bin.s fibb64.s
16
+	gcc -no-pie fibb64.s fibb_bin.s -o fibb_bin
8 17
 
9 18
 second: second.c timestamp32.s
10 19
 	gcc -m32 timestamp32.s second.c -o second
11 20
 
12 21
 clean:
13
-	rm fibb_32 fibb_64 second
22
+	rm fibb_32 fibb_64 fibb_32_inline fibb_asm fibb_bin second

+ 50
- 0
fibb_tsc/fibb_asm.s Bestand weergeven

@@ -0,0 +1,50 @@
1
+# Jan Potocki 2023
2
+# Wersja przystosowana do zbudowania jako PIE (Position Independent Executable)
3
+
4
+# Definicje numerow funkcji systemowych i ich parametrow
5
+SYSEXIT64 = 60
6
+
7
+# Stale okreslajace rozmiar przetwarzanych danych
8
+word_length = 8
9
+
10
+.global main
11
+
12
+# Segment niezainicjalizowanych danych
13
+.bss
14
+element: .space word_length
15
+
16
+# Segment zainicjalizowanych danych
17
+.data
18
+format_s: .asciz "%lu"
19
+format_p: .asciz "%lu\n"
20
+
21
+# Segment kodu
22
+.text
23
+
24
+main:
25
+# To jest potrzebne zeby printf i scanf nie konczyly sie segfaultem
26
+push %rbp
27
+mov %rsp, %rbp
28
+
29
+lea format_s(%rip), %rdi    # Adresowanie wzgledem rip, wprowadzone w x86-64
30
+#mov $format_s, %rdi        # ...tu nam wystarczy sam adres
31
+lea element(%rip), %rsi
32
+#mov $element, %rsi
33
+mov $0, %rax                # Nie ma argumentow dla scanf() w rejestrach xmm
34
+call scanf
35
+
36
+
37
+lea element(%rip), %r8      # Tutaj trzeba jakis posredni rejestr...
38
+movq (%r8), %rdi            # ...zeby odwolac sie do wartosci
39
+#movq element, %rdi
40
+call fibb
41
+
42
+lea format_p(%rip), %rdi
43
+#mov $format_p, %rdi
44
+mov %rax, %rsi              # rax - wartosc zwrocona przez fibb()
45
+mov $0, %rax                # Nie ma argumentow dla printf() w rejestrach xmm
46
+call printf
47
+
48
+mov $SYSEXIT64, %rax
49
+mov $0, %rdi
50
+syscall

+ 47
- 0
fibb_tsc/fibb_bin.s Bestand weergeven

@@ -0,0 +1,47 @@
1
+# Jan Potocki 2020
2
+# Przyklad wywolania:
3
+# echo 08 | xxd -p -r | ./fibb_asm | hexdump
4
+
5
+# Definicje numerow funkcji systemowych i ich parametrow
6
+SYSEXIT64 = 60
7
+SYSREAD = 0
8
+SYSWRITE = 1
9
+STDIN = 0
10
+STDOUT = 1
11
+
12
+# Stale okreslajace rozmiar przetwarzanych danych
13
+word_length = 8
14
+
15
+.global main
16
+
17
+# Segment niezainicjalizowanych danych
18
+.bss
19
+result: .space word_length
20
+
21
+# Segment zainicjalizowanych danych
22
+.data
23
+term: .zero word_length
24
+
25
+# Segment kodu
26
+.text
27
+
28
+main:
29
+mov $SYSREAD, %rax
30
+mov $STDIN, %rdi
31
+mov $term, %rsi
32
+mov $word_length, %rdx
33
+syscall
34
+
35
+movq term, %rdi
36
+call fibb
37
+movq %rax, result
38
+
39
+mov $SYSWRITE, %rax
40
+mov $STDOUT, %rdi
41
+mov $result, %rsi
42
+mov $word_length, %rdx
43
+syscall
44
+
45
+mov $SYSEXIT64, %rax
46
+mov $0, %rdi
47
+syscall

+ 7
- 5
fibb_tsc/fibb_c.c Bestand weergeven

@@ -1,5 +1,7 @@
1 1
 #include <stdio.h>
2 2
 
3
+// Jan Potocki 2020
4
+
3 5
 unsigned long fibb(unsigned long n);
4 6
 unsigned long long timestamp();
5 7
 
@@ -7,15 +9,15 @@ int main()
7 9
 {
8 10
     unsigned long term, result;
9 11
     unsigned long long tstamp1, tstamp2;
10
-    
12
+
11 13
     scanf("%lu", &term);
12
-    
14
+
13 15
     tstamp1 = timestamp();
14 16
     result = fibb(term);
15 17
     tstamp2 = timestamp();
16
-    
17
-    printf("%lu\n", result);
18
+
19
+    printf("Result: %lu\n", result);
18 20
     printf("Cycles: %llu\n", tstamp2-tstamp1);
19
-    
21
+
20 22
     return 0;
21 23
 }

+ 38
- 0
fibb_tsc/fibb_inline.c Bestand weergeven

@@ -0,0 +1,38 @@
1
+#include <stdio.h>
2
+
3
+// Jan Potocki 2020
4
+
5
+unsigned long fibb(unsigned long n);
6
+
7
+unsigned long long timestamp()
8
+{
9
+    unsigned long long tsc;
10
+
11
+    asm volatile(
12
+        "xor %%eax, %%eax\n"
13
+        "cpuid\n"
14
+        "rdtsc"
15
+        : "=A" (tsc)
16
+        :
17
+        : "ebx", "ecx"
18
+    );
19
+
20
+    return tsc;
21
+}
22
+
23
+int main()
24
+{
25
+    unsigned long term, result;
26
+    unsigned long long tstamp1, tstamp2;
27
+
28
+    scanf("%lu", &term);
29
+
30
+    tstamp1 = timestamp();
31
+    result = fibb(term);
32
+    tstamp2 = timestamp();
33
+
34
+    printf("Result: %lu\n", result);
35
+    printf("Cycles: %llu\n", tstamp2-tstamp1);
36
+
37
+    return 0;
38
+}

Laden…
Annuleren
Opslaan