initial commit

This commit is contained in:
ІО-23 Шмуляр Олег 2024-09-15 16:53:50 +03:00
commit 1bff04e860
4 changed files with 108 additions and 0 deletions

10
Makefile Normal file
View File

@ -0,0 +1,10 @@
ASMFLAGS =
CFLAGS = -I lib -O3
build: src/strutils.asm src/main.c
nasm $(ASMFLAGS) -f elf64 -o out/strutils.o src/strutils.asm
#gcc $(CFLAGS) -o out/main.o -c src/main.c
#ld -o bin/main out/main.o out/strutils.o
gcc $(CFLAGS) -o bin/main \
src/main.c \
out/strutils.o

1
lib/strutils.h Normal file
View File

@ -0,0 +1 @@
void count_vowels(char *str, int *result);

16
src/main.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
#include "strutils.h"
int main(void)
{
char source_string[1024];
int amount_of_vowels;
fgets(source_string, 1023, stdin);
count_vowels(source_string, &amount_of_vowels);
printf("%d\n", amount_of_vowels);
return 0;
}

81
src/strutils.asm Normal file
View File

@ -0,0 +1,81 @@
global count_vowels
section .note.GNU-stack
section .text
vowel_found:
inc r12
jmp next_char
; void count_vowels(char *str, int *result);
; | |
; v v
; %rdi %rsi
count_vowels:
; back up the pointers
push rsi
push rdi
; find string length
mov rcx, 1024
mov al, 0
; rdi is set by caller
repne scasb
; not wasting time on JNE
; as ZF should always equal to 1
mov rdi, rcx
mov rcx, 1024
sub rcx, rdi ; strlen -> rcx
; recover *str
pop rdi
; loop through the letters
xor r12, r12 ; reset counter
loop1:
mov bl, [rdi+rcx-1]
cmp bl, 0x3F
jle next_char ; if the symbol code <= 0x3F,
; it can never be a letter
and bl, 0x5F ; 0x5F -> 01011111
; ^ ^
; | cut off 6th bit to cast
; | all letters to uppercase
; |
; cut off any signed nonsense
cmp bl, 'A'
je vowel_found
cmp bl, 'Y'
jg next_char
je vowel_found
cmp bl, 'E'
je vowel_found
cmp bl, 'I'
je vowel_found
cmp bl, 'O'
je vowel_found
cmp bl, 'U'
je vowel_found
next_char:
dec rcx
jnz loop1
pop rsi
; copy result to target memory location
mov [rsi], r12d
ret