diff --git a/even_odd/.vscode/launch.json b/even_odd/.vscode/launch.json new file mode 100644 index 0000000..01d5062 --- /dev/null +++ b/even_odd/.vscode/launch.json @@ -0,0 +1,29 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "(lldb) Launch x64", + "type": "lldb", + "request": "launch", + "program": "${workspaceFolder}/build/even_odd", + "cwd": "${workspaceFolder}/build", + "preLaunchTask": "asm64", + }, + { + "name": "(gdb) Launch x64", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/build/even_odd", + "cwd": "${workspaceFolder}/build", + "preLaunchTask": "asm64" + }, + { + "name": "(gdb+gcc) Launch x64", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/build/even_odd", + "cwd": "${workspaceFolder}/build", + "preLaunchTask": "asm64+gcc" + } + ] +} \ No newline at end of file diff --git a/even_odd/.vscode/tasks.json b/even_odd/.vscode/tasks.json new file mode 100644 index 0000000..3c17b33 --- /dev/null +++ b/even_odd/.vscode/tasks.json @@ -0,0 +1,53 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "asm64", + "type": "shell", + "command": [ + "builddir=${workspaceFolder}/build;", + "mkdir -p $builddir;", + "rawfilename=$builddir/even_odd;", + "nasm -gdwarf -f elf64 -o $rawfilename.o ${workspaceFolder}/even_odd.asm;", + "ld -g -o $rawfilename $rawfilename.o;" + ], + "problemMatcher": { + "pattern": { + "regexp": "error" + } + }, + "presentation": { + "focus": true, + "panel": "dedicated", + "reveal": "silent", + "clear": true + }, + "group": { + "kind": "build", + "isDefault": true + } + }, + { + "label": "asm64+gcc", + "type": "shell", + "command": [ + "builddir=${workspaceFolder}/build;", + "mkdir -p $builddir;", + "rawfilename=$builddir/even_odd;", + "nasm -gdwarf -f elf64 -o $rawfilename.o ${workspaceFolder}/even_odd.asm;", + "gcc -o $rawfilename $rawfilename.o;" + ], + "problemMatcher": { + "pattern": { + "regexp": "error" + } + }, + "presentation": { + "focus": true, + "panel": "dedicated", + "reveal": "silent", + "clear": true + }, + } + ] +} \ No newline at end of file diff --git a/even_odd/even_odd.asm b/even_odd/even_odd.asm new file mode 100644 index 0000000..afcfbb4 --- /dev/null +++ b/even_odd/even_odd.asm @@ -0,0 +1,80 @@ +global _start + +section .data +buff times 32 db 0 +count dq 0 +even db "Even" +evenCount dq $-even +odd db "Odd" +oddCount dq $-odd + +section .text +default rel + +_start: + call read_text + + lea rsi, [buff] + add rsi, qword [count] + sub rsi, 2 + mov rax, [rsi] + + test rax, 0x1 + jnz .even +.odd: + lea rsi, [even] + mov rdx, [evenCount] + jmp .write +.even: + lea rsi, [odd] + mov rdx, [oddCount] + +.write: + call write_text + + mov rax, 60 + mov rdi, 0 + syscall + +; Функция читает текст в буффер +; Регистры не изменяет +read_text: + push rax + push rdi + push rsi + push rdx + + mov rax, 0 ; sys_read + mov rdi, 0 ; stdin + lea rsi, [rel buff] ; адрес буфера + mov rdx, 256 ; количество байт + syscall + mov [count], rax + + pop rdx + pop rsi + pop rdi + pop rax + ret + +; Функция выводит buff +; In: +; - RSI - начало буфера +; - RDX - размер буфера +; Регистры не изменяет +write_text: + push rax + push rdi + push rsi + push rdx + + mov rax, 1 ; sys_write + mov rdi, 1 ; stdout + syscall + mov [count], rax + + pop rdx + pop rsi + pop rdi + pop rax + ret \ No newline at end of file diff --git a/nasm.code-workspace b/nasm.code-workspace index c39e75f..7309cb0 100644 --- a/nasm.code-workspace +++ b/nasm.code-workspace @@ -6,6 +6,9 @@ { "path": "sum" }, + { + "path": "even_odd" + }, { "path": "docs" } diff --git a/sum/sum.asm b/sum/sum.asm index d36404b..a6a9539 100644 --- a/sum/sum.asm +++ b/sum/sum.asm @@ -1,7 +1,7 @@ global _start section .data -buff times 256 db 0 +buff times 32 db 0 buffend: count dq 0 @@ -151,4 +151,4 @@ print_num_to_buf: sub rdi, rsi inc rdi mov [count], rdi - ret \ No newline at end of file + ret