-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path19_Modulo_Without_Division_HRM_Level_24.asm
More file actions
40 lines (34 loc) · 1.36 KB
/
19_Modulo_Without_Division_HRM_Level_24.asm
File metadata and controls
40 lines (34 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
; ==============================================================
; 1. Speed-optimized (loop unrolling for small divisors)
; 2. Size-optimized (compact loop structure)
; Core concept: Compute A % B without division.
; ==============================================================
section .text
global _start
_start:
; --- Input simulation (replace with syscalls) ---
mov eax, 17 ; Dividend (A)
mov ebx, 5 ; Divisor (B) → 17 % 5 = 2
; === Approach 1: Speed-Optimized ===
; Goal: Minimize branches for small B (e.g., B ≤ 8)
mov ecx, eax ; ecx = A
.mod_loop_speed:
sub ecx, ebx ; ecx -= B
cmp ecx, ebx ; Compare ecx and B
jge .mod_loop_speed ; If ecx >= B, keep looping
mov [output_speed], ecx ;
; === Approach 2: Size-Optimized ===
; Goal: Minimal instruction bytes
mov edx, eax ; edx = A
.mod_loop_size:
sub edx, ebx ; edx -= B
cmp edx, ebx ; Compare edx and B
jge .mod_loop_size ; Continue if edx >= B
mov [output_size], edx
; --- Exit (Linux syscall) ---
mov eax, 60 ; sys_exit
xor edi, edi ; status 0
syscall
section .data
output_speed dd 0 ; Result from speed-optimized
output_size dd 0 ; Result from size-optimized