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

.global concat_array
.global itoascii

concat_array:

SUB SP, SP, 64 // Create stack frame
STR X30, [SP, 0] // Create Stack Frame

STR X20, [SP, 8] //  Create Stack Frame
STR X21, [SP, 16] // Create Stack Frame
STR X22, [SP, 24] // Create Stack Frame
STR X23, [SP, 32] // Create Stack Frame
STR X24, [SP, 40] // Create Stack Frame
STR X25, [SP, 48] // Create Stack Frame
STR X26, [SP, 56] // Create Stack Frame


MOV X20, X0 // Move X0 (the array) into X20 as a temporary register in the stack.
MOV X21, X1 // Move X1 (the length of the array) into X21 as a temporary register. 

MOV X22, 0 // Initialize offset counter for calling toascii to everything in the array.
MOV X23, 0 // Initialize loop counter for storing each character in the buffer one char at a time.
MOV X26, 0 // Initialize loop counter for counting elements processed

ADR X24, concat_array_outstr // Store the address of the buffer in X24

MOV X25, ' ' // ASCII for space
MOV X8, 0 // Loop counter for clearing out the buffer

concat_array_memset:
   CMP X8, 1024 // Compare if the counter = the length of the buffer
   B.EQ concat_array_loop // Branch away if they are equal (clearing is done)
   STRB WZR, [X24, X8] //  Set each byte in the entire buffer to 0
   ADD X8, X8, 1 // Increment counter by 1
   B concat_array_memset // Branch back to concat_array_memset


concat_array_loop_prior:
STRB W25, [X24, X23] // Add a space character after the last character
ADD X22, X22, 8 // Add one to the offset for the integer array
ADD X23, X23, 1 // Add one to the offset for character ptr
ADD X26, X26, 1 // Increment the loop for counting elements processed

concat_array_loop:
   CMP X21, X26 // Check if the length of the array is equal to the counter. If so, all elements have been processed.
   B.EQ done_concat_array
   LDR X0, [X20, X22] // Stores the next int from the given array (W0) with an offset of X22 (10 out of {10, 200, 30})
   BL itoascii // Branch to itoascii with the byte from the array in X0 as the input params.
   MOV X10, 0 // Reset offset for character pointer
      char_to_buffer: 

      LDRB W7, [X0, X10] // X0 is now the character pointer. Get the first byte from it by an offset of X23.
      CBZ W7, concat_array_loop_prior // Check if the byte stored was the null terminator
      STRB W7, [X24, X23] // Stores the non-null terminator byte into the buffer
      ADD X23, X23, 1 // Add one to the offset for CHARACTERS in the buffer   
      ADD X10, X10, 1 // Add one to the character pointer offset


      B char_to_buffer

done_concat_array:
MOV X0, X24 // Moves buffer to X0 for return value


LDR X30, [SP, 0] // Delete Stack Frame

LDR X20, [SP, 8] //  Delete Stack Frame
LDR X21, [SP, 16] // Delete Stack Frame
LDR X22, [SP, 24] // Delete Stack Frame
LDR X23, [SP, 32] // Delete Stack Frame
LDR X24, [SP, 40] // Delete Stack Frame
LDR X25, [SP, 48] // Delete Stack Frame
LDR X26, [SP, 56] // Delete Stack Frame
ADD SP, SP, 64 // Delete Stack Frame
RET           // Return from procedure/function call



.data
    /* Put the converted string into concat_array_outstrer,
       and return the address of concat_array_outstr */
    concat_array_outstr:  .fill 1024, 1, 0

