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

escaping double quotes inside an inline asm statement

Post 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.
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 »

It's just basic string literal, escape them like everywhere.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: escaping double quotes inside an inline asm statement

Post 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
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
bobl
Posts: 16
Joined: Wed Jan 06, 2010 8:43 am

Re: escaping double quotes inside an inline asm statement

Post 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??
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: escaping double quotes inside an inline asm statement

Post by Firestryke31 »

Doesn't .string automagically null-terminate it's data?
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
bobl
Posts: 16
Joined: Wed Jan 06, 2010 8:43 am

Re: escaping double quotes inside an inline asm statement

Post 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
Last edited by bobl on Wed Jan 06, 2010 12:55 pm, edited 1 time in total.
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: escaping double quotes inside an inline asm statement

Post 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).
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
bobl
Posts: 16
Joined: Wed Jan 06, 2010 8:43 am

Re: escaping double quotes inside an inline asm statement

Post 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
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 »

Code: Select all

char device[] = "/dev/vcsa1";
Last edited by qw on Wed Jan 06, 2010 1:17 pm, edited 2 times in total.
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: escaping double quotes inside an inline asm statement

Post 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
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
bobl
Posts: 16
Joined: Wed Jan 06, 2010 8:43 am

Re: escaping double quotes inside an inline asm statement

Post 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");
}
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 »

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.
bobl
Posts: 16
Joined: Wed Jan 06, 2010 8:43 am

Re: escaping double quotes inside an inline asm statement

Post 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???
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: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.
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 »

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).
Locked