/*  Name: Tristan Shabbick
    Pledge: I pledge my honor that I have abided by the Stevens Honor System.
 */

.global pringle
.global concat_array

pringle:

SUB SP, SP, 96 // Create stack frame
STR X30, [SP, 0] // Save link register
STR X19, [SP, 8] // Save callee-saved registers
STR X20, [SP, 16] // Save callee-saved registers
STR X21, [SP, 24] // Save callee-saved registers
STR X22, [SP, 32] // Save callee-saved registers
STR X23, [SP, 40] // Save callee-saved registers
STR X24, [SP, 48] // Save callee-saved registers
STR X25, [SP, 56] // Save callee-saved registers
STR X26, [SP, 64] // Save callee-saved registers
STR X27, [SP, 72] // Save callee-saved registers
STR X28, [SP, 80] // Save callee-saved registers

ADR X19, pringle_output_string // X19 = address of the output buffer
MOV X20, X0 // X20 = the format string
MOV X21, X1 // X21 = current array (starts as arr1)
MOV X22, X2 // X22 = length of current array
MOV X23, X3 // X23 = arr2 (next up)
MOV X24, X4 // X24 = length of arr2
MOV X25, X5 // X25 = arr3 (after that)
MOV X26, X6 // X26 = length of arr3
MOV X27, 0 // X27 = index into the INPUT string
MOV X28, 0 // X28 = index into the OUTPUT buffer

pringle_loop:
    LDRB W9, [X20, X27] // W9 = current char of the input string
    CBZ W9, pringle_done // Null terminator, we are finished
    CMP W9, '%' // Is it a %?
    B.NE pringle_copy_char // No, just copy it
    ADD X10, X27, 1 // Index of the char after the %
    LDRB W10, [X20, X10] // W10 = char after the %
    CMP W10, 'a' // Is it an 'a'?
    B.NE pringle_copy_char // No, so this % is just a normal character

// We found "%a": expand the current array
MOV X0, X21 // Arg 1: the current array
MOV X1, X22 // Arg 2: its length
BL concat_array // X0 = pointer to the "10 200 30 " string

MOV X10, 0 // Index into the concat_array result
pringle_copy_result:
LDRB W11, [X0, X10] // Next char of the result
CBZ W11, pringle_result_done // Null terminator, done copying
STRB W11, [X19, X28] // Put it in the output buffer
ADD X28, X28, 1 // Output index moves by one per char written
ADD X10, X10, 1 // Move along in the result
B pringle_copy_result

pringle_result_done:
MOV X21, X23 // Shift the arrays down so the next "%a"
MOV X22, X24 // uses the next array
MOV X23, X25 // arr3 becomes the next array up
MOV X24, X26 // Same for its length
ADD X27, X27, 2 // Skip over both chars of "%a" in the input
B pringle_loop

pringle_copy_char:
STRB W9, [X19, X28] // Copy the char to the output buffer
ADD X28, X28, 1 // Advance output index
ADD X27, X27, 1 // Advance input index
B pringle_loop

pringle_done:
STRB WZR, [X19, X28] // Null-terminate the output

ADR X0, pringle_fmt // printf("%s", output)
MOV X1, X19 // the output buffer to be printed
BL printf // print the output string

MOV X0, X28 // Return value = length of the OUTPUT string

LDR X30, [SP, 0] // Delete stack frame
LDR X19, [SP, 8] // Delete stack frame
LDR X20, [SP, 16] // Delete stack frame
LDR X21, [SP, 24] // Delete stack frame
LDR X22, [SP, 32] // Delete stack frame
LDR X23, [SP, 40] // Delete stack frame
LDR X24, [SP, 48] // Delete stack frame
LDR X25, [SP, 56] // Delete stack frame
LDR X26, [SP, 64] // Delete stack frame
LDR X27, [SP, 72] // Delete stack frame
LDR X28, [SP, 80] // Delete stack frame
ADD SP, SP, 96 // Finish deleting stack frame
RET // Return from procedure/function call

.data
pringle_fmt: .asciz "%s"
pringle_output_string: .fill 1024, 1, 0
