escaping double quotes inside an inline asm statement

Programming, for all ages and all languages.
bobl
Posts: 16
Joined: Wed Jan 06, 2010 8:43 am

Re: escaping double quotes inside an inline asm statement

Post by bobl »

I stuck with my inline example above cos the compiler listing is in normal gas assembly and not in the syntax specific to INLINE assembly which is illustrated in the program below.

To test that eax is loaded with the string's address I added "recipient" which I filled with eax's contents and printed out.
It gives the correct result i.e. /dev/vcsa1

"char device [] = "/dev/vcsa1"; corrupts the string.
const char * device = "/dev/vcsa1"; doesn't.

Code: Select all

#include <iostream>
using namespace std;
int main(){
const char * device = "/dev/vcsa1";
const char * recipient = "";
__asm__ __volatile__("movl  %%eax,%%ebx"
                     :"=b"(recipient)
                     :"a"(device), "b"(recipient)
                     );
cout << recipient << "\n";
return 0;
}
Your advice has been very much appreciated. Thank you.
fronty
Member
Member
Posts: 188
Joined: Mon Jan 14, 2008 5:53 am
Location: Helsinki

Re: escaping double quotes inside an inline asm statement

Post by fronty »

bobl wrote:I stuck with my inline example above cos the compiler listing is in normal gas assembly and not in the syntax specific to INLINE assembly which is illustrated in the program below.
What do you mean with compiler listing? Program listing (or source code, who prints source nowadays)? And if you want normal assembly, why don't you write plain assembly?
To test that eax is loaded with the string's address I added "recipient" which I filled with eax's contents and printed out.
It gives the correct result i.e. /dev/vcsa1

"char device [] = "/dev/vcsa1"; corrupts the string.
const char * device = "/dev/vcsa1"; doesn't.
What do you mean with "corrupting"? I don't see any reason that would corrupt anything.

I still don't see any reason in inline assembly, why can't you do something like this?

Code: Select all

#include <stdio.h>

int
main(void)
{
        char device[] = "/dev/vcsa1";
        char *recipient = device;

        printf("%s\n", recipient);

        return 0;
}
bobl
Posts: 16
Joined: Wed Jan 06, 2010 8:43 am

Re: escaping double quotes inside an inline asm statement

Post by bobl »

fronty just for you

Code: Select all

#include <iostream>
using namespace std;
int main(){
//const char * device = "/dev/vcsa1";
//const char * recipient = "";
char device [] = "/dev/vcsa1";
char recipient [] = "";
__asm__ __volatile__("movl  %%eax,%%ebx"
                     :"=b"(recipient)
                     :"a"(device), "b"(recipient)
                     );
cout << recipient << "\n";
return 0;
}
produces

Code: Select all

me@me-desktop:~$ g++ fronty.cpp; ./a.out; rm a.out
@/dev/vcsa1
me@me-desktop:~$ g++ fronty.cpp; ./a.out; rm a.out
Segmentation fault <device as [] recipient as const char * 
me@me-desktop:~$ g++ fronty.cpp; ./a.out; rm a.out
�/dev/vcsa1
me@me-desktop:~$ g++ fronty.cpp; ./a.out; rm a.out
�/dev/vcsa1
me@me-desktop:~$ g++ fronty.cpp; ./a.out; rm a.out
 /dev/vcsa1  <<< this isn't correct ie theres a space in front
me@me-desktop:~$ g++ fronty.cpp; ./a.out; rm a.out
P/dev/vcsa1
me@me-desktop:~$ 
Re corruption Take your pick.
By contrast if we switch back to const char *
we always get the correct result. There is a difference.
i.e.

Code: Select all

me@me-desktop:~$ g++ fronty.cpp; ./a.out; rm a.out
/dev/vcsa1
me@me-desktop:~$ g++ fronty.cpp; ./a.out; rm a.out
/dev/vcsa1
me@me-desktop:~$ g++ fronty.cpp; ./a.out; rm a.out
/dev/vcsa1
me@me-desktop:~$ g++ fronty.cpp; ./a.out; rm a.out
/dev/vcsa1
me@me-desktop:~$ g++ fronty.cpp; ./a.out; rm a.out
/dev/vcsa1
me@me-desktop:~$ g++ fronty.cpp; ./a.out; rm a.out
/dev/vcsa1
me@me-desktop:~$
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: escaping double quotes inside an inline asm statement

Post by qw »

Check the assembly output that GCC produces.
Locked