From c616d4e3c3a3abfcfeb5e9d96ddfbf7d3111603a Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Wed, 19 Jun 2024 22:13:49 +0200 Subject: Engage! --- LICENSE | 12 ++++++++++++ README.md | 0 c-asm/.clang-format | 4 ++++ c-asm/Makefile | 13 +++++++++++++ c-asm/main.c | 11 +++++++++++ c-asm/sum.s | 11 +++++++++++ c-embed/.clang-format | 4 ++++ c-embed/Makefile | 6 ++++++ c-embed/main.c | 19 +++++++++++++++++++ c-embed/test.h | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ c-embed/test.txt | 8 ++++++++ c-signals/.clang-format | 4 ++++ c-signals/Makefile | 5 +++++ c-signals/main.c | 21 +++++++++++++++++++++ shell.nix | 9 +++++++++ 15 files changed, 176 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 c-asm/.clang-format create mode 100644 c-asm/Makefile create mode 100644 c-asm/main.c create mode 100644 c-asm/sum.s create mode 100644 c-embed/.clang-format create mode 100644 c-embed/Makefile create mode 100644 c-embed/main.c create mode 100644 c-embed/test.h create mode 100644 c-embed/test.txt create mode 100644 c-signals/.clang-format create mode 100644 c-signals/Makefile create mode 100644 c-signals/main.c create mode 100644 shell.nix diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4700f89 --- /dev/null +++ b/LICENSE @@ -0,0 +1,12 @@ +Copyright (C) 2024, Mitja Felicijan + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/c-asm/.clang-format b/c-asm/.clang-format new file mode 100644 index 0000000..5f0e585 --- /dev/null +++ b/c-asm/.clang-format @@ -0,0 +1,4 @@ +BasedOnStyle: Chromium +ColumnLimit: 120 +IndentWidth: 4 + diff --git a/c-asm/Makefile b/c-asm/Makefile new file mode 100644 index 0000000..907e50c --- /dev/null +++ b/c-asm/Makefile @@ -0,0 +1,13 @@ +all: a.out + +sum.o: + as sum.s -o sum.o + +a.out: sum.o + gcc main.c sum.o + +clean: + -rm a.out *.o + +tags: + ctags -R * diff --git a/c-asm/main.c b/c-asm/main.c new file mode 100644 index 0000000..56b9bc5 --- /dev/null +++ b/c-asm/main.c @@ -0,0 +1,11 @@ +#include + +int sum(int a, int b); + +int main() { + for (int i = 0; i < 10; ++i) { + printf("SUM of 5+%d is %d\n", i, sum(5, i)); + } + + return 0; +} diff --git a/c-asm/sum.s b/c-asm/sum.s new file mode 100644 index 0000000..8b943b9 --- /dev/null +++ b/c-asm/sum.s @@ -0,0 +1,11 @@ +.intel_syntax noprefix + +.global sum + +.text + +sum: + add rdi, rsi + mov rax, rdi + ret + diff --git a/c-embed/.clang-format b/c-embed/.clang-format new file mode 100644 index 0000000..5f0e585 --- /dev/null +++ b/c-embed/.clang-format @@ -0,0 +1,4 @@ +BasedOnStyle: Chromium +ColumnLimit: 120 +IndentWidth: 4 + diff --git a/c-embed/Makefile b/c-embed/Makefile new file mode 100644 index 0000000..29e4f2d --- /dev/null +++ b/c-embed/Makefile @@ -0,0 +1,6 @@ +a.out: + xxd -i test.txt > test.h + gcc -Wall -Werror -Wpedantic main.c + +clean: + -rm a.out diff --git a/c-embed/main.c b/c-embed/main.c new file mode 100644 index 0000000..c0d2f59 --- /dev/null +++ b/c-embed/main.c @@ -0,0 +1,19 @@ +#include + +#include "test.h" + +int main(void) { + printf("Testing embedding of files into binary.\n"); + + for (unsigned int i = 0; i < test_txt_len; i++) { + printf("%02x ", test_txt[i]); + } + printf("\n\n"); + + for (unsigned int i = 0; i < test_txt_len; i++) { + printf("%c", test_txt[i]); + } + printf("\n\n"); + + return 0; +} diff --git a/c-embed/test.h b/c-embed/test.h new file mode 100644 index 0000000..cfdfa3a --- /dev/null +++ b/c-embed/test.h @@ -0,0 +1,49 @@ +unsigned char test_txt[] = { + 0x54, 0x68, 0x65, 0x20, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x72, 0x75, + 0x6c, 0x65, 0x20, 0x69, 0x73, 0x20, 0x74, 0x79, 0x70, 0x69, 0x63, 0x61, + 0x6c, 0x20, 0x6f, 0x66, 0x20, 0x71, 0x75, 0x61, 0x6e, 0x74, 0x75, 0x6d, + 0x20, 0x70, 0x68, 0x79, 0x73, 0x69, 0x63, 0x73, 0x2e, 0x20, 0x49, 0x74, + 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x73, 0x20, 0x66, 0x72, 0x6f, + 0x6d, 0x20, 0x74, 0x68, 0x65, 0x20, 0x70, 0x6f, 0x73, 0x74, 0x75, 0x6c, + 0x61, 0x74, 0x65, 0x20, 0x6f, 0x66, 0x0a, 0x75, 0x6e, 0x69, 0x74, 0x61, + 0x72, 0x79, 0x20, 0x65, 0x76, 0x6f, 0x6c, 0x75, 0x74, 0x69, 0x6f, 0x6e, + 0x2e, 0x20, 0x54, 0x68, 0x65, 0x20, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, + 0x20, 0x72, 0x75, 0x6c, 0x65, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x63, + 0x6c, 0x61, 0x73, 0x73, 0x69, 0x63, 0x61, 0x6c, 0x20, 0x72, 0x75, 0x6c, + 0x65, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x64, 0x64, + 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x6f, 0x66, 0x0a, 0x70, 0x72, 0x6f, + 0x62, 0x61, 0x62, 0x69, 0x6c, 0x69, 0x74, 0x69, 0x65, 0x73, 0x2e, 0x20, + 0x49, 0x74, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x73, 0x20, 0x66, + 0x72, 0x6f, 0x6d, 0x20, 0x64, 0x65, 0x63, 0x6f, 0x68, 0x65, 0x72, 0x65, + 0x6e, 0x63, 0x65, 0x20, 0x74, 0x68, 0x72, 0x6f, 0x75, 0x67, 0x68, 0x20, + 0x6f, 0x62, 0x73, 0x65, 0x72, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2e, + 0x20, 0x57, 0x68, 0x65, 0x6e, 0x0a, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6d, + 0x65, 0x64, 0x69, 0x61, 0x74, 0x65, 0x20, 0x73, 0x74, 0x61, 0x74, 0x65, + 0x73, 0x20, 0x61, 0x72, 0x65, 0x20, 0x6f, 0x62, 0x73, 0x65, 0x72, 0x76, + 0x65, 0x64, 0x2c, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6f, 0x62, 0x73, 0x65, + 0x72, 0x76, 0x65, 0x64, 0x20, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x20, + 0x69, 0x73, 0x20, 0x72, 0x65, 0x70, 0x72, 0x65, 0x73, 0x65, 0x6e, 0x74, + 0x65, 0x64, 0x20, 0x62, 0x79, 0x20, 0x61, 0x0a, 0x6d, 0x69, 0x78, 0x74, + 0x75, 0x72, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x73, 0x74, 0x61, 0x74, 0x65, + 0x73, 0x2e, 0x20, 0x54, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, 0x73, + 0x69, 0x63, 0x61, 0x6c, 0x20, 0x72, 0x75, 0x6c, 0x65, 0x20, 0x6f, 0x66, + 0x20, 0x61, 0x64, 0x64, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x6f, 0x66, + 0x20, 0x70, 0x72, 0x6f, 0x62, 0x61, 0x62, 0x69, 0x6c, 0x69, 0x74, 0x69, + 0x65, 0x73, 0x20, 0x6d, 0x75, 0x73, 0x74, 0x20, 0x74, 0x68, 0x65, 0x6e, + 0x20, 0x62, 0x65, 0x0a, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x65, 0x64, 0x2e, + 0x20, 0x45, 0x6e, 0x74, 0x61, 0x6e, 0x67, 0x6c, 0x65, 0x6d, 0x65, 0x6e, + 0x74, 0x20, 0x62, 0x79, 0x20, 0x6f, 0x62, 0x73, 0x65, 0x72, 0x76, 0x61, + 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x74, 0x68, 0x75, 0x73, 0x20, 0x65, 0x78, + 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x73, 0x20, 0x46, 0x65, 0x79, 0x6e, 0x6d, + 0x61, 0x6e, 0x27, 0x73, 0x20, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x20, + 0x72, 0x75, 0x6c, 0x65, 0x2e, 0x20, 0x54, 0x68, 0x69, 0x73, 0x0a, 0x65, + 0x78, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, + 0x46, 0x65, 0x79, 0x6e, 0x6d, 0x61, 0x6e, 0x20, 0x52, 0x75, 0x6c, 0x65, + 0x73, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x63, 0x61, 0x6c, 0x63, 0x75, 0x6c, + 0x61, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x70, 0x72, 0x6f, 0x62, 0x61, 0x62, + 0x69, 0x6c, 0x69, 0x74, 0x69, 0x65, 0x73, 0x20, 0x69, 0x6e, 0x20, 0x71, + 0x75, 0x61, 0x6e, 0x74, 0x75, 0x6d, 0x20, 0x70, 0x68, 0x79, 0x73, 0x69, + 0x63, 0x73, 0x0a, 0x28, 0x46, 0x65, 0x79, 0x6e, 0x6d, 0x61, 0x6e, 0x20, + 0x31, 0x39, 0x36, 0x36, 0x29, 0x2e, 0x0a +}; +unsigned int test_txt_len = 547; diff --git a/c-embed/test.txt b/c-embed/test.txt new file mode 100644 index 0000000..a3cac17 --- /dev/null +++ b/c-embed/test.txt @@ -0,0 +1,8 @@ +The first rule is typical of quantum physics. It results from the postulate of +unitary evolution. The second rule is a classical rule of the addition of +probabilities. It results from decoherence through observation. When +intermediate states are observed, the observed system is represented by a +mixture of states. The classical rule of addition of probabilities must then be +applied. Entanglement by observation thus explains Feynman's second rule. This +explains the Feynman Rules for calculating probabilities in quantum physics +(Feynman 1966). diff --git a/c-signals/.clang-format b/c-signals/.clang-format new file mode 100644 index 0000000..5f0e585 --- /dev/null +++ b/c-signals/.clang-format @@ -0,0 +1,4 @@ +BasedOnStyle: Chromium +ColumnLimit: 120 +IndentWidth: 4 + diff --git a/c-signals/Makefile b/c-signals/Makefile new file mode 100644 index 0000000..afd71c3 --- /dev/null +++ b/c-signals/Makefile @@ -0,0 +1,5 @@ +a.out: + gcc -Wall -Werror -Wpedantic main.c + +clean: + -rm a.out diff --git a/c-signals/main.c b/c-signals/main.c new file mode 100644 index 0000000..a471016 --- /dev/null +++ b/c-signals/main.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include + +void handle_signal(int signal) { + printf("Signal received %d\n", signal); +} + +// use “kill -10 pidof a.out“ +// https://www.man7.org/linux/man-pages/man7/signal.7.html +int main(void) { + signal(SIGUSR1, handle_signal); + signal(SIGUSR2, handle_signal); + + printf("Waiting for signals...\n"); + for (;;) + sleep(1); + + return 0; +} diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..ae3a692 --- /dev/null +++ b/shell.nix @@ -0,0 +1,9 @@ +{ pkgs ? import {} }: + pkgs.mkShell { + nativeBuildInputs = with pkgs.buildPackages; [ + binutils + gnumake + nasm + tinycc + ]; +} -- cgit v1.2.3