Files
mdk.01.04-cherevko/report_cherevko_228.md
T
2026-09-10 07:57:06 +00:00

106 lines
2.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Отчёт по практическому заданию №1
**Студент:** Черевко Леонид Дмитриевич
**Группа:** 228
---
## ⚙️ Подготовка к заданию
1. Открыл [godbolt.org](https://godbolt.org/)
2. В левой панели выбрал язык: **C**.
3. В правой панели добавил **два** компилятора (кнопка `+ Add new...`):
- **x86-64 GCC 14.1**
- **x86-64 Clang 18.1**
4. Для обоих компиляторов в **Compiler options** ввел:
`-O2 -masm=intel -fno-verbose-asm`
5. Снял галочки с **Directives** и **Comments** в фильтрах (Filter...), чтобы видеть только "чистый" ассемблер.
---
## 📌 Задание 1
```c
long variable_division(long a, long b) {
return a / b;
}
```
```asm
variable_division:
mov rax, rdi ; Переносим делимое (a) в регистр rax
cqo ; Расширяем знак из rax в rdx (создаем 128-битное rdx:rax)
idiv rsi ; Делим rdx:rax на rsi (b). Результат (частное) остается в rax
ret
```
---
## 📌 Задание 2
```c
unsigned short swap_bytes(unsigned short a) {
return (a >> 8) | (a << 8);
}
```
```asm
swap_bytes:
mov eax, edi
rol ax, 8 ; Циклический сдвиг (Rotate Left) 16-битного регистра ax на 8 бит
ret
```
---
## 📌 Задание 3
```c
long variable_division(long a, long b) {
return a / b;
}
```
```asm
variable_division:
sdiv x0, x0, x1 ; x0 = x0 / x1 (знаковое деление)
ret
```
---
## 📌 Задание 4
```c
long mult_3(long x) { return x * 3; }
long mult_5(long x) { return x * 5; }
long mult_9(long x) { return x * 9; }
```
```asm
mult_3:
lea rax, [rdi + rdi*2] ; rax = rdi + rdi * 2 (то есть x * 3)
ret
mult_5:
lea rax, [rdi + rdi*4] ; rax = rdi + rdi * 4 (то есть x * 5)
ret
mult_9:
lea rax, [rdi + rdi*8] ; rax = rdi + rdi * 8 (то x * 9)
ret
```
---
## 📌 Задание 5
```c
long factorial(int n) {
long res = 1;
for (int i = 1; i <= n; ++i) {
res *= i;
}
return res;
}
```