R2023.05.1 - aktualizacja wykładu
This commit is contained in:
+12
-3
@@ -1,13 +1,22 @@
|
|||||||
all: fibb_32 fibb_64 second
|
all: fibb_32 fibb_64 fibb_32_inline fibb_asm fibb_bin second
|
||||||
|
|
||||||
fibb_32: fibb_c.c fibb32.s timestamp32.s
|
fibb_32: fibb_c.c fibb32.s timestamp32.s
|
||||||
gcc -m32 fibb32.s timestamp32.s fibb_c.c -o fibb_32
|
gcc -m32 fibb32.s timestamp32.s fibb_c.c -o fibb_32
|
||||||
|
|
||||||
fibb_64: fibb_c.c fibb64.s timestamp64.s
|
fibb_64: fibb_c.c fibb64.s timestamp64.s
|
||||||
gcc -no-pie fibb64.s timestamp64.s fibb_c.c -o fibb_64
|
gcc fibb64.s timestamp64.s fibb_c.c -o fibb_64
|
||||||
|
|
||||||
|
fibb_32_inline: fibb_inline.c fibb32.s
|
||||||
|
gcc -m32 fibb32.s fibb_inline.c -o fibb_32_inline
|
||||||
|
|
||||||
|
fibb_asm: fibb_asm.s fibb64.s
|
||||||
|
gcc fibb64.s fibb_asm.s -o fibb_asm
|
||||||
|
|
||||||
|
fibb_bin: fibb_bin.s fibb64.s
|
||||||
|
gcc -no-pie fibb64.s fibb_bin.s -o fibb_bin
|
||||||
|
|
||||||
second: second.c timestamp32.s
|
second: second.c timestamp32.s
|
||||||
gcc -m32 timestamp32.s second.c -o second
|
gcc -m32 timestamp32.s second.c -o second
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm fibb_32 fibb_64 second
|
rm fibb_32 fibb_64 fibb_32_inline fibb_asm fibb_bin second
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
# Jan Potocki 2023
|
||||||
|
# Wersja przystosowana do zbudowania jako PIE (Position Independent Executable)
|
||||||
|
|
||||||
|
# Definicje numerow funkcji systemowych i ich parametrow
|
||||||
|
SYSEXIT64 = 60
|
||||||
|
|
||||||
|
# Stale okreslajace rozmiar przetwarzanych danych
|
||||||
|
word_length = 8
|
||||||
|
|
||||||
|
.global main
|
||||||
|
|
||||||
|
# Segment niezainicjalizowanych danych
|
||||||
|
.bss
|
||||||
|
element: .space word_length
|
||||||
|
|
||||||
|
# Segment zainicjalizowanych danych
|
||||||
|
.data
|
||||||
|
format_s: .asciz "%lu"
|
||||||
|
format_p: .asciz "%lu\n"
|
||||||
|
|
||||||
|
# Segment kodu
|
||||||
|
.text
|
||||||
|
|
||||||
|
main:
|
||||||
|
# To jest potrzebne zeby printf i scanf nie konczyly sie segfaultem
|
||||||
|
push %rbp
|
||||||
|
mov %rsp, %rbp
|
||||||
|
|
||||||
|
lea format_s(%rip), %rdi # Adresowanie wzgledem rip, wprowadzone w x86-64
|
||||||
|
#mov $format_s, %rdi # ...tu nam wystarczy sam adres
|
||||||
|
lea element(%rip), %rsi
|
||||||
|
#mov $element, %rsi
|
||||||
|
mov $0, %rax # Nie ma argumentow dla scanf() w rejestrach xmm
|
||||||
|
call scanf
|
||||||
|
|
||||||
|
|
||||||
|
lea element(%rip), %r8 # Tutaj trzeba jakis posredni rejestr...
|
||||||
|
movq (%r8), %rdi # ...zeby odwolac sie do wartosci
|
||||||
|
#movq element, %rdi
|
||||||
|
call fibb
|
||||||
|
|
||||||
|
lea format_p(%rip), %rdi
|
||||||
|
#mov $format_p, %rdi
|
||||||
|
mov %rax, %rsi # rax - wartosc zwrocona przez fibb()
|
||||||
|
mov $0, %rax # Nie ma argumentow dla printf() w rejestrach xmm
|
||||||
|
call printf
|
||||||
|
|
||||||
|
mov $SYSEXIT64, %rax
|
||||||
|
mov $0, %rdi
|
||||||
|
syscall
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
# Jan Potocki 2020
|
||||||
|
# Przyklad wywolania:
|
||||||
|
# echo 08 | xxd -p -r | ./fibb_asm | hexdump
|
||||||
|
|
||||||
|
# Definicje numerow funkcji systemowych i ich parametrow
|
||||||
|
SYSEXIT64 = 60
|
||||||
|
SYSREAD = 0
|
||||||
|
SYSWRITE = 1
|
||||||
|
STDIN = 0
|
||||||
|
STDOUT = 1
|
||||||
|
|
||||||
|
# Stale okreslajace rozmiar przetwarzanych danych
|
||||||
|
word_length = 8
|
||||||
|
|
||||||
|
.global main
|
||||||
|
|
||||||
|
# Segment niezainicjalizowanych danych
|
||||||
|
.bss
|
||||||
|
result: .space word_length
|
||||||
|
|
||||||
|
# Segment zainicjalizowanych danych
|
||||||
|
.data
|
||||||
|
term: .zero word_length
|
||||||
|
|
||||||
|
# Segment kodu
|
||||||
|
.text
|
||||||
|
|
||||||
|
main:
|
||||||
|
mov $SYSREAD, %rax
|
||||||
|
mov $STDIN, %rdi
|
||||||
|
mov $term, %rsi
|
||||||
|
mov $word_length, %rdx
|
||||||
|
syscall
|
||||||
|
|
||||||
|
movq term, %rdi
|
||||||
|
call fibb
|
||||||
|
movq %rax, result
|
||||||
|
|
||||||
|
mov $SYSWRITE, %rax
|
||||||
|
mov $STDOUT, %rdi
|
||||||
|
mov $result, %rsi
|
||||||
|
mov $word_length, %rdx
|
||||||
|
syscall
|
||||||
|
|
||||||
|
mov $SYSEXIT64, %rax
|
||||||
|
mov $0, %rdi
|
||||||
|
syscall
|
||||||
+7
-5
@@ -1,5 +1,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// Jan Potocki 2020
|
||||||
|
|
||||||
unsigned long fibb(unsigned long n);
|
unsigned long fibb(unsigned long n);
|
||||||
unsigned long long timestamp();
|
unsigned long long timestamp();
|
||||||
|
|
||||||
@@ -7,15 +9,15 @@ int main()
|
|||||||
{
|
{
|
||||||
unsigned long term, result;
|
unsigned long term, result;
|
||||||
unsigned long long tstamp1, tstamp2;
|
unsigned long long tstamp1, tstamp2;
|
||||||
|
|
||||||
scanf("%lu", &term);
|
scanf("%lu", &term);
|
||||||
|
|
||||||
tstamp1 = timestamp();
|
tstamp1 = timestamp();
|
||||||
result = fibb(term);
|
result = fibb(term);
|
||||||
tstamp2 = timestamp();
|
tstamp2 = timestamp();
|
||||||
|
|
||||||
printf("%lu\n", result);
|
printf("Result: %lu\n", result);
|
||||||
printf("Cycles: %llu\n", tstamp2-tstamp1);
|
printf("Cycles: %llu\n", tstamp2-tstamp1);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// Jan Potocki 2020
|
||||||
|
|
||||||
|
unsigned long fibb(unsigned long n);
|
||||||
|
|
||||||
|
unsigned long long timestamp()
|
||||||
|
{
|
||||||
|
unsigned long long tsc;
|
||||||
|
|
||||||
|
asm volatile(
|
||||||
|
"xor %%eax, %%eax\n"
|
||||||
|
"cpuid\n"
|
||||||
|
"rdtsc"
|
||||||
|
: "=A" (tsc)
|
||||||
|
:
|
||||||
|
: "ebx", "ecx"
|
||||||
|
);
|
||||||
|
|
||||||
|
return tsc;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
unsigned long term, result;
|
||||||
|
unsigned long long tstamp1, tstamp2;
|
||||||
|
|
||||||
|
scanf("%lu", &term);
|
||||||
|
|
||||||
|
tstamp1 = timestamp();
|
||||||
|
result = fibb(term);
|
||||||
|
tstamp2 = timestamp();
|
||||||
|
|
||||||
|
printf("Result: %lu\n", result);
|
||||||
|
printf("Cycles: %llu\n", tstamp2-tstamp1);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user