|
| 1 | +section .text |
| 2 | +global row |
| 3 | +global column |
| 4 | + |
| 5 | +%macro store_num 0 |
| 6 | + inc r8 ; increments counter |
| 7 | + |
| 8 | + mov eax, r10d ; eax now holds number |
| 9 | + |
| 10 | + xor rdx, rdx |
| 11 | + mov r10d, 10 |
| 12 | + div r10d ; divide by 10 to account for an extra multiplication in accumulating loop |
| 13 | + |
| 14 | + cmp r9b, 1 |
| 15 | + jne %%store_num ; if sign flag is not set, proceed with storing it |
| 16 | + ; otherwise negate num before storing it |
| 17 | + |
| 18 | + neg eax |
| 19 | + |
| 20 | +%%store_num: |
| 21 | + stosd ; store number in buffer |
| 22 | +%endmacro |
| 23 | + |
| 24 | +; size_t row(int32_t *buffer, const char *input, size_t index); |
| 25 | +row: |
| 26 | + ; rdi - output int32_t buffer |
| 27 | + ; rsi - input string |
| 28 | + ; rdx - index from the row to be extracted as a uint64_t |
| 29 | + ; return is the size of buffer in rax |
| 30 | + |
| 31 | + ; The algorithm is: |
| 32 | + ; |
| 33 | + ; The input string is looped over until rdx is 1. |
| 34 | + ; Each newline marks the end of a row and decreases rdx by 1. |
| 35 | + ; |
| 36 | + ; Then the desired row is looped over and each number is accumulated in r10. |
| 37 | + ; Each space marks the end of a num |
| 38 | + ; and a newline or a NULL marks the end of the entire row. |
| 39 | + ; |
| 40 | + ; At the beginning of each number, there's a check for the sign char. |
| 41 | + ; |
| 42 | + ; At the end of each number and of the row, the accumulator is adjusted for sign |
| 43 | + ; and divided by 10 to account for an extra multiplication in the loop |
| 44 | + ; then it's stored in the output buffer. |
| 45 | + ; There's also a counter for the numbers, for returning at the end. |
| 46 | + ; |
| 47 | + ; If it's not the end of the row, the loop continues |
| 48 | + ; It it is, the counter is moved to rax |
| 49 | + |
| 50 | + xor r8, r8 ; counter |
| 51 | +.row_main_loop: |
| 52 | + cmp rdx, 1 ; matrix is 1-indexed |
| 53 | + je .row_copy ; found desired row |
| 54 | + |
| 55 | +.find_next_row: |
| 56 | + lodsb |
| 57 | + |
| 58 | + cmp al, 10 ; newline |
| 59 | + jne .find_next_row |
| 60 | + |
| 61 | + dec rdx ; one row less |
| 62 | + jmp .row_main_loop |
| 63 | + |
| 64 | +.row_copy: |
| 65 | + xor r9, r9 |
| 66 | + xor r10, r10 ; accumulator for the number |
| 67 | + xor rax, rax |
| 68 | +.check_sign: |
| 69 | + lodsb |
| 70 | + cmp al, '-' |
| 71 | + sete r9b ; sign flag |
| 72 | + je .prepare_next_iteration |
| 73 | +.row_copy_loop: |
| 74 | + cmp al, 10 |
| 75 | + je .end_copy ; newline marks end of the row |
| 76 | + |
| 77 | + cmp al, 0 |
| 78 | + je .end_copy ; NULL marks end of the row |
| 79 | + |
| 80 | + cmp al, ' ' |
| 81 | + je .end_num ; space marks end of the num |
| 82 | + ; otherwise the number continues |
| 83 | + |
| 84 | + sub al, '0' ; fromEnum |
| 85 | + |
| 86 | + add r10, rax ; accumulates |
| 87 | + imul r10, r10, 10 ; multiplies by 10 to accumulate in next iteration |
| 88 | + ; accumulator must be adjusted for an extra multiplication at the end |
| 89 | + |
| 90 | +.prepare_next_iteration: |
| 91 | + lodsb |
| 92 | + jmp .row_copy_loop |
| 93 | + |
| 94 | +.end_num: |
| 95 | + store_num ; macro |
| 96 | + jmp .row_copy |
| 97 | + |
| 98 | +.end_copy: |
| 99 | + store_num ; macro |
| 100 | + mov rax, r8 ; moves counter to rax for returning |
| 101 | + |
| 102 | + ret |
| 103 | + |
| 104 | +; size_t column(int32_t *buffer, const char *input, size_t index); |
| 105 | +column: |
| 106 | + ; rdi - output int32_t buffer |
| 107 | + ; rsi - input string |
| 108 | + ; rdx - index from the column to be extracted as a uint64_t |
| 109 | + ; return is the size of buffer in rax |
| 110 | + |
| 111 | + ; The algorithm is: |
| 112 | + ; The input string is looped over until rdx is 1, which marks the desired column. |
| 113 | + ; |
| 114 | + ; As this process is repeated for each new element, rdx is saved in r11 beforehand. |
| 115 | + ; |
| 116 | + ; Each space marks the end of a number and decreases rdx by 1, |
| 117 | + ; the NULL char marks the end of the string and returns the function. |
| 118 | + ; |
| 119 | + ; Once found the element in the desired column, it's looped over and accumulated in r10. |
| 120 | + ; At the beginning of each number, there's a check for the sign char. |
| 121 | + ; |
| 122 | + ; At the end of each number, the accumulator is adjusted for sign |
| 123 | + ; and divided by 10 to account for an extra multiplication in the loop |
| 124 | + ; then it's stored in the output buffer. |
| 125 | + ; There's also a counter for the numbers, for returning at the end. |
| 126 | + ; |
| 127 | + ; A space, a newline or NULL marks the end of a number. |
| 128 | + ; In case of a NULL, the number is stored and the function returns right away. |
| 129 | + ; In the other cases, the number is stored and rdx is restored to loop over the input string |
| 130 | + ; in search of the next desired element. |
| 131 | + |
| 132 | + xor r8, r8 ; counter |
| 133 | + mov r11, rdx |
| 134 | +.column_main_loop: |
| 135 | + cmp rdx, 1 ; matrix is 1-indexed |
| 136 | + je .elem_copy ; found desired column |
| 137 | + |
| 138 | +.find_next_elem: |
| 139 | + lodsb |
| 140 | + |
| 141 | + cmp al, 0 |
| 142 | + je .return ; NULL marks end of input and function returns right away |
| 143 | + |
| 144 | + cmp al, ' ' ; num separator |
| 145 | + jne .find_next_elem |
| 146 | + |
| 147 | + dec rdx ; decreases counter until desired column |
| 148 | + jmp .column_main_loop |
| 149 | + |
| 150 | +.elem_copy: |
| 151 | + xor r10, r10 ; accumulator for the number |
| 152 | + xor rax, rax |
| 153 | +.check_sign: |
| 154 | + lodsb |
| 155 | + cmp al, '-' |
| 156 | + sete r9b ; sign flag |
| 157 | + je .prepare_next_iteration |
| 158 | +.elem_copy_loop: |
| 159 | + cmp al, 10 |
| 160 | + je .end_num ; newline marks end of the number |
| 161 | + |
| 162 | + cmp al, ' ' |
| 163 | + je .end_num ; space marks end of the number |
| 164 | + |
| 165 | + cmp al, 0 |
| 166 | + je .end_copy ; NULL marks end of the string |
| 167 | + |
| 168 | + sub al, '0' ; fromEnum |
| 169 | + |
| 170 | + add r10, rax ; accumulates |
| 171 | + imul r10, r10, 10 ; multiplies by 10 to accumulate in next iteration |
| 172 | + ; accumulator must be adjusted for an extra multiplication at the end |
| 173 | + |
| 174 | +.prepare_next_iteration: |
| 175 | + lodsb |
| 176 | + jmp .elem_copy_loop |
| 177 | + |
| 178 | +.end_num: |
| 179 | + ; here it's end of current number, but not of the string |
| 180 | + |
| 181 | + store_num ; macro |
| 182 | + |
| 183 | + mov rdx, r11 ; as the string doesn't end here, there might be another row |
| 184 | + jmp .column_main_loop |
| 185 | + |
| 186 | +.end_copy: |
| 187 | + ; here it's end of the number and of the input string |
| 188 | + ; so the number must be stored and the function should return soon after |
| 189 | + |
| 190 | + store_num ; macro |
| 191 | + |
| 192 | +.return: |
| 193 | + mov rax, r8 ; moves counter to rax for returning |
| 194 | + |
| 195 | + ret |
| 196 | + |
| 197 | +%ifidn __OUTPUT_FORMAT__,elf64 |
| 198 | +section .note.GNU-stack noalloc noexec nowrite progbits |
| 199 | +%endif |
0 commit comments