Alex Larose

Generation

generate functionTue, 07 Mar 2023

Write a C callable assembler function that counts the number of characters in a string. The function should work for any string and character. The address of the string and character to be counted are passed as arguments according to the C function prototype: int count (char *string, char c); Since there are input arguments, your assembly function should use a stack frame. It should return the count to the calling C program in %eax. Use 32-bit quantities for all data. Even though chars have only 8 bits, they are stored in memory (and on the stack) as 32 bits in “little endian.” format. If the 32-bit value is moved from memory to a register, the char value is available in the 8 least significant bits of the register.

int count (char *string, char c){ int count = 0; while (*string != 0){ if (*string == c){ count += 1; } string += 1; } return count; }

Questions about programming?Chat with your personal AI assistant