aboutsummaryrefslogtreecommitdiff
path: root/src/lib/tub2tsv.s
diff options
context:
space:
mode:
authormithe24 <mithe24@student.sdu.dk>2025-10-26 15:38:43 +0100
committermithe24 <mithe24@student.sdu.dk>2025-10-29 13:49:57 +0100
commit2e39f481369d708cf2c723136c3bf4c765d6c994 (patch)
tree0e5d7b8588ad8d31875725cfabbc17d876037038 /src/lib/tub2tsv.s
parent88d4c92a5a757ed4ee88373e9ae53bfe27041e7f (diff)
downloadsorter-2e39f481369d708cf2c723136c3bf4c765d6c994.tar.gz
sorter-2e39f481369d708cf2c723136c3bf4c765d6c994.zip
fix(printing & int2str): Better printing and int to string func
Diffstat (limited to 'src/lib/tub2tsv.s')
-rw-r--r--src/lib/tub2tsv.s86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/lib/tub2tsv.s b/src/lib/tub2tsv.s
new file mode 100644
index 0000000..558c51e
--- /dev/null
+++ b/src/lib/tub2tsv.s
@@ -0,0 +1,86 @@
+# --------------------------------------------
+# FUNCTION: tub2tsv
+# PURPOSE : take a buffer of coordinates
+# and print each coordinate on a line
+# INPUTS : rdi = pointer to buffer,
+# rsi = number of coordinates
+# OUTPUTS : rax = address to string of tab seperated values
+# rdx = number of bytes
+# CLOBBERS:
+# --------------------------------------------
+.section .text
+.globl tub2tsv
+.type tub2tsv, @function
+tub2tsv:
+ # save callee-saved registers
+ pushq %r12
+ pushq %r13
+ pushq %r14
+ pushq %r15
+ pushq %rbx
+
+ movq %rdi, %r12 # r12 = address to array of coordinates
+ movq %rsi, %r13 # r13 = count of coordinates
+
+ # allocate memory based on number of coordinates and max integer size
+ # maybe allocate in chunks might be better, as to waste less memory
+ movq %rsi, %rdi
+ imulq $42, %rdi # 20 bytes for each coordindate + tab
+ incq %rdi # +1 byte for null terminator
+ call allocate
+ movq %rax, %r14 # r14 = output buffer start
+ movq %rax, %r15 # r15 = current write pos
+
+ xorq %rbx, %rbx # loop counter
+
+.tub2tsv_loop:
+ cmpq %r13, %rbx # if loop counter >= count of coordinates
+ jge .tub2tsv_exit # done
+
+ # get addrss to current tuple
+ movq (%r12, %rbx, 8), %rdi # rdi = array[loop counter]
+ pushq %rdi # save pointer to current tuple
+
+ # convert first integer to ASCII
+ movq (%rdi), %rdi # rdi = first int
+ movq %r15, %rsi # rsi = current write pos
+ call int2str # rax = string start, rdx = length
+ addq %rdx, %r15 # advance write pos with num bytes written
+
+ # write tab
+ movb $'\t', (%r15)
+ incq %r15
+
+ # convert second integer to ASCII
+ popq %rdi # restore pointer to tuple
+ movq 8(%rdi), %rdi # rdi = second int
+ movq %r15, %rsi # rsi = current write pos
+ call int2str # rax = string start, rdx = length
+ addq %rdx, %r15 # advance write pos with num bytes written
+
+ # write newline
+ movb $'\n', (%r15)
+ incq %r15
+
+ incq %rbx
+ jmp .tub2tsv_loop
+
+.tub2tsv_exit:
+ # add null terminator
+ movb $0, (%r15)
+
+ # move address to output register
+ movq %r14, %rax
+
+ # return length in rdx
+ movq %r15, %rdx
+ subq %r14, %rdx
+
+ # restore callee-saved registers
+ popq %rbx
+ popq %r15
+ popq %r14
+ popq %r13
+ popq %r12
+
+ ret