I am working on a c++ code that needs to have inline assembly code to reverse the string. So if my input is : "qwerasd" the output should be "dsarewq". I thought of implementing this using stacks. My code is:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void reverseString(char* buffer, int len){
__asm {
push ecx
push edi
push eax
mov ecx, len
mov edi, buffer
top:
mov al, BYTE PTR [edi]
push al
inc edi
loop top
mov ecx, len
mov edi, buffer
refill:
pop al
mov BYTE PTR [edi], al
inc edi
loop refill
}
}
int main() {
char s[64];
char *ptr = s;
int size;
printf("\nEnter text: ");
scanf("%s", s);
size = strlen(s);
reverseString(ptr, size);
printf("\nThe new text is: %s\n\n", s);
exit(0);
}
I am trying to push the character one by one onto the stack and then just popping them one by one and storing it back in the string.
When I run the code I get the following error: Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
What am I doing wrong?