Page 1 of 2

escaping double quotes inside an inline asm statement

Posted: Wed Jan 06, 2010 12:04 pm
by bobl
Here's a nasm statement that I'd like to convert to g++ inline asm

Code: Select all

device db "/dev/vcsa1", 0
In think it should be something like

Code: Select all

asm (" device .byte "/dev/vcsa1", 0 ")
but I'm not sure how to escape the internal double quotes so that
they don't interfere with the enclosing double quotes.

BTW is it "device" or "device":
Any help appreciated.

Re: escaping double quotes inside an inline asm statement

Posted: Wed Jan 06, 2010 12:13 pm
by fronty
It's just basic string literal, escape them like everywhere.

Re: escaping double quotes inside an inline asm statement

Posted: Wed Jan 06, 2010 12:23 pm
by Combuster
Can you program C? How would you normally escape quotes? Why haven't you tried that?

also, use .string

Edit: fronty beat me to the point

Re: escaping double quotes inside an inline asm statement

Posted: Wed Jan 06, 2010 12:43 pm
by bobl
Here's my test

Code: Select all

#include <iostream>
using namespace std;
int main ()
{
  cout << "escaped\"\n";
  asm ("device: .byte \"/dev/vcsa1\", 0");
  return 0;
}
and it looks like I can't program in c cos
whilst the cout statement gives...
escaped"
the asm statement gives...
Error: junk at end of line, first unrecognized character is `"'

This is not what I expected so if you could advise me I'd be very grateful.
I will give .string a go.
EDIT: .string gives junk at end of line, first unrecognized character is `0'
Where am I going wrong??

Re: escaping double quotes inside an inline asm statement

Posted: Wed Jan 06, 2010 12:49 pm
by Firestryke31
Doesn't .string automagically null-terminate it's data?

Re: escaping double quotes inside an inline asm statement

Posted: Wed Jan 06, 2010 12:53 pm
by bobl
Taking account of the null termination I changed the instruction to...

Code: Select all

asm ("device: .string \"/dev/vcsa1\"");
The thing did compile but when running I got...
escaped"
Illegal instruction

Thx for your suggestion though

Re: escaping double quotes inside an inline asm statement

Posted: Wed Jan 06, 2010 12:54 pm
by Firestryke31
Perhaps it's because you're trying to run the string as code (due to the way the compiler is inlineing the ASM and the fact that your 'return' is after the ASM).

Re: escaping double quotes inside an inline asm statement

Posted: Wed Jan 06, 2010 12:59 pm
by bobl
Good point cos the line is in the .data section in the standalone nasm program.
In that case perhaps it should be a string variable outside of the inline asm statements.
I don't know but it's certainly something to try.
Thx

Re: escaping double quotes inside an inline asm statement

Posted: Wed Jan 06, 2010 1:12 pm
by qw

Code: Select all

char device[] = "/dev/vcsa1";

Re: escaping double quotes inside an inline asm statement

Posted: Wed Jan 06, 2010 1:13 pm
by gravaera
Hi,

I'm not going to comment on the '.string' pseudo op which several members have mentioned, but based on what I got from the GAS manuals, the correct directive should be '.ascii' for a non-null terminated string, and '.asciz' for a null-terminated one.
Try those out :)

And also try the GAS manuals, too.

--All the best,
gravaera

Re: escaping double quotes inside an inline asm statement

Posted: Wed Jan 06, 2010 1:54 pm
by bobl
Thx both
I tried ascii and asciz with the 0 added and removed.
Still getting illegal instruction

I'm not sure how you "pass" an "external" string variable like...
char device[] = "/dev/vcsa1";
to an inline instruction like...
asm("movl $device ,%eax");

EDIT:
Would this do it???

Code: Select all

void fn(){
   char device[] = "/dev/vcsa1";
   asm ("movl $device ,%eax"
      : /* no output registers */
      : "a" ($device);
      : "%eax");
}

Re: escaping double quotes inside an inline asm statement

Posted: Wed Jan 06, 2010 2:45 pm
by fronty
The idea was to use that definition in place of that inline assembly thingy. Why do you even want to use inline assembly and what do you think that will do?

EDIT: I just tested and .asciz works with my copy of as like I think it should. But still this usage doesn't make sense to me.

And to gravaera, IMO those syntax errors have nothing to do with ISA.

Re: escaping double quotes inside an inline asm statement

Posted: Wed Jan 06, 2010 3:14 pm
by bobl
fronty
precisely what definition was supposed to replace the
inline thingy? please explain.

In answer to your question
I have a c++ object with smaller objects embedded in a vector inside the bigger object. The information supplied to asm is therefore not pod. As such it would take one function call to access the data and turn it into pod and one to access the asm, if the asm was linked as an external routine.

If I use inline asm I can put it in the function that accesses the data, thereby cutting my function call overhead in half. Because this is a very frequently used, performance critical routine the saving is worthwhile.

Thx for the advice re reading the manuals.
I've already done a small test to write from eax to a string var
and also a test to do things the other way around.
I just find it difficult to test the latter so I don't know if it works.

Code: Select all

#include <iostream>
using namespace std;
int main(){
     char * stringtest = new char[20];
     asm("movb $0, 4(%%eax)\n\t"      //Make sure to zero terminate
             "movl $0x61626364, (%%eax)\n\t"
             :
             :"a"(stringtest)
	     :
     ); 
     cout << stringtest << "\n";
}

/*
//Here's my test to do it the other way around
//Although at the moment I'm not sure how to test this.
//The above is easy to test but loads data from eax to var //stringtest. I just want to do things the other way around.
int main(){
	 //char * stringtest = new char[20];
	const char * stringtest = "bob";
         asm volatile ("nop\n\t"
             :
             :"a"(stringtest)
	     :
     ); 
     //cout << stringtest << "\n";
}
*/
What do you reckon???

Re: escaping double quotes inside an inline asm statement

Posted: Wed Jan 06, 2010 3:35 pm
by fronty
bobl wrote:fronty
precisely what definition was supposed to replace the
inline thingy? please explain.
Consider these snippets.

Code: Select all

        .data
foobar: .asciz "quux"
        .text
        .globl main
main:
...

Code: Select all

static char foobar[] = "quux";

int
main(void)
{
...
They do the same thing. So why can't you create a static global variable if you want to do your thing like you are trying or create a local variable like Hobbes suggested?

I have to admit I'm in such a hurry and so tired right now so I didn't read your complete post to find out about your motives, but I'll get back to this tomorrow with better time.

Re: escaping double quotes inside an inline asm statement

Posted: Thu Jan 07, 2010 2:47 am
by qw
If you insist on using inline assembly, compile my code to assembly and copy what GCC produces. Just escape the inline quotes (hint: and other escape characters).