// u.cpp // // Generic C++ driver program to demonstrate // returning function results from assembly // language to C++. // // This is a variant of the c.cpp program // that supports UNICODE strings. #include #include #include #include #include #include #include // extern "C" namespace prevents // "name mangling" by the C++ // compiler. extern "C" { // asmMain is the assembly language // code's "main program": void asmMain( void ); // getTitle returns a pointer to a // string of characters from the // assembly code that specifies the // title of that program (that makes // this program generic and usable // with a large number of sample // programs in "The Art of 64-bit // Assembly Language." wchar_t *getwTitle( void ); // C++ function that the assembly // language program can call: int readLine( wchar_t *dest, int maxLen ); }; // readLine reads a line of text from the // user (from the console device) and stores // that string into the destinatio buffer // the first argument specifies. Strings // are limited in length to the value specified // by the second argument (minus 1). // // This function returns the number of // code points actually read, or -1 if there // was an error. // // Note that if the user enters too many // characters (maxlen or more code points) // then this function only returns the first // maxlen-1 characters. This is not considered // an error. int readLine( wchar_t *dest, int maxLen ) { // Note: wfgets returns NULL if there was // an error, else it returns a pointer to // the string data read (which will be the // value of the dest pointer). wchar_t *result = fgetws( dest, maxLen, stdin ); if( result != NULL ) { // Wipe out the new line character at the // end of the string: int len = wcslen( result ); if( len > 0 ) { dest[ len - 1 ] = 0; } return len; } return -1; // If there was an error. } int main(void) { // Get the assembly language program's title: try { wchar_t *wtitle = getwTitle(); _setmode(_fileno(stdout), _O_U16TEXT ); _setmode(_fileno(stdin), _O_U16TEXT ); wprintf( L"Calling %s:\n", wtitle ); asmMain(); wprintf( L"%s terminated\n", wtitle ); } catch(...) { wprintf ( L"Exception occurred during program execution\n" L"Abnormal program termination.\n" ); } }