aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNavid Samanghoon <nsama24@student.sdu.dk>2025-10-06 19:10:07 +0200
committermithe24 <mithe24@student.sdu.dk>2025-10-29 13:49:57 +0100
commiteae5db135d3874039fc1ddc8152ba1b0b6c77726 (patch)
tree8dc18ac26b9ad43ad64fef6eea115416961ae933
parent6c8e0df8edc7fdf3b0629746dd6e70c1b5a397cd (diff)
downloadsorter-eae5db135d3874039fc1ddc8152ba1b0b6c77726.tar.gz
sorter-eae5db135d3874039fc1ddc8152ba1b0b6c77726.zip
feat(outputGenerator.s): suggestion to how we print the sorted buffer
Diffstat (limited to '')
-rw-r--r--src/outputGenerator.s142
1 files changed, 142 insertions, 0 deletions
diff --git a/src/outputGenerator.s b/src/outputGenerator.s
new file mode 100644
index 0000000..8a885d1
--- /dev/null
+++ b/src/outputGenerator.s
@@ -0,0 +1,142 @@
+.section .data
+newline:
+ .byte 10 # '\n'
+
+tab:
+ .byte 9 # '\tab'
+
+
+# --------------------------------------------
+# FUNCTION: print_tab
+# PURPOSE : print a tab character ('\t')
+# INPUTS :
+# OUTPUTS :
+# CLOBBERS: rax, rdi, rsi, rdx
+# --------------------------------------------
+.globl print_tab
+.type print_tab, @function
+print_tab:
+ movq $1, %rax # write
+ movq $1, %rdi # stdout
+ movq $tab, %rsi # load tab character
+ movq $1, %rdx # 1 byte
+ syscall
+ ret
+
+
+# --------------------------------------------
+# FUNCTION: print_newline
+# PURPOSE : print a newline character ('\n')
+# INPUTS :
+# OUTPUTS :
+# CLOBBERS: rax, rdi, rsi, rdx
+# --------------------------------------------
+.globl print_newline
+.type print_newline, @function
+print_newline:
+ movq $1, %rax # write
+ movq $1, %rdi # stdout
+ movq $newline, %rsi # load newline character
+ movq $1, %rdx # 1 byte
+ syscall
+ ret
+
+
+# --------------------------------------------
+# FUNCTION: print_buffer
+# PURPOSE : take a buffer of coordinates and print each coordinate on a line
+# INPUTS : rdi = pointer to buffer, rsi = number of coordinates
+# OUTPUTS :
+# CLOBBERS: rax, rdi,
+# --------------------------------------------
+.globl print_buffer
+.type print_buffer, @function
+print_buffer:
+ cmpq $0, %rsi
+ jle done # if n > 0 print_coordinate
+
+ movq %rdi, %rdx # keep pointer here, rdi will be used for printNum
+
+loop:
+ # print x and tab
+ movq (%rdx), %rdi # load x
+ call printNum
+ push %rdx # save pointer
+ push %rsi # save n
+ call print_tab
+ pop %rsi # retrieve n
+ pop %rdx # retrieve pointer
+
+ # print y and newline
+ movq 8(%rdx), %rdi # load y
+ call printNum
+ push %rdx # save pointer
+ push %rsi # save n
+ call print_newline
+ pop %rsi # retrieve n
+ pop %rdx # retrieve pointer
+
+ # move to next coordinate and decq n
+ addq $16, %rdx
+ decq %rsi
+
+ cmpq $0, %rsi
+ jg loop
+
+done:
+ret
+
+
+# Print RDI as an unsigned integer without any new line.
+# Note: the function does not follow the ordinary calling convention,
+# but restores all registers.
+.type printNum, @function
+.globl printNum
+printNum:
+ push %rbp
+ movq %rsp, %rbp
+
+ # save
+ push %rax
+ push %rdi
+ push %rsi
+ push %rdx
+ push %rcx
+ push %r8
+ push %r9
+
+ movq %rdi, %rax # arg
+ movq $0, %r9 # digit count
+.LprintNum_convertLoop:
+ movq $0, %rdx
+ movq $10, %rcx
+ idivq %rcx
+ addq $48, %rdx # '0' is 48
+ push %rdx
+ addq $1, %r9
+ cmpq $0, %rax
+ jne .LprintNum_convertLoop
+.LprintNum_printLoop:
+ movq $1, %rax # sys_write
+ movq $1, %rdi # stdout
+ movq %rsp, %rsi # buf
+ movq $1, %rdx # len
+ syscall
+ addq $8, %rsp
+ addq $-1, %r9
+ jne .LprintNum_printLoop
+
+ # restore
+ pop %r9
+ pop %r8
+ pop %rcx
+ pop %rdx
+ pop %rsi
+ pop %rdi
+ pop %rax
+
+ movq %rbp, %rsp
+ pop %rbp
+ ret
+
+