-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path21_Integer_Division_No_Remainder_HRM_Level_26.asm
More file actions
46 lines (39 loc) · 1.47 KB
/
21_Integer_Division_No_Remainder_HRM_Level_26.asm
File metadata and controls
46 lines (39 loc) · 1.47 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
41
42
43
44
45
46
; ==============================================================
; 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 = 3
; === Approach 1: Speed-Optimized ===
; Goal: Minimize branches for small B (e.g., B ≤ 8)
xor ecx, ecx ; ecx = quotient
.div_loop_speed:
sub eax, ebx ; A -= B
js .output_speed ; If A < 0, exit
inc ecx ; quotient++
jmp .div_loop_speed
.output_speed:
mov [output_speed], ecx
; === Approach 2: Size-Optimized ===
; Goal: Minimal instruction bytes
xor edx, edx ; edx = quotient
.div_loop_size:
sub eax, ebx ; A -= B
js .output_size ; Exit if A < 0
inc edx ; quotient++
jmp .div_loop_size
.output_size:
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
; For large A/B, use bit-shifting (e.g., div via shifts/adds).