diff --git a/.c-process-arguments.c.swp b/.c-process-arguments.c.swp new file mode 100644 index 0000000..4bedec5 Binary files /dev/null and b/.c-process-arguments.c.swp differ diff --git a/HomeWork1/expression.S b/HomeWork1/expression.S new file mode 100644 index 0000000..099cf1f --- /dev/null +++ b/HomeWork1/expression.S @@ -0,0 +1,10 @@ +.text +.global f + +f: +mul x0, x0, x3 +mul x0, x0, x3 +mul x1, x1, x3 +add x0, x0, x1 +add x0, x0, x2 +ret diff --git a/HomeWork10/.clang-format b/HomeWork10/.clang-format new file mode 100644 index 0000000..a7b6975 --- /dev/null +++ b/HomeWork10/.clang-format @@ -0,0 +1,115 @@ +--- +Language: Cpp +AccessModifierOffset: -2 +AlignAfterOpenBracket: AlwaysBreak +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Right +AlignOperands: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: true +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +BinPackArguments: false +BinPackParameters: false +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: true + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Linux +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + - Regex: '.*' + Priority: 1 +IncludeIsMainRegex: '(Test)?$' +IndentCaseLabels: false +IndentPPDirectives: None +IndentWidth: 4 +IndentWrappedFunctionNames: false +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Left +ReflowComments: false +SortIncludes: true +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Cpp11 +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 4 +UseTab: Never +... diff --git a/HomeWork10/a.out b/HomeWork10/a.out new file mode 100755 index 0000000..460f444 Binary files /dev/null and b/HomeWork10/a.out differ diff --git a/HomeWork10/main (51).c:Zone.Identifier b/HomeWork10/main (51).c:Zone.Identifier new file mode 100644 index 0000000..be537eb Binary files /dev/null and b/HomeWork10/main (51).c:Zone.Identifier differ diff --git a/HomeWork10/main.c b/HomeWork10/main.c new file mode 100644 index 0000000..07afa64 --- /dev/null +++ b/HomeWork10/main.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include + +int main(int argc, char* argv[]) +{ + int curr = 0; + int max = 10; + while (curr < max) { + ++curr; + printf("%i ", curr); + int k = fork(); + if (k != 0) { + break; + } + } + return 0; +} \ No newline at end of file diff --git a/HomeWork10/posix-mmap-find-substrings.c b/HomeWork10/posix-mmap-find-substrings.c new file mode 100644 index 0000000..5e91f73 --- /dev/null +++ b/HomeWork10/posix-mmap-find-substrings.c @@ -0,0 +1,36 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char* argv[]) +{ + char* str_to_find = argv[2]; + int fd = open(argv[1], O_RDONLY); + if (fd == -1) { + return 0; + } + struct stat st; + fstat(fd, &st); + if (st.st_size == 0) { + return 0; + } + char* mmap_begin = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + char* mmap_iterator = mmap_begin; + int64_t len = st.st_size; + while ((mmap_iterator = + memmem(mmap_iterator, len, str_to_find, strlen(str_to_find))) != + NULL) { + int64_t diff = mmap_iterator - mmap_begin; + len = (int64_t)st.st_size - diff; + printf("%li ", diff); + mmap_iterator++; + } + munmap(mmap_begin, st.st_size); + close(fd); + return 0; +} diff --git a/HomeWork10/posix-mmap-find-substrings.c:Zone.Identifier b/HomeWork10/posix-mmap-find-substrings.c:Zone.Identifier new file mode 100644 index 0000000..adce20d --- /dev/null +++ b/HomeWork10/posix-mmap-find-substrings.c:Zone.Identifier @@ -0,0 +1,3 @@ +[ZoneTransfer] +ZoneId=3 +HostUrl=https://fpmi.yajudge.ru/ diff --git a/HomeWork10/posix-mmap-print-list.c b/HomeWork10/posix-mmap-print-list.c new file mode 100644 index 0000000..56bbc9b --- /dev/null +++ b/HomeWork10/posix-mmap-print-list.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef struct Item { + int value; + uint32_t next_pointer; +} item_t; + +int main(int argc, char* argv[]) +{ + struct stat st; + char* file = argv[1]; + int fd = open(file, O_RDONLY); + if (fd == -1) { + return 1; + } + lstat(file, &st); + if (st.st_size == 0) { + return 2; + } + void* ptr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + item_t* item = (item_t*)ptr; + printf("%d", item->value); + while (item->next_pointer != 0) { + item = (item_t*)(ptr + item->next_pointer); + printf("%s", " "); + printf("%d", item->value); + } + munmap(ptr, st.st_size); + close(fd); + return 0; +} \ No newline at end of file diff --git a/HomeWork10/posix-mmap-print-list.c:Zone.Identifier b/HomeWork10/posix-mmap-print-list.c:Zone.Identifier new file mode 100644 index 0000000..adce20d --- /dev/null +++ b/HomeWork10/posix-mmap-print-list.c:Zone.Identifier @@ -0,0 +1,3 @@ +[ZoneTransfer] +ZoneId=3 +HostUrl=https://fpmi.yajudge.ru/ diff --git a/HomeWork10/test.txt b/HomeWork10/test.txt new file mode 100644 index 0000000..f839a19 --- /dev/null +++ b/HomeWork10/test.txt @@ -0,0 +1,5 @@ +world +worlf hey hello +world new +World +lol diff --git a/HomeWork11/.clang-format b/HomeWork11/.clang-format new file mode 100644 index 0000000..a7b6975 --- /dev/null +++ b/HomeWork11/.clang-format @@ -0,0 +1,115 @@ +--- +Language: Cpp +AccessModifierOffset: -2 +AlignAfterOpenBracket: AlwaysBreak +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Right +AlignOperands: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: true +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +BinPackArguments: false +BinPackParameters: false +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: true + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Linux +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + - Regex: '.*' + Priority: 1 +IncludeIsMainRegex: '(Test)?$' +IndentCaseLabels: false +IndentPPDirectives: None +IndentWidth: 4 +IndentWrappedFunctionNames: false +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Left +ReflowComments: false +SortIncludes: true +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Cpp11 +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 4 +UseTab: Never +... diff --git a/HomeWork11/a.out b/HomeWork11/a.out new file mode 100755 index 0000000..635ad9d Binary files /dev/null and b/HomeWork11/a.out differ diff --git a/HomeWork11/posix-fork-count-words.c b/HomeWork11/posix-fork-count-words.c new file mode 100644 index 0000000..5896537 --- /dev/null +++ b/HomeWork11/posix-fork-count-words.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char* argv[]) +{ + char str[128]; + int p; + int status_check = 1; + pid_t start_pid = getpid(); + int* mmap_ptr = (int*)mmap( + NULL, + sizeof(int), + PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_SHARED, + -1, + 0); + while (status_check) { + int input = scanf("%s", str); + if (input == -1) { + break; + } + p = fork(); + if (p != 0) { + int status; + waitpid(p, &status, 0); + (*(mmap_ptr))++; + break; + } + } + + if (getpid() == start_pid) { + printf("%d", *mmap_ptr); + munmap(mmap_ptr, sizeof(int)); + } +} \ No newline at end of file diff --git a/HomeWork11/posix-fork-count-words.c:Zone.Identifier b/HomeWork11/posix-fork-count-words.c:Zone.Identifier new file mode 100644 index 0000000..adce20d --- /dev/null +++ b/HomeWork11/posix-fork-count-words.c:Zone.Identifier @@ -0,0 +1,3 @@ +[ZoneTransfer] +ZoneId=3 +HostUrl=https://fpmi.yajudge.ru/ diff --git a/HomeWork11/posix-fork-print-numbers.c b/HomeWork11/posix-fork-print-numbers.c new file mode 100644 index 0000000..4629414 --- /dev/null +++ b/HomeWork11/posix-fork-print-numbers.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char* argv[]) +{ + int32_t N = strtol(argv[1], NULL, 10); + for (int32_t i = 1; i <= N; i++) { + if (fork() == 0) { + printf("%u", i); + if (i < N) { + printf("%s", " "); + exit(0); + } + } + wait(0); + } +} \ No newline at end of file diff --git a/HomeWork11/posix-fork-print-numbers.c:Zone.Identifier b/HomeWork11/posix-fork-print-numbers.c:Zone.Identifier new file mode 100644 index 0000000..adce20d --- /dev/null +++ b/HomeWork11/posix-fork-print-numbers.c:Zone.Identifier @@ -0,0 +1,3 @@ +[ZoneTransfer] +ZoneId=3 +HostUrl=https://fpmi.yajudge.ru/ diff --git a/HomeWork11/posix-fork-test-limit (2).c:Zone.Identifier b/HomeWork11/posix-fork-test-limit (2).c:Zone.Identifier new file mode 100644 index 0000000..be537eb Binary files /dev/null and b/HomeWork11/posix-fork-test-limit (2).c:Zone.Identifier differ diff --git a/HomeWork11/posix-fork-test-limit.c b/HomeWork11/posix-fork-test-limit.c new file mode 100644 index 0000000..382ec70 --- /dev/null +++ b/HomeWork11/posix-fork-test-limit.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include +#include + +int main() +{ + int count = 1; + pid_t pid = 0; + int start_pid = getpid(); + while (getpid() == start_pid) { + if (-1 == (pid = fork())) { + break; + } + ++count; + } + + pid = waitpid(pid, 0, 0); + if (pid > 0) { + int status; + waitpid(pid, &status, 0); + } else if (pid == -1){ + printf("%i", count); + } + return 0; +} \ No newline at end of file diff --git a/HomeWork11/posix-fork-test-limit.c:Zone.Identifier b/HomeWork11/posix-fork-test-limit.c:Zone.Identifier new file mode 100644 index 0000000..adce20d --- /dev/null +++ b/HomeWork11/posix-fork-test-limit.c:Zone.Identifier @@ -0,0 +1,3 @@ +[ZoneTransfer] +ZoneId=3 +HostUrl=https://fpmi.yajudge.ru/ diff --git a/HomeWork11/test.c b/HomeWork11/test.c new file mode 100644 index 0000000..aa72866 --- /dev/null +++ b/HomeWork11/test.c @@ -0,0 +1,22 @@ +#include +#include +#include +#include +#include + +int main() +{ + int count = 0; + int pid = 0; + int start_pid = getpid(); + while (getpid() == start_pid) { + if (-1 == (pid = fork())) { + break; + } + ++count; + } + if (getpid() == start_pid) { + printf("%i", count + 1); + } + return 0; +} \ No newline at end of file diff --git a/HomeWork12/.clang-format b/HomeWork12/.clang-format new file mode 100644 index 0000000..a7b6975 --- /dev/null +++ b/HomeWork12/.clang-format @@ -0,0 +1,115 @@ +--- +Language: Cpp +AccessModifierOffset: -2 +AlignAfterOpenBracket: AlwaysBreak +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Right +AlignOperands: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: true +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +BinPackArguments: false +BinPackParameters: false +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: true + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Linux +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + - Regex: '.*' + Priority: 1 +IncludeIsMainRegex: '(Test)?$' +IndentCaseLabels: false +IndentPPDirectives: None +IndentWidth: 4 +IndentWrappedFunctionNames: false +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Left +ReflowComments: false +SortIncludes: true +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Cpp11 +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 4 +UseTab: Never +... diff --git a/HomeWork12/posix-exec-python.c b/HomeWork12/posix-exec-python.c new file mode 100644 index 0000000..b363577 --- /dev/null +++ b/HomeWork12/posix-exec-python.c @@ -0,0 +1,18 @@ +#include +#include +#include + +int main() +{ + char buff[4096]; + char str[4096]; + fgets(buff, sizeof(buff), stdin); + char* slash = strchr(buff, '\n'); + if (slash) + *slash = '\0'; + if (strnlen(buff, sizeof(buff)) == 0) + return 0; + snprintf(str, sizeof(str), "ans = %s; print(ans)", buff); + execlp("python3", "python3", "-c", str, NULL); + perror("failed to exec"); +} \ No newline at end of file diff --git a/HomeWork12/posix-exec-python.c:Zone.Identifier b/HomeWork12/posix-exec-python.c:Zone.Identifier new file mode 100644 index 0000000..adce20d --- /dev/null +++ b/HomeWork12/posix-exec-python.c:Zone.Identifier @@ -0,0 +1,3 @@ +[ZoneTransfer] +ZoneId=3 +HostUrl=https://fpmi.yajudge.ru/ diff --git a/HomeWork4/a.out b/HomeWork4/a.out new file mode 100755 index 0000000..d19d0e6 Binary files /dev/null and b/HomeWork4/a.out differ diff --git a/HomeWork4/char-filter-digits.S b/HomeWork4/char-filter-digits.S new file mode 100644 index 0000000..add2943 --- /dev/null +++ b/HomeWork4/char-filter-digits.S @@ -0,0 +1,27 @@ +.text +.global main + +main: + sub sp, sp, 16 + str x30, [sp] + + .Begin: + bl getchar_unlocked + + cmp w0, 0 + blt .End + + cmp w0, '0' + blt .Begin + cmp w0, '9' + bgt .Begin + + bl putchar_unlocked + b .Begin + .End: + + ldr x30, [sp] + add sp, sp, 16 + mov x0, 0 + + ret diff --git a/HomeWork4/format-io.S b/HomeWork4/format-io.S new file mode 100644 index 0000000..fc2247e --- /dev/null +++ b/HomeWork4/format-io.S @@ -0,0 +1,33 @@ +.data + scanf_format: .string "%d %d" + printf_format: .string "%d" + a: .word 1 + b: .word 1 + +.text + +.global main + +main: + sub sp, sp, 16 + str x30, [sp] + + adr x0, scanf_format + adr x1, a + adr x2, b + bl scanf + + adr x1, a + adr x2, b + ldr w1, [x1] + ldr w2, [x2] + + add w1, w1, w2 + adr x0, printf_format + bl printf + + ldr x30, [sp] + add sp, sp, 16 + mov x0, 0 + + ret diff --git a/HomeWork4/globalvars.S b/HomeWork4/globalvars.S new file mode 100644 index 0000000..239fcf2 --- /dev/null +++ b/HomeWork4/globalvars.S @@ -0,0 +1,25 @@ +.text + +.global calculate +.global R + +calculate: + adr x0, A + ldr x0, [x0] + adr x1, B + ldr x1, [x1] + adr x2, C + ldr x2, [x2] + adr x3, D + ldr x3, [x3] + + mul x0, x0, x1 + mul x2, x2, x3 + add x0, x0, x2 + + adr x4, R + str x0, [x4] + + ret +.data + R: .quad diff --git a/HomeWork4/reverse-print-chars.S b/HomeWork4/reverse-print-chars.S new file mode 100644 index 0000000..cb61059 --- /dev/null +++ b/HomeWork4/reverse-print-chars.S @@ -0,0 +1,58 @@ +.text +.global main + +main: + sub sp, sp, 16 + str x30, [sp] + + mov x0, 0 + mov x1, 8 + mov x19, 0 + mov x20, 8 + mov x21, 0 + mov x22, 2 + bl realloc + + mov x19, x0 + + .B_input: + + bl getchar_unlocked + cmp w0, 0 + blt .E_input + + str w0, [x19, x21] + add x21, x21, 1 + cmp x21, x20 + bne .Skip + + mul x20, x20, x22 + mov x1, x20 + mov x0, x19 + bl realloc + + mov x19, x0 + + .Skip: + b .B_input + + .E_input: + + .B_output: + + cmp x21, 1 + blt .E_output + + sub x21, x21, 1 + ldr x0, [x19, x21] + bl putchar_unlocked + b .B_output + + .E_output: + + mov x0, x19 + bl free + ldr x30, [sp] + add sp, sp, 16 + + ret \ No newline at end of file diff --git a/HomeWork4/test.S b/HomeWork4/test.S new file mode 100755 index 0000000..0d8573c --- /dev/null +++ b/HomeWork4/test.S @@ -0,0 +1,2 @@ +echo 'ok' + diff --git a/HomeWork5/a.out b/HomeWork5/a.out new file mode 100755 index 0000000..755ef8f Binary files /dev/null and b/HomeWork5/a.out differ diff --git a/HomeWork5/arrays.S b/HomeWork5/arrays.S new file mode 100644 index 0000000..0f79645 --- /dev/null +++ b/HomeWork5/arrays.S @@ -0,0 +1,20 @@ + .intel_syntax noprefix + .global sum + .text + +sum: + mov r10, 0 + .B_input: + cmp rdi, r10 + jz .E_input + + mov r8d, [rsi + 4*r10] + add r8d, [rdx + 4*r10] + mov [rcx + 4*r10], r8d + inc r10 + + jmp .B_input + + .E_input: + + ret diff --git a/HomeWork5/hello-x86.S b/HomeWork5/hello-x86.S new file mode 100755 index 0000000..d83fd22 --- /dev/null +++ b/HomeWork5/hello-x86.S @@ -0,0 +1,45 @@ +.intel_syntax noprefix +.global very_important_function +.section .text + +very_important_function: + push rbp + push r12 + push r13 + + sub rsp, 16 + mov r12, rdi + mov r13, 0 + + LoopBegin: + cmp r12, r13 + jz LoopEnd + + lea rdi, input_fmt[rip] + mov rsi, rsp + call scanf + + mov rax, [rsp] + mov r9, A[rip] + MOVSXD r9, [r9 + r13*4] + imul r9 + + mov rsi, rax + lea rdi, output_fmt[rip] + call printf + inc r13 + jmp LoopBegin + + LoopEnd: + add rsp, 16 + pop r13 + pop r12 + pop rbp + + ret + +.section .rodata + input_fmt: + .string "%lld" + output_fmt: + .string "%lld\n" diff --git a/HomeWork5/main.c b/HomeWork5/main.c new file mode 100755 index 0000000..8024332 --- /dev/null +++ b/HomeWork5/main.c @@ -0,0 +1,20 @@ +#include +#include +#include + +void very_important_function(size_t N); + +int *A; + +int main() { + A = malloc(sizeof(int)*7); + A[0] = 2; + A[1] = 1; + A[2] = 2; + A[3] = 3; + A[4] = 4; + A[5] = 5; + A[6] = 6; + very_important_function(7); + free(A); +} \ No newline at end of file diff --git a/HomeWork6/avx-sin.S b/HomeWork6/avx-sin.S new file mode 100644 index 0000000..0dad2df --- /dev/null +++ b/HomeWork6/avx-sin.S @@ -0,0 +1,39 @@ +.intel_syntax noprefix +.section .text +.global avx_sin + +avx_sin: + movsd xmm1, xmm0 + mulsd xmm1, xmm1 + movsd xmm2, xmm0 + mov r12, -1 + cvtsi2sd xmm5, r12 + mulsd xmm1, xmm5 + mov r9, 21 + mov r10, 1 + mov r11, 1 + vxorpd xmm0, xmm0, xmm0 + + .Begin: + + cvtsi2sd xmm3, r11 + movsd xmm4, xmm2 + divsd xmm4, xmm3 + addsd xmm0, xmm4 + mulsd xmm2, xmm1 + add r10, 1 + mov rax, r11 + mul r10 + mov r11, rax + add r10, 1 + mov rax, r11 + mul r10 + mov r11, rax + cmp r9, r10 + jz .End + jmp .Begin + + .End: + + ret + diff --git a/HomeWork6/dot-product.S b/HomeWork6/dot-product.S new file mode 100644 index 0000000..ffa5aa2 --- /dev/null +++ b/HomeWork6/dot-product.S @@ -0,0 +1,51 @@ +.intel_syntax noprefix +.text +.global dot_product + +dot_product: + mov r9, 0 + mov r10, 0 + mov r11, 8 + vxorpd xmm0, xmm0, xmm0 + .Begin: + + add r10, r9 + add r10, r11 + cmp rdi, r10 + jae .End + + vmovups ymm1, [rsi + 4*r9] + vmovups ymm2, [rdx + 4*r9] + vmulps ymm1, ymm1, ymm2 + + vextractf128 xmm2, ymm1, 1 + haddps xmm2, xmm1 + haddps xmm2, xmm2 + haddps xmm2, xmm2 + + addps xmm0, xmm2 + + add r9, 8 + + jmp .Begin + + .End: + + sub r9, 8 + + remain_begin: + cmp r9, rdi + jae remain_end + + movss xmm3, [rsi + 4*r9] + movss xmm4, [rdx + 4*r9] + mulss xmm3, xmm4 + addss xmm0, xmm3 + + add r9, 1 + + jmp remain_begin + + remain_end: + + ret \ No newline at end of file diff --git a/HomeWork6/helo-avx.S b/HomeWork6/helo-avx.S new file mode 100644 index 0000000..69c782c --- /dev/null +++ b/HomeWork6/helo-avx.S @@ -0,0 +1,34 @@ +.intel_syntax noprefix +.text +.global very_important_function + +very_important_function: + xor rax, rax + vxorpd ymm2, ymm2, ymm2 + + .Begin: + + cmp rax, rdi + jz .End + + vmovaps ymm0, [rsi + 4*rax] + vmovaps ymm1, [rdx + 4*rax] + vaddps ymm0, ymm0, ymm1 + vaddps ymm2, ymm2, ymm0 + vmovaps [rcx + 4*rax], ymm0 + add rax, 8 + jmp .Begin + + .End: + + vmovaps ymm0, ymm2 + vextractf128 xmm1, ymm0, 1 + haddps xmm1, xmm0 + haddps xmm1, xmm1 + haddps xmm1, xmm1 + + vcvtsi2sd xmm0, xmm0, rdi + vcvtss2sd xmm1, xmm1, xmm1 + vdivsd xmm0, xmm1, xmm0 + + ret \ No newline at end of file diff --git a/HomeWork7/a.out b/HomeWork7/a.out new file mode 100755 index 0000000..a246478 Binary files /dev/null and b/HomeWork7/a.out differ diff --git a/HomeWork7/linux-syscalls-copy-stream.c b/HomeWork7/linux-syscalls-copy-stream.c new file mode 100644 index 0000000..407dcce --- /dev/null +++ b/HomeWork7/linux-syscalls-copy-stream.c @@ -0,0 +1,11 @@ +#include +#include + +long syscall(long number, ...); +void _start() +{ + char buffer[30000]; + size_t bytes = syscall(SYS_read, 0, buffer, 30000); + syscall(SYS_write, 1, buffer, bytes); + syscall(SYS_exit, 0); +} \ No newline at end of file diff --git a/HomeWork7/linux-syscalls-copy-stream.c:Zone.Identifier b/HomeWork7/linux-syscalls-copy-stream.c:Zone.Identifier new file mode 100644 index 0000000..adce20d --- /dev/null +++ b/HomeWork7/linux-syscalls-copy-stream.c:Zone.Identifier @@ -0,0 +1,3 @@ +[ZoneTransfer] +ZoneId=3 +HostUrl=https://fpmi.yajudge.ru/ diff --git a/HomeWork7/linux-syscalls-hello-world.c b/HomeWork7/linux-syscalls-hello-world.c new file mode 100644 index 0000000..8393a3e --- /dev/null +++ b/HomeWork7/linux-syscalls-hello-world.c @@ -0,0 +1,8 @@ +#include + +long syscall(long number, ...); +void _start() +{ + syscall(SYS_write, 1, "Hello, World!\n", 14); + syscall(SYS_exit, 0); +} \ No newline at end of file diff --git a/HomeWork7/reverse-print-lines.S b/HomeWork7/reverse-print-lines.S new file mode 100644 index 0000000..d9e8444 --- /dev/null +++ b/HomeWork7/reverse-print-lines.S @@ -0,0 +1,68 @@ +.intel_syntax noprefix + .text + .global _start + +_start: + mov r13, 131072 + + mov rax, 45 + mov rbx, 0 + int 0x80 + mov rsi, rax + + mov rax, 45 + add rsi, r13 + mov rbx, rsi + int 0x80 + + sub rax, r13 + mov rsi, rax + mov r12, rax + + mov rax, 45 + mov rbx, 0 + int 0x80 + mov r14, rax + + mov rax, 45 + add r14, 4096 + mov rbx, r14 + int 0x80 + + mov r14, rax + sub r14, 4096 + mov QWORD PTR[r14], 0 + mov r15, 0 + mov rdi, 0 + + read: + mov rax, 3 + mov rbx, 0 + mov rcx, rsi + mov rdx, r13 + int 0x80 + add r15, 1 + add rsi, rax + add rdi, rax + mov QWORD PTR[r14 + 8 * r15], rdi + cmp rax, 1 + jg read + + write: + cmp r15, 0 + je end + mov rax, 4 + mov rbx, 1 + mov r9, QWORD PTR[r14 + 8 * r15] + sub r15, 1 + mov r10, QWORD PTR[r14 + 8 * r15] + mov r11, r12 + add r11, r10 + sub r9, r10 + mov rcx, r11 + mov rdx, r9 + int 0x80 + jmp write + end: + + ret \ No newline at end of file diff --git a/HomeWork7/syscall.S b/HomeWork7/syscall.S new file mode 100644 index 0000000..e04e6bb --- /dev/null +++ b/HomeWork7/syscall.S @@ -0,0 +1,42 @@ + .intel_syntax noprefix + .global syscall + .text +syscall: + // prologue + push rbx + push rbp + push r12 + push r13 + push r14 + push r15 + // also keep r11 and rcx due to 'syscall' on x64 modifies them + push r11 + push rcx + // push arguments into memory to reaarange them + push r9 + push r8 + push rcx + push rdx + push rsi + push rdi + // first argument is syscall number, move it into rax + pop rax + // move arguments into registers to match syscall calling conventions + pop rdi + pop rsi + pop rdx + pop r10 + pop r8 + // make actual syscall + syscall + // epilogue + pop rcx + pop r11 + pop r15 + pop r14 + pop r13 + pop r12 + pop rbp + pop rbx + ret + diff --git a/HomeWork8/.clang-format b/HomeWork8/.clang-format new file mode 100644 index 0000000..a7b6975 --- /dev/null +++ b/HomeWork8/.clang-format @@ -0,0 +1,115 @@ +--- +Language: Cpp +AccessModifierOffset: -2 +AlignAfterOpenBracket: AlwaysBreak +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Right +AlignOperands: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: true +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +BinPackArguments: false +BinPackParameters: false +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: true + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Linux +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + - Regex: '.*' + Priority: 1 +IncludeIsMainRegex: '(Test)?$' +IndentCaseLabels: false +IndentPPDirectives: None +IndentWidth: 4 +IndentWrappedFunctionNames: false +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Left +ReflowComments: false +SortIncludes: true +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Cpp11 +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 4 +UseTab: Never +... diff --git a/HomeWork8/posix-io-print.c b/HomeWork8/posix-io-print.c new file mode 100644 index 0000000..ab68930 --- /dev/null +++ b/HomeWork8/posix-io-print.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include + +int main(int args, char* argv[]) +{ + int input = open(argv[1], O_RDONLY); + int value = -1; + if (read(input, &value, sizeof(value)) <= 0) { + return 0; + } + uint32_t next_pointer = -1; + read(input, &next_pointer, sizeof(next_pointer)); + while (next_pointer > 0) { + printf("%d ", value); + lseek(input, next_pointer, SEEK_SET); + read(input, &value, sizeof(value)); + read(input, &next_pointer, sizeof(next_pointer)); + } + printf("%d", value); + close(input); + return 0; +} diff --git a/HomeWork8/read-filter-write.c b/HomeWork8/read-filter-write.c new file mode 100644 index 0000000..5d222d0 --- /dev/null +++ b/HomeWork8/read-filter-write.c @@ -0,0 +1,23 @@ +#include +#include +#include + +int main(int args, char* argv[]) +{ + int input = open(argv[1], O_RDONLY); + int digits = open(argv[2], O_WRONLY | O_CREAT, 0640); + int other = open(argv[3], O_WRONLY | O_CREAT, 0640); + + ssize_t bytes_read; + char in_byte; + int output_FD; + while ((bytes_read = read(input, &in_byte, sizeof(in_byte))) > 0) { + if ('0' <= in_byte && in_byte <= '9') { + output_FD = digits; + } else { + output_FD = other; + } + write(output_FD, &in_byte, sizeof(in_byte)); + } + return 0; +} \ No newline at end of file diff --git a/HomeWork9/.clang-format b/HomeWork9/.clang-format new file mode 100644 index 0000000..a7b6975 --- /dev/null +++ b/HomeWork9/.clang-format @@ -0,0 +1,115 @@ +--- +Language: Cpp +AccessModifierOffset: -2 +AlignAfterOpenBracket: AlwaysBreak +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Right +AlignOperands: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: true +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +BinPackArguments: false +BinPackParameters: false +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: true + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Linux +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + - Regex: '.*' + Priority: 1 +IncludeIsMainRegex: '(Test)?$' +IndentCaseLabels: false +IndentPPDirectives: None +IndentWidth: 4 +IndentWrappedFunctionNames: false +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Left +ReflowComments: false +SortIncludes: true +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Cpp11 +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 4 +UseTab: Never +... diff --git a/HomeWork9/a.out b/HomeWork9/a.out new file mode 100755 index 0000000..0ebdbed Binary files /dev/null and b/HomeWork9/a.out differ diff --git a/HomeWork9/file-list-sum-size.c b/HomeWork9/file-list-sum-size.c new file mode 100644 index 0000000..8e70990 --- /dev/null +++ b/HomeWork9/file-list-sum-size.c @@ -0,0 +1,27 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int main() +{ + int64_t summary = 0; + char file[PATH_MAX + 1]; + struct stat stat; + while (fgets(file, sizeof(file), stdin)) { + char* back_slash_n = memchr(file, '\n', sizeof(file)); + if (back_slash_n != 0) { + *back_slash_n = '\0'; + } + if (lstat(file, &stat) != -1) { + if (S_ISREG(stat.st_mode)) { + summary += stat.st_size; + } + } + } + printf("%li", summary); +} \ No newline at end of file diff --git a/HomeWork9/find-invalid-executables.c b/HomeWork9/find-invalid-executables.c new file mode 100644 index 0000000..3869cc0 --- /dev/null +++ b/HomeWork9/find-invalid-executables.c @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main() +{ + char* back_slash_n; + char file[PATH_MAX]; + struct stat stat1; + while (fgets(file, sizeof(file), stdin)) { + back_slash_n = memchr(file, '\n', sizeof(file)); + if (back_slash_n) { + *back_slash_n = '\0'; + } + int new_fd = open(file, O_RDONLY); + if (new_fd <= 0) { + continue; + } + if (fstat(new_fd, &stat1) != -1) { + if (stat1.st_mode & S_IXUSR) { + if (lseek(new_fd, 0, SEEK_END) >= 2) { + lseek(new_fd, 0, SEEK_SET); + char first = 0; + char second = 0; + read(new_fd, &first, sizeof(char)); + read(new_fd, &second, sizeof(char)); + + if (first != '#' || second != '!') { + lseek(new_fd, 0, SEEK_SET); + char a = 0; + char b = 0; + char c = 0; + char d = 0; + read(new_fd, &a, sizeof(char)); + read(new_fd, &b, sizeof(char)); + read(new_fd, &c, sizeof(char)); + read(new_fd, &d, sizeof(char)); + if (a != 0x7f || b != 'E' || c != 'L' || d != 'F') { + goto PUTS; + } + continue; + } + + char buff[PATH_MAX]; + read(new_fd, buff, sizeof(buff)); + back_slash_n = memchr(buff, '\n', sizeof(buff)); + if (back_slash_n) { + *back_slash_n = '\0'; + } + struct stat stat2; + if (stat(buff, &stat2) == -1 || + !(stat2.st_mode & S_IXUSR)) { + goto PUTS; + } + } else { + goto PUTS; + } + } + continue; + } + continue; + PUTS: + puts(file); + } + return 0; +} \ No newline at end of file diff --git a/HomeWork9/main (21).c b/HomeWork9/main (21).c new file mode 100644 index 0000000..69df188 --- /dev/null +++ b/HomeWork9/main (21).c @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#include +#include +#include + +void fill(int** ptr, int64_t size) +{ + for (int i = 0; i < size; ++i) { + for (int j = 0; j < size; ++j) { + ptr[i][j] = -1; + } + } + if (size == 1) { + ptr[0][0] = 1; + return; + } + int x = 0; + int y = 0; + int curr = 1; + while (1) { + while (x < size) { + if (curr == size * size + 1) { + goto end; + } + if (ptr[y][x] != -1) { + break; + } + ptr[y][x] = curr; + ++curr; + ++x; + } + --x; + ++y; + while (y < size) { + if (curr == size * size + 1) { + goto end; + } + if (ptr[y][x] != -1) { + break; + } + ptr[y][x] = curr; + ++curr; + ++y; + } + --y; + --x; + while (x > -1) { + if (curr == size * size + 1) { + goto end; + } + if (ptr[y][x] != -1) { + break; + } + ptr[y][x] = curr; + ++curr; + --x; + } + ++x; + --y; + while (y > -1) { + if (curr == size * size + 1) { + goto end; + } + if (ptr[y][x] != -1) { + break; + } + ptr[y][x] = curr; + ++curr; + --y; + } + ++y; + ++x; + } +end: + ++x; +} + +int main(int args, char* argv[]) +{ + char* name = argv[1]; + int64_t size = strtol(argv[2], 0, 10); + int64_t width = strtol(argv[3], 0, 10); + int64_t file_size = (size * size * width + size - 1) * sizeof(char); + int fd = open(name, O_CREAT | O_RDWR); + if (fd < 0) { + return 1; + } + lseek(fd, file_size - 1, SEEK_SET); + write(fd, "", 1); + lseek(fd, 0, SEEK_SET); + + int** numbers = (int**)malloc(sizeof(int*) * size); + for (int i = 0; i < size; ++i) { + numbers[i] = (int*)malloc(sizeof(int) * size); + } + fill(numbers, size); + + void* mmap_ptr = + mmap(0, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + char* copy = (char*)mmap_ptr; + + char format[100]; + format[0] = '%'; + int len = sprintf(format + 1, "%li", width); + format[len + 1] = 'l'; + format[len + 2] = 'i'; + format[len + 3] = '\0'; + + for (int i = 0; i < size; ++i) { + for (int j = 0; j < size; ++j) { + sprintf(copy, format, numbers[i][j]); + copy += width * sizeof(char); + } + if (i != size - 1) { + sprintf(copy, "%c", '\n'); + copy += sizeof(char); + } + } + + close(fd); + munmap(mmap_ptr, file_size); + for (int i = 0; i < size; ++i) { + free(numbers[i]); + } + free(numbers); + return 0; +} diff --git a/HomeWork9/manage-symlinks.c b/HomeWork9/manage-symlinks.c new file mode 100644 index 0000000..8cd6ca7 --- /dev/null +++ b/HomeWork9/manage-symlinks.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main() +{ + char* back_slash_n; + char file[PATH_MAX + 1]; + struct stat stat1; + while (fgets(file, sizeof(file), stdin)) { + back_slash_n = memchr(file, '\n', sizeof(file)); + if (back_slash_n != 0) { + *back_slash_n = '\0'; + } + + if (lstat(file, &stat1) != -1) { + if (S_ISREG(stat1.st_mode)) { + char buff[PATH_MAX + 1]; + strcat(strcpy(buff, "link_to_"), basename(file)); + symlink(file, buff); + continue; + } + + if (S_ISLNK(stat1.st_mode)) { + char file_name[PATH_MAX + 1]; + char path_name[PATH_MAX + 1]; + char buff[PATH_MAX + 1]; + buff[readlink(file, buff, sizeof(buff))] = '\0'; + strcpy(file_name, dirname(file)); + strcat(file_name, "/"); + strcat(file_name, buff); + realpath(file_name, path_name); + puts(path_name); + } + } + } +} \ No newline at end of file diff --git a/HomeWork9/test.txt b/HomeWork9/test.txt new file mode 100644 index 0000000..e69de29 diff --git a/c-process-arguments (2).c b/c-process-arguments (2).c new file mode 100755 index 0000000..01bf67b --- /dev/null +++ b/c-process-arguments (2).c @@ -0,0 +1,19 @@ +#include +#include + +int main(int argc, char* argv[]) +{ + long long sum = 0; + for (int i = 1; i < argc; i++) { + char* q = argv[i]; + if (atoll(q)) { + sum += (atoll(argv[i])); + } else { + printf("%s", q); + printf("\n"); + } + } + printf("%lld", sum); +} + + diff --git a/c-process-arguments.c b/c-process-arguments.c new file mode 100755 index 0000000..cf6ea19 --- /dev/null +++ b/c-process-arguments.c @@ -0,0 +1,16 @@ +#include +#include + +int main(int argc, char *argv[]) { + int sum = 0; + for (int i = 1; i < argc; i++) { + char *q = argv[i]; + if (atoi(q)) { + sum += (atoi(argv[i])); + } else { + printf("%s", q); + printf("\n"); + } + } + printf("%d", sum); +} diff --git a/clang-format.txt:Zone.Identifier b/clang-format.txt:Zone.Identifier new file mode 100644 index 0000000..adce20d --- /dev/null +++ b/clang-format.txt:Zone.Identifier @@ -0,0 +1,3 @@ +[ZoneTransfer] +ZoneId=3 +HostUrl=https://fpmi.yajudge.ru/ diff --git a/eqsolution.S b/eqsolution.S new file mode 100644 index 0000000..c1bfaa7 --- /dev/null +++ b/eqsolution.S @@ -0,0 +1,46 @@ +.text +.global solve + +solve: +// x0 : A +// x1 : B +// x2 : C +// x3 : D +// x4 : x = [0, 254] +// x5 : Ax^3 +// x6 : Bx^2 +// x7 : Cx + +mov w4, 0 +.LoopBegin: +mov w5, 0 +mov w6, 0 +mov w7, 0 +// Ax^3 +add w5, w5, w0 +mul w5, w5, w4 +mul w5, w5, w4 +mul w5, w5, w4 +// Bx^2 +add w6, w6, w1 +mul w6, w6, w4 +mul w6, w6, w4 +// Cx +add w7, w7, w2 +mul w7, w7, w4 +// sum +add w5, w5, w6 +add w5, w5, w7 +add w5, w5, w3 +// check +cmp w5, 0 +beq .LoopEnd + +add w4, w4, 1 +cmp w4, 254 +blt .LoopBegin + +.LoopEnd: +mov w0, w4 + +ret diff --git a/eqsoluyion.S b/eqsoluyion.S new file mode 100644 index 0000000..c0b9af6 --- /dev/null +++ b/eqsoluyion.S @@ -0,0 +1,47 @@ +.text +.global solve + +solve: +// x0 : A +// x1 : B +// x2 : C +// x3 : D +// x4 : x = [0, 254] +// x5 : sum +// x6 : Ax^3 +// x7 : Bx^2 +// x8 : Cx + +mov x4, 0 +.LoopBegin: +mov x5, 0 +mov x6, 0 +mov x7, 0 +mov x8, 0 +// Ax^3 +add x6, x6, x0 +mul x6, x6, x4 +mul x6, x6, x4 +mul x6, x6, x4 +// Bx^2 +add x7, x7, x1 +mul x7, x7, x4 +mul x7, x7, x4 +// Cx +add x8, x8, x2 +mul x8, x8, x4 +// sum +add x5, x6, x7 +add x5, x5, x8 +add x5, x5, x3 +// check +cmp x5, 0 +beq .LoopEnd + +add x4, x4, 1 +b .LoopBegin + +.LoopEnd: +mov x0, x5 + +ret diff --git a/format.txt b/format.txt new file mode 100644 index 0000000..a7b6975 --- /dev/null +++ b/format.txt @@ -0,0 +1,115 @@ +--- +Language: Cpp +AccessModifierOffset: -2 +AlignAfterOpenBracket: AlwaysBreak +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Right +AlignOperands: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: true +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +BinPackArguments: false +BinPackParameters: false +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: true + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Linux +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + - Regex: '.*' + Priority: 1 +IncludeIsMainRegex: '(Test)?$' +IndentCaseLabels: false +IndentPPDirectives: None +IndentWidth: 4 +IndentWrappedFunctionNames: false +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Left +ReflowComments: false +SortIncludes: true +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Cpp11 +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 4 +UseTab: Never +... diff --git a/memaccess.S b/memaccess.S new file mode 100644 index 0000000..dd42436 --- /dev/null +++ b/memaccess.S @@ -0,0 +1,20 @@ +.text +.global sum + +sum: +// x0 (4 bytes) +// x1 : N (8 bytes) +// x2 : *X (4 bytes) +// x3 = current element +// x4 = end condition + +.LoopBegin: +mov x3, 0 +ldr x3, [x2] +add x0, x0, x3 +add x2, x2, 4 +sub x1, x1, 1 +cmp x1, 0 +bgt .LoopBegin + +ret diff --git a/output.txt b/output.txt new file mode 100755 index 0000000..ed760b4 --- /dev/null +++ b/output.txt @@ -0,0 +1 @@ +./source.bin0 \ No newline at end of file diff --git a/sed-find-c-includes.sh b/sed-find-c-includes.sh new file mode 100755 index 0000000..c16ef85 --- /dev/null +++ b/sed-find-c-includes.sh @@ -0,0 +1,25 @@ +#!/User/bin/env + +function find_header() { + grep -E "\#include <" | sed -En 's/.*#include <(.*)>/\1/p' +} +IFS=":" +while read name +do + result="---" + for mpath in $MANPATH + do + path="$mpath/man3/$str.3" + if [-f "$path"] + then + result=`cat $path | find_header` + break + fi + if [-f "$path"] + then + result=`gunzip -cd "$path.gz" | find_header` + break + fi + done + echo $result +done diff --git a/sed-find-c-includes.sh:Zone.Identifier b/sed-find-c-includes.sh:Zone.Identifier new file mode 100644 index 0000000..adce20d --- /dev/null +++ b/sed-find-c-includes.sh:Zone.Identifier @@ -0,0 +1,3 @@ +[ZoneTransfer] +ZoneId=3 +HostUrl=https://fpmi.yajudge.ru/ diff --git a/source.bin b/source.bin new file mode 100755 index 0000000..3fe0e03 Binary files /dev/null and b/source.bin differ