aboutsummaryrefslogtreecommitdiff
path: root/snippets/allocate.s
diff options
context:
space:
mode:
authormithe24 <mithe24@student.sdu.dk>2025-09-29 16:53:18 +0200
committermithe24 <mithe24@student.sdu.dk>2025-10-29 13:49:57 +0100
commit703af39239b9dbb8a4159cc39fc483e4cc8df1b6 (patch)
tree8d331b974bf430ee3a9df91fc0e5e04078fdd7f5 /snippets/allocate.s
parentc6d80b48f91d8eaadf0d87bdd0bba6a4c5f1324d (diff)
downloadsorter-703af39239b9dbb8a4159cc39fc483e4cc8df1b6.tar.gz
sorter-703af39239b9dbb8a4159cc39fc483e4cc8df1b6.zip
chore: Added provided code snippets
Diffstat (limited to '')
-rw-r--r--snippets/allocate.s20
1 files changed, 20 insertions, 0 deletions
diff --git a/snippets/allocate.s b/snippets/allocate.s
new file mode 100644
index 0000000..ef80957
--- /dev/null
+++ b/snippets/allocate.s
@@ -0,0 +1,20 @@
+# void *allocate(int n)
+#
+# A naive memory allocator that simply retrieves some new space from the OS.
+# It is not possible to deallocate the memory again.
+.globl allocate
+.type allocate, @function
+allocate:
+ push %rdi
+ # 1. Find the current end of the data segment.
+ movq $12, %rax # brk
+ xorq %rdi, %rdi # 0 means we retrieve the current end.
+ syscall
+ # 2. Add the amount of memory we want to allocate.
+ pop %rdi # the argument
+ push %rax # current end, which is where the allocated memory will start
+ addq %rax, %rdi # compute the new end
+ movq $12, %rax # brk
+ syscall
+ pop %rax # the old end, which is the address of our allocated memory
+ ret