aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2023-06-15 22:09:25 +0200
committerMarvin Borner2023-06-15 22:13:27 +0200
commit47d30d97b707b575d5d68e2b61aade245585734b (patch)
treee6bc7afba1761864365b62c5e59a987c38588f35
parent746a227e08e35abbf556be228f37cfbcfc60ee41 (diff)
Cool strategies
-rw-r--r--readme.md18
-rw-r--r--sort.asm33
2 files changed, 46 insertions, 5 deletions
diff --git a/readme.md b/readme.md
index 6d096c0..2531502 100644
--- a/readme.md
+++ b/readme.md
@@ -1,8 +1,18 @@
# Sleepsort
-> efficiently sleeping
+> efficiently sleeping with (sub-)nanosecond precision and asm threads
-## Installation
+## Dependencies
- make
- ./sort
+- ed
+- nasm
+- gcc
+- make
+
+## Usage
+
+``` bash
+ed sort.asm # edit array and choose coolsleep/boringsleep
+make
+./sort
+```
diff --git a/sort.asm b/sort.asm
index d089dc9..9b3569b 100644
--- a/sort.asm
+++ b/sort.asm
@@ -24,7 +24,36 @@ end:
mov rdi, 0
syscall ; exit
+; this works sometimes (if numbers are good)
+; uses busy loop
+coolsleep:
+ mov rax, 0x4242424
+ mov rcx, rsi
+ mul rcx
+.loop:
+ dec rax
+ jnz .loop
+ ret
+
+; this works on most normal CPUs (if numbers are good)
+; uses nanosleep syscall
+boringsleep:
+ mov rax, 0x424242
+ mov rcx, rsi
+ mul rcx
+ push qword rax ; ns
+ push qword 0 ; s
+ mov rax, 0x23
+ mov rdi, rsp
+ xor rsi, rsi
+ syscall
+ add rsp, 16
+ ret
+
print:
+ push rsi
+ call boringsleep ; or coolsleep
+ pop rsi
mov rdi, format
xor rax, rax
call printf
@@ -32,7 +61,7 @@ print:
thread:
lea rax, print
- mov rsi, [array + (ecx - 1) * 4]
+ mov esi, [array + (ecx - 1) * 4]
call thread_run
ret
@@ -58,6 +87,8 @@ child:
;;; data
+section .data
+
array: dd 3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3
size: equ ($-array)/4
format: db "%d", 10, 0