feat: mvp

This commit is contained in:
Miheev Egor
2024-12-08 19:14:13 +03:00
parent b161ff5465
commit 009f4d3bda
3 changed files with 998 additions and 0 deletions

467
coursework/main.s Normal file
View File

@ -0,0 +1,467 @@
.file "main.c"
.intel_syntax noprefix
.text
.globl name
.bss
.align 32
.type name, @object
.size name, 64
name:
.zero 64
.globl buffer
.align 32
.type buffer, @object
.size buffer, 1024
buffer:
.zero 1024
.globl buffer_tail
.section .data.rel.local,"aw"
.align 8
.type buffer_tail, @object
.size buffer_tail, 8
buffer_tail:
.quad buffer
.globl iseof
.bss
.type iseof, @object
.size iseof, 1
iseof:
.zero 1
.text
.globl namecmp
.type namecmp, @function
namecmp:
.LFB6:
.cfi_startproc
push rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
mov rbp, rsp
.cfi_def_cfa_register 6
mov QWORD PTR -8[rbp], rdi
mov QWORD PTR -16[rbp], rsi
jmp .L2
.L6:
mov rax, QWORD PTR -8[rbp]
movzx edx, BYTE PTR [rax]
mov rax, QWORD PTR -16[rbp]
movzx eax, BYTE PTR [rax]
cmp dl, al
je .L3
mov eax, 0
jmp .L4
.L3:
add QWORD PTR -8[rbp], 1
add QWORD PTR -16[rbp], 1
.L2:
mov rax, QWORD PTR -8[rbp]
movzx eax, BYTE PTR [rax]
test al, al
je .L5
mov rax, QWORD PTR -16[rbp]
movzx eax, BYTE PTR [rax]
test al, al
jne .L6
.L5:
mov eax, 1
.L4:
pop rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE6:
.size namecmp, .-namecmp
.section .rodata
.align 8
.LC0:
.string "Something went wrong while opening file\n"
.text
.globl openfile
.type openfile, @function
openfile:
.LFB7:
.cfi_startproc
push rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
mov rbp, rsp
.cfi_def_cfa_register 6
sub rsp, 32
mov QWORD PTR -24[rbp], rdi
mov rax, QWORD PTR -24[rbp]
mov esi, 0
mov rdi, rax
mov eax, 0
call open@PLT
mov DWORD PTR -4[rbp], eax
cmp DWORD PTR -4[rbp], 0
jns .L8
lea rax, .LC0[rip]
mov rdi, rax
call puts@PLT
mov edi, 1
call exit@PLT
.L8:
mov eax, DWORD PTR -4[rbp]
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE7:
.size openfile, .-openfile
.globl fill_buffer
.type fill_buffer, @function
fill_buffer:
.LFB8:
.cfi_startproc
push rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
mov rbp, rsp
.cfi_def_cfa_register 6
sub rsp, 32
mov DWORD PTR -20[rbp], edi
mov rdx, QWORD PTR buffer_tail[rip]
lea rax, buffer[rip]
sub rdx, rax
mov eax, 1024
sub rax, rdx
mov QWORD PTR -8[rbp], rax
mov rdx, QWORD PTR -8[rbp]
mov eax, DWORD PTR -20[rbp]
lea rcx, buffer[rip]
mov rsi, rcx
mov edi, eax
call read@PLT
mov QWORD PTR -16[rbp], rax
cmp QWORD PTR -16[rbp], 1023
ja .L11
mov BYTE PTR iseof[rip], 1
.L11:
lea rdx, buffer[rip]
mov rax, QWORD PTR -16[rbp]
add rax, rdx
mov QWORD PTR buffer_tail[rip], rax
nop
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE8:
.size fill_buffer, .-fill_buffer
.globl find_second_word
.type find_second_word, @function
find_second_word:
.LFB9:
.cfi_startproc
push rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
mov rbp, rsp
.cfi_def_cfa_register 6
mov QWORD PTR -8[rbp], rdi
jmp .L13
.L14:
add QWORD PTR -8[rbp], 1
.L13:
mov rax, QWORD PTR -8[rbp]
movzx eax, BYTE PTR [rax]
cmp al, 32
jne .L14
add QWORD PTR -8[rbp], 1
mov rax, QWORD PTR -8[rbp]
pop rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE9:
.size find_second_word, .-find_second_word
.globl find_fullname_end
.type find_fullname_end, @function
find_fullname_end:
.LFB10:
.cfi_startproc
push rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
mov rbp, rsp
.cfi_def_cfa_register 6
lea rax, buffer[rip]
mov QWORD PTR -8[rbp], rax
jmp .L17
.L20:
add QWORD PTR -8[rbp], 1
.L17:
mov rax, QWORD PTR -8[rbp]
movzx eax, BYTE PTR [rax]
cmp al, 10
jne .L18
mov rax, QWORD PTR -8[rbp]
movzx eax, BYTE PTR [rax]
test al, al
jne .L19
.L18:
lea rax, buffer[rip+1024]
cmp QWORD PTR -8[rbp], rax
jb .L20
.L19:
lea rax, buffer[rip+1024]
cmp QWORD PTR -8[rbp], rax
jne .L21
mov eax, 0
jmp .L22
.L21:
mov rax, QWORD PTR -8[rbp]
.L22:
pop rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE10:
.size find_fullname_end, .-find_fullname_end
.globl pop_fullname
.type pop_fullname, @function
pop_fullname:
.LFB11:
.cfi_startproc
push rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
mov rbp, rsp
.cfi_def_cfa_register 6
sub rsp, 32
mov eax, 0
call find_fullname_end
add rax, 1
mov QWORD PTR -16[rbp], rax
lea rdx, buffer[rip]
mov rax, QWORD PTR -16[rbp]
sub rax, rdx
mov QWORD PTR -24[rbp], rax
mov QWORD PTR -8[rbp], 0
jmp .L24
.L25:
mov rdx, QWORD PTR -16[rbp]
mov rax, QWORD PTR -8[rbp]
add rax, rdx
movzx eax, BYTE PTR [rax]
lea rcx, buffer[rip]
mov rdx, QWORD PTR -8[rbp]
add rdx, rcx
mov BYTE PTR [rdx], al
mov rdx, QWORD PTR -16[rbp]
mov rax, QWORD PTR -8[rbp]
add rax, rdx
mov BYTE PTR [rax], 0
add QWORD PTR -8[rbp], 1
.L24:
mov eax, 1024
sub rax, QWORD PTR -24[rbp]
cmp QWORD PTR -8[rbp], rax
jb .L25
mov rax, QWORD PTR buffer_tail[rip]
mov rdx, QWORD PTR -24[rbp]
neg rdx
add rax, rdx
mov QWORD PTR buffer_tail[rip], rax
nop
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE11:
.size pop_fullname, .-pop_fullname
.section .rodata
.LC1:
.string "count: "
.LC2:
.string "\n"
.text
.globl print_count
.type print_count, @function
print_count:
.LFB12:
.cfi_startproc
push rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
mov rbp, rsp
.cfi_def_cfa_register 6
sub rsp, 32
mov QWORD PTR -24[rbp], rdi
mov edx, 8
lea rax, .LC1[rip]
mov rsi, rax
mov edi, 1
call write@PLT
jmp .L27
.L28:
mov rcx, QWORD PTR -24[rbp]
movabs rdx, -3689348814741910323
mov rax, rcx
mul rdx
shr rdx, 3
mov rax, rdx
sal rax, 2
add rax, rdx
add rax, rax
sub rcx, rax
mov rdx, rcx
mov eax, edx
add eax, 48
mov BYTE PTR -1[rbp], al
mov rax, QWORD PTR -24[rbp]
movabs rdx, -3689348814741910323
mul rdx
mov rax, rdx
shr rax, 3
mov QWORD PTR -24[rbp], rax
lea rax, -1[rbp]
mov edx, 1
mov rsi, rax
mov edi, 1
call write@PLT
.L27:
cmp QWORD PTR -24[rbp], 0
jne .L28
mov edx, 1
lea rax, .LC2[rip]
mov rsi, rax
mov edi, 1
call write@PLT
nop
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE12:
.size print_count, .-print_count
.globl get_name
.type get_name, @function
get_name:
.LFB13:
.cfi_startproc
push rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
mov rbp, rsp
.cfi_def_cfa_register 6
sub rsp, 40
mov QWORD PTR -40[rbp], rdi
lea rax, buffer[rip]
mov rdi, rax
call find_second_word
mov QWORD PTR -24[rbp], rax
mov rax, QWORD PTR -24[rbp]
mov QWORD PTR -8[rbp], rax
jmp .L30
.L31:
add QWORD PTR -8[rbp], 1
.L30:
mov rax, QWORD PTR -8[rbp]
movzx eax, BYTE PTR [rax]
cmp al, 32
jne .L31
cmp QWORD PTR -8[rbp], 0
jne .L32
mov eax, -1
jmp .L33
.L32:
mov QWORD PTR -16[rbp], 0
jmp .L34
.L35:
mov rdx, QWORD PTR -24[rbp]
mov rax, QWORD PTR -16[rbp]
add rax, rdx
mov rcx, QWORD PTR -40[rbp]
mov rdx, QWORD PTR -16[rbp]
add rdx, rcx
movzx eax, BYTE PTR [rax]
mov BYTE PTR [rdx], al
add QWORD PTR -16[rbp], 1
.L34:
mov rdx, QWORD PTR -24[rbp]
mov rax, QWORD PTR -16[rbp]
add rax, rdx
cmp rax, QWORD PTR -8[rbp]
jb .L35
mov eax, 0
.L33:
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE13:
.size get_name, .-get_name
.globl main
.type main, @function
main:
.LFB14:
.cfi_startproc
push rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
mov rbp, rsp
.cfi_def_cfa_register 6
sub rsp, 96
mov DWORD PTR -84[rbp], edi
mov QWORD PTR -96[rbp], rsi
mov rax, QWORD PTR -96[rbp]
add rax, 8
mov rax, QWORD PTR [rax]
mov rdi, rax
call openfile
mov DWORD PTR -12[rbp], eax
mov edx, 64
lea rax, name[rip]
mov rsi, rax
mov edi, 0
call read@PLT
mov QWORD PTR -8[rbp], 0
mov QWORD PTR -80[rbp], 0
mov QWORD PTR -72[rbp], 0
mov QWORD PTR -64[rbp], 0
mov QWORD PTR -56[rbp], 0
mov QWORD PTR -48[rbp], 0
mov QWORD PTR -40[rbp], 0
mov QWORD PTR -32[rbp], 0
mov QWORD PTR -24[rbp], 0
.L39:
mov eax, 0
call find_fullname_end
test rax, rax
jne .L37
mov eax, DWORD PTR -12[rbp]
mov edi, eax
call fill_buffer
.L37:
lea rax, -80[rbp]
mov rdi, rax
call get_name
lea rax, -80[rbp]
lea rdx, name[rip]
mov rsi, rdx
mov rdi, rax
call namecmp
test eax, eax
je .L38
add QWORD PTR -8[rbp], 1
.L38:
mov eax, 0
call pop_fullname
mov rdx, QWORD PTR buffer_tail[rip]
lea rax, buffer[rip]
cmp rdx, rax
jne .L39
mov rax, QWORD PTR -8[rbp]
mov rdi, rax
call print_count
mov eax, 0
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE14:
.size main, .-main
.ident "GCC: (Debian 14.2.0-6) 14.2.0"
.section .note.GNU-stack,"",@progbits