Here is a solution using inline assembly in c program
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
char s1[] = "Hello World";
char s2[255] = {0};
int len = 0;
_asm
{
push esi
push edi
lea esi,s1
lea edi,s2
; get length of string s1
xor eax,eax
mov ecx,0ffh ; max length of string ?
repnz cmpsb ;locate 0 byte, if it exists
jnz l1 ; oops! not a null terminated string
not ecx ; make it an integer
and ecx,0000000ffh ; mask off high dword
lea edi,[len] ; save for output later
mov [edi],cx ;
;
;
; reverse the string
lea edi,s2 ; set up registers for moving string
lea esi,s1
add esi,ecx
dec esi
dec esi
xor eax,eax
l2: ; move each byte from ds:esi to es:edi in reverse order
; would be nice if we could use rep movsb, but we can't
; so have to do it the lard way.
mov al,[esi]
mov [edi],al
inc edi
dec esi
loopnz l2
mov [edi],0
l1: nop ; not found
pop edi
pop esi
}
cout << len << endl;
cout << s2 << endl;
return 0;
} Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343