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

.global itoascii

itoascii:
   SUB SP, SP, 16 // Create stack frame (can go to 8)
   STR X30, [SP, 8] // Create Stack Frame (can to to 0)

   MOV X1, X0 // Move the input value into a temporary register
   CBZ X1, handle_zero // If X1 is zero, handle it separately because I am lazy
   MOV X3, 10 // For use in UDIV where it takes only two registers
   ADR X5, buffer // Load the address of buffer into X5
   MOV X4, #0 // Initialize buffer index to zero
   MOV X8, 0 // Buffer clear loop counter

   itoascii_buffer_clear_loop:
   CMP X8, 128 // Compare the buffer index to 128
   B.EQ convert_digit // If the buffer is full, start converting digits

   STRB WZR, [X5, X8] // Set each byte in the entire buffer to 0
   ADD X8, X8, 1 // Increment the buffer index
   B itoascii_buffer_clear_loop // Repeat until the entire buffer is cleared



convert_digit:
   CBZ X1, done_converting_digits // If X1 is zero, no more digits to convert
   UDIV X2, X1, X3 // Divide X1 by 10 (1234 -> 123)
   MUL X6, X2, X3 // Multiply the quotient by 10 (123 -> 1230)
   SUB X2, X1, X6 // Subtract the result from X1 to get the remainder (1234 - 1230 = 4)
   ADD X2, X2, #'0' // Add the ASCII value of '0' to the remainder to get the ASCII value of the digit (4 + 48 = 52)
   STRB W2, [X5, X4] // Store the ASCII value in the buffer
   ADD X4, X4, #1 // Increment the buffer index
   UDIV X1, X1, X3 // Divide X1 by 10 to remove the rightmost digit
   B convert_digit // Repeat until all digits have been converted and stored in the buffer

done_converting_digits:
   SUB X4, X4, #1 // Decrement X4 to get the index of the last valid byte in the buffer
   MOV X6, #0 // Initialize a counter for the start of the buffer

reverse_buffer:
   CMP X6, X4 // Compare the start and end pointers
   B.GE done_reversing // If they meet or cross, we're done reversing. Changed from "BGE" to "B.GE" @ 4:38 PM EST, 11/10/2023. Results seem to be identical.
   LDRB W2, [X5, X6] // Load the byte at the start pointer
   LDRB W3, [X5, X4] // Load the byte at the end pointer
   STRB W2, [X5, X4] // Store the start byte at the end
   STRB W3, [X5, X6] // Store the end byte at the start
   ADD X6, X6, #1 // Increment the start pointer
   SUB X4, X4, #1 // Decrement the end pointer
   B reverse_buffer // Repeat until the entire buffer is reversed

done_reversing:
   LDR X30, [SP, 8] // Delete Stack Frame 
   ADD SP, SP, 16 // Delete Stack Frame
   MOV X0, X5 // Move buffer into X0 for return
   RET           // Return from procedure/function call


handle_zero:
   ADR X5, buffer // Load the address of buffer into X5
   MOV W2, #'0' // Move the ASCII value of '0' into W2
   STRB W2, [X5] // Store the ASCII value in the buffer
   MOV W2, 0 // Add a null terminator after '0'
   STRB W2, [X5, 1] // Store null terminator after 0
   b done_reversing 

.data
    /* Put the converted string into buffer,
       and return the address of buffer */
    buffer: .fill 128, 1, 0


