Skip to content
Snippets Groups Projects
Commit d887a373 authored by Robert GRÁNCSA's avatar Robert GRÁNCSA
Browse files

Merge branch 'second' into 'master'

readme - part 2

See merge request iocla/tema-1-2025-private!1
parents 97278a6a 6f8b429d
No related branches found
No related tags found
No related merge requests found
......@@ -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 than or equal to 5` |
| | `JMP start_loop` |
| `}` | `end_loop:` |
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment