Advertisement

Problems with GCC and inline assembly (x86)

Started by May 29, 2001 06:31 PM
6 comments, last by ironfroggy 23 years, 8 months ago
I''m working on a program in GCC. I did have it on VC++6.0 but I like GCC better. However, I am getting errors with some inline assembly. The line is this: __asm__ ("mov %EAX, value"); I get an error from the assembler saying it has too many memory references! What could be the cause?
(http://www.ironfroggy.com/)(http://www.ironfroggy.com/pinch)
Inline ASM in GCC is _very_ different from VC++. I would recommed you check out a tutorial such as this one.

Incidentally, the line should probably look something like:
__asm__("movl %0, %%eax" ::"g"(value));

Yep, it''s that ugly.

~~~~~~~~~~
Martee
http://www.csc.uvic.ca/~mdill
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Advertisement
ok it goes like this
when you don''t have input/output vars you add % before every register
you add $ before number
the dest, src is not src, dest
to put 5 in ax you would do movw $5, %ax
that''s it
this it the basic
another thing is that when you want to read a specific cell
you would do:
movw (%esi), %ax
which reads a word from [esi] and puts in a
when you do something like:
mov ax, [esi+4]
that would be: movw $4(%esi), %ax

another thing is that now you have to add a suffix B W L
which means
byte, word or long
to the code this lets the compiler know what size of registers and variables you use

so instead of just
mov ah, 0
uu would do
movb $0, %ah

ok remember that every register can be used as a pointer to array
like DI, SI and BX
so now you can do(in pmode)
movl (%eax), %bx

that''s it
now remember that you can''t
use something like this in at&t syntax

int x = 5;
movw x, %ax
that won''t work
so what you have to do is to pass a parameter
that''s pretty eas once you get it
so let''s start
we want to take the variable X and put it in ax
how would we do it?
so here come the input and output variables
remember when using them you have to put %% before every register
all other rulz are the same

__asm__("
movw %%ax, %0"
"
:
:"g" (value));
pretty easy isn''t it?
ok you notice the : and then anotehr :
the first means you receive a parameter from the code
and the second means you put one in the code
notice the %0 ?? it means it reads from the "g" (value)
this is how you use it
say you did : "g"(value1), "g"(value2)
then if you wanted to read the second value
you would do movw %%ax, %1
notice we use %0 and not $0
the % means we points to an input/output paramaeter
the "g" means to put it in a kind of stack

oh, even if you don''t use the first output) parameters
you must put them in code...

let''s use a better way now to put X in ax
basically it''s the same
but it''s less work for us
let''s see:

__asm__("
//do something we the initizalized ax
"
:
:"a" (x)
);
you see that now we do "a" and not "g"
that means we put the parameter directly in EAX or AX depends on the type of variable x

now you can use b for bx c for cx D for DI S for SI
etc....(i don''t remember them all, lemme know if you need''em)

ok let''s continue:
now let''s say we want to do x=x+5; but in pure asm
so the code would be something like this
notice the output) usage this time
__asm__("
//we have ax initialized alrady with x
addl $5, %0
"
:"=a"(x)
:"a"(x)
);
notice the same way we use "=g"
we can use can use "=a"
which will take *automatically" eax in puts it in x

easy huh?
instead of doing it this way we wrote the shorter one
__asm__("
movl %1, %%eax // puts x in eax
addl $5, %%eax // add to eax 5
movl %%eax, %0" // put eax in x
:"g="(x)
:"g"(x));
notice the order of the stack that we use
first we count from 0 to N for output "g="
and then we count from N to K for input "g"

now let''s do the last thingy
x = x + x2;
__asm__("
addl %%eax, %%ebx
:"=a"(x)
:"a"(x), "b"(x2));
we could use bx for x2, who cares
when you need the registers then use the stack way
%0, %1 etc...

it''s funny that for doing x=x+x2 we write just one line
the : : do the rest
that''s the only good thing in this syntax IMHO

ok there is a last : for telling the compiler what we use
in the last code we use eax and ebx
so the last one would be
: "eax", "ebx"
i think it''s for optimizations and stuff
but you don''t have to write it...
and when you use the stack way you add "memory"
that''s
like this:
__asm__("
addl $5, %%eax\n
incl %%eax
"
:"=a"(x) // 0 to N
:"a"(x) // N to K
:"eax"

ok in your code you have to add a suffix \n to every line
this tells the compiler where is the end of each line

ok now that you are an at&t syntax master
you can start using it.....

god luck pal

Arkon
[QSoft Systems]
This is a pain

I tried what you said, but now I still get an error of too many memory references! This is the line now:

__asm__ ("movl %0, %%EAX" : : "g"(value));

by any chance, would anyone know how a simple library someone made that makes this easier? asm("mov eax, %l", value); would be really nice to have.
You could always use an external assembler such as NASM.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
How do i do that and have it in my program?
(http://www.ironfroggy.com/)(http://www.ironfroggy.com/pinch)
Advertisement
nasm is more pain in the ass!!
you need to use suffix letters for function names
use globals
link the .asm to yer project etc....
damn code
what ver of djgpp do you have over there?

Arkon
[QSoft Systems]
Why are you wanting to use inline asm anyway? If it is a learning experience, fair enough. If it is hardware programming, also fair enough (to an extent) but if it is just for speed, then gcc with -O6 flag is pretty blimin good!

If you think about your C code carefully, and help gcc out by using << when multiplying by two and that sort of thing, then you''ll get almost as fast with a lot less headache.

Don''t get me wrong, I know that real mean program in asm but it is often unnecessary.

Try running gcc -O6 -Wall -S myCode.c

Then open up myCode.S and you''ll see optimizations you wouldn''t have even thought of!

But good luck with your asm anyway.

------
BigDave

A witty saying proves nothing
------BigDaveA witty saying proves nothing

This topic is closed to new replies.

Advertisement