diff options
Diffstat (limited to '')
| -rw-r--r-- | src/lib/int2str.s | 83 | ||||
| -rw-r--r-- | src/lib/tub2tsv.s | 86 |
2 files changed, 169 insertions, 0 deletions
diff --git a/src/lib/int2str.s b/src/lib/int2str.s new file mode 100644 index 0000000..82a2aaa --- /dev/null +++ b/src/lib/int2str.s @@ -0,0 +1,83 @@ +# -------------------------------------------- +# FUNCTION: int2str +# PURPOSE : Convert a 64 bit integer into ascii +# into ASCII encoding +# INPUTS : rdi = 64 bit integer +# rsi = address of buffer +# OUTPUTS : rax = address of string (points into the provided buffer) +# rdx = length of string +# CLOBBERS: +# -------------------------------------------- +.section .text +.globl int2str +.type int2str, @function +int2str: + pushq %rbx # save callee-saved register + + movq %rdi, %rax # integer to convert + movq %rsi, %rbx # buffer base + movq %rsi, %r9 # save original buffer + leaq 19(%rbx), %r8 # pointer to last char position + movq $10, %rcx # divisor + xorq %r10, %r10 # sign flag (0 = positive) + + # handle sign + testq %rax, %rax + jns .convert + negq %rax # if the number negative we negate it and + movq $1, %r10 # set sign flag + +.convert: + testq %rax, %rax + jnz .loop # check if the number is 0 + + # if the number is 0, we just set the char to '0', and length to 0. + movb $'0', (%r8) + movq %r8, %rax + movq $1, %rdx + jmp .shift_left # shift the result leftmust + +.loop: + xorq %rdx, %rdx # clear rdx before division + divq %rcx # divide rdx:rax by 10 + addb $'0', %dl # add ASCII base + movb %dl, (%r8) # move digit into buffer + decq %r8 # decrement pointer + testq %rax, %rax # while quotient is not 0, loop + jnz .loop + + incq %r8 # move back to first digit + + # add '-' if negative + testq %r10, %r10 + jns .calc_len + decq %r8 + movb $'-', (%r8) + +.calc_len: # count length of the string + movq %r8, %rax # string start + leaq 20(%r9), %rdx # end of buffer + subq %rax, %rdx # string length + +.shift_left: + # shift string to leftmost in buffer if not already there + cmpq %rax, %r9 + je .done # already leftmost + movq %rdx, %rcx # rcx = length of string + movq %rax, %rsi # rsi = source :: start of string + movq %r9, %rdi # rdi = destination :: original buff start + rep movsb # copy string left + # Found this online. It's a way to copy down values in memoery. + # movbs = Move String Byte.. + # It copies 1 byte from memoery to memoery and + # incrementing destination and incrementing source. + # Source :: %rsi + # Dest :: %rdi + # rep = repeat wow. + # It's a prefix that repeats an isntructution rcx times + # https://stackoverflow.com/questions/27804852/assembly-rep-movs-mechanism + movq %r9, %rax # return pointer to start + +.done: + popq %rbx + ret 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 |