From eae5db135d3874039fc1ddc8152ba1b0b6c77726 Mon Sep 17 00:00:00 2001 From: Navid Samanghoon Date: Mon, 6 Oct 2025 19:10:07 +0200 Subject: feat(outputGenerator.s): suggestion to how we print the sorted buffer --- src/outputGenerator.s | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 src/outputGenerator.s (limited to 'src') 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 + + -- cgit v1.2.3-70-g09d2