aboutsummaryrefslogtreecommitdiff
path: root/src/main.s
blob: 036671e4b320c5bd85e62f0185e317d665b68970 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
.section .data
    isort:
        .string "isort"
    qsort:
        .string "qsort"


.section .text
.globl _start
_start:
    
    cmpq $2, (%rsp)             # Check if there are only two arguments
    jne .algorithm_selected      # If not, go to algorithm_selected

.default:
    movq 16(%rsp), %rdi         # Select first argument as file name
    movq $0, %rsi               # Select read only
    movq $0, %rdx               # Unused mode for read only
    movq $2, %rax               # Select open syscall
    syscall                     # Open file, file descriptor returned in rax
    movq %rax, %r13             # Save file descriptor in r13
    jmp .select_quicksort        # Select quicksort

.algorithm_selected:
    movq 32(%rsp), %rdi         # Select third argument as file name
    movq $0, %rsi               # Select read only
    movq $0, %rdx               # Unused mode for read only
    movq $2, %rax               # Select open syscall
    syscall                     # Open file, file descriptor returned in rax
    movq %rax, %r13             # Save file descriptor in r13

.check_insertion_sort:
    movq 24(%rsp), %rdi         # Select algorithm string
    movq $isort, %rsi           # Select "isort" to compare with
    call strcmp                 # Compare
    cmp $1, %rax                # If the strings where equal
    je .select_insertionsort     # Select insertionsort

.check_quicksort:
    movq 24(%rsp), %rdi         # Select algorithm string
    movq $qsort, %rsi           # Select "qsort" to compare with
    call strcmp                 # Compare
    cmp $1, %rax                # If the strings where equal
    je .select_quicksort         # Go to selection_done

.else_invalid_algorithm:
    movq $60, %rax              # Select exit syscall
    movq $1, %rdi               # Exit code 1
    syscall                     # Exit

.select_insertionsort:
    movq $insertion_sort, %r14  # Select insertion_sort in r14
    jmp .the_rest

.select_quicksort:
    movq $quick_sort, %r14      # Select quicksort in r14
    jmp .the_rest 

.the_rest:
    # Convert to array
    movq %r13, %rdi             # Select file descriptor
    call make_array_from_file   # Convert file to array format
    movq %rdx, %r15             # Save length of array in r15

    # Sort
    movq %rax, %rdi             # Select address of array
    movq %r15, %rsi             # Select number of coordinates
    movq $1, %rdx               # Sort by key 1
    call *%r14                  # Sort the array
    
    # Convert array to string
    # in tsv format
    movq %rax, %rdi             # Select the pointer to the array
    movq %r15, %rsi             # Select length of array
    call tub2tsv                # Print array

    # print string to stdout
    movq %rax, %rsi
    movq $1, %rax
    movq $1, %rdi
    syscall

    # Exit
    movq $60, %rax              
    movq $0, %rdi               # Exit code 0
    syscall