From 324d9c8f3cfd6cec2ab2a7064b700c3c72604241 Mon Sep 17 00:00:00 2001 From: Alexe Adelina Maria <adelina_maria.alexe@stud.acs.upb.ro> Date: Wed, 12 Mar 2025 00:01:50 +0200 Subject: [PATCH] readme - part 2 --- README.md | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 106 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d6c60a8..30054ca 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ The translation should be as simple as possible while covering basic arithmetic ## Conventions and Guidelines -Because we want to prevent you from having to juggle with registers and +Because we want to prevent you from having to juggle with registers and allocate memory for vectors. - **Basic Register Mapping:** - `A` â `eax` @@ -103,3 +103,108 @@ When multiplying with MUL, the multiplication will actually take EAX and source2 The last 3 rows represent the second operation. **Note**: For simplicity, we will never use eax as source2, and we will never never use the value from EDX. + +### Shift operations + +SHL (Shift left) and SHR (Shift right) are bitwise shift instructions. + +### Usage - shl, shr + +`SHL destination, no_bits_shifted` + +`SHR destination, no_bits_shifted` + +- SHL: Moves bits to the left, filling the rightmost bits with zeros. Each shift effectively **multiplies** the value by 2. + - Example: `00001100` (`12` in decimal) shifted left by 1 becomes `00011000` (`24` in decimal). + + +- SHR: Moves bits to the right, filling the leftmost bits with zeros. Each shift effectively **divides** the value by 2. + - Example: `00001100` (`12` in decimal) shifted left by 1 becomes `00000110` (`6` in decimal). + + +| **C Code** | **ASM Code** | +|------------- |---------------- | +| `a = a << 1` | `SHL eax, 1` | +| `b = b >> 2` | `SHR ebx, 2` | + + +### Conditional Statements + +### CMP Instrunction + +`CMP` instruction compares two values by substracting the second operand from the first. It updates CPU flags, but does not store the result. + +### Usage - cmp + +`CMP operand1, operand2` + +- Example: + + `MOV eax, 3` + + `CMP eax, 3` ; Compares eax with 3 + + `JE equal_label` ; Jumps to label "equal_label" if eax == 3 (Zero flag is set) + + +### Jumps + +Here's a short description of each conditional jump: + +- **JMP** (Jump): Jumps to the given label. +- **JE** (Jump if Equal): Jumps to the given label if the two compared values are equal (e.g.: `CMP` sets the Zero Flag). +- **JNE** (Jump if Not Equal): Jumps if the two values are not equal (Zero Flag is not set). +- **JG** (Jump if Greater): Jumps if the first value is greater than the second (Signed comparison). +- **JGE** (Jump if Greater or Equal): Jumps if the first value is greater than or equal to the second. +- **JL** (Jump if Less): Jumps if the first value is less than the second. +- **JLE** (Jump if Less or Equal): Jumps if the first value is less than or equal to the second. + +### Usage + +`JMP label` + +`JE label` + +`JNE label` + +`JG label` + +`JL label` + +### C - ASM translation + +| **C Code** | **ASM Code** | +|------------- |---------------- | +| `if (a == b) {` | `CMP eax, ebx` | +| | `JNE else_label` | +| `// some code here` | `; some code here` | +| | `JMP end_if` | +| `} else {` | `else_label:` | +| `some other code here` | `; some other code here`| +| `}` | `end_if:` | + +### Loops - for, while + +### `for` loop + +| **C Code** | **ASM Code** | +|------------- |---------------- | +| `for (a = 0; a < 10; a++) {` | `MOV eax, 0` | +| | `start_loop:` | +| | `CMP eax, 10` | +| | `JGE end_loop` | +| `// some code here` | `; some code here` | +| | `ADD eax, 1` | +| | `JMP start_loop` | +| `}` | `end_loop:` | + +### `while` loop + +| **C Code** | **ASM Code** | +|------------- |---------------- | +| `while (b < 5) {` | `start_loop:` | +| | `CMP ebx, 5` | +| | `JGE end_loop` | +| `// some code here` | `; some code that makes b greater or equal than 5` | +| | `JMP start_loop` | +| `}` | `end_loop:` | \ No newline at end of file -- GitLab