Page 2 of 2
Re: escaping double quotes inside an inline asm statement
Posted: Thu Jan 07, 2010 4:47 am
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.
Re: escaping double quotes inside an inline asm statement
Posted: Thu Jan 07, 2010 9:10 am
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;
}
Re: escaping double quotes inside an inline asm statement
Posted: Thu Jan 07, 2010 10:45 am
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:~$
Re: escaping double quotes inside an inline asm statement
Posted: Thu Jan 07, 2010 12:25 pm
by qw
Check the assembly output that GCC produces.