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

.global count_specs

count_specs:

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 the string of specifiers into a non-volatile register (X20)
MOV X21, '%' // Store ASCII code for % into X21. 
MOV X22, 'a' // Store ASCII code for a into X22.
MOV X23, 0 // Initialize loop counter for count_specs_loop. Works as offset for the char pointer of the input
MOV X25, 0 // Initialize an offset that is X23 + 1 for purposes of checking if the following character after % is a.
MOV X26, 0 // Initialize the counter for number of specifiers

count_specs_loop: 
LDRB W24, [X20, X23] // Loads a byte of the string into W24
CBZ W24, done_counting_specs // Hit null terminator, end of string found.

ADD X23, X23, 1 // Increment the counter by one

CMP X24, X21 // Compare if W24 (the loaded byte/char) is equal to X21 (the ASCII code for %)
B.EQ found_percentage
B count_specs_loop




found_percentage:
LDRB W24, [X20, X23] // Load the next char/byte after the %.
CBZ X24, done_counting_specs // Null terminator hit, we done
CMP X24, X22 // Compare if the loaded byte after the % is a "a"
B.EQ spec_counter_plus_plus // We found a "a" after the %, yay! (Increment the counter)
B count_specs_loop


spec_counter_plus_plus:
ADD X26, X26, 1 // Increment the counter by one
b count_specs_loop


done_counting_specs:
MOV X0, X26 // Move counter into 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
/*
    Declare .data here if you need.
*/
