Re: C inline assembler & accessing constants

From: Matt (spamtrap_at_crayne.org)
Date: 03/08/05


Date: Tue, 8 Mar 2005 20:23:32 +0000 (UTC)

M. Maniak skrev:
[...]
> Is it possible to access a constant (say #define LENGTH 8)
> in the assembler section of the C source code per "mov ecx,LENGTH"?
>
> I get an error if I try to do so ("undefined reference to `LENGTH'").

What makes you think that a preprocessor declaration will be applied inside
a string?

[...]
> int main()
> {
> __asm
> (
> ".intel_syntax noprefix\n"
>
> "mov EAX,0\n"
> "mov EBX,0\n" // index
> "mov ECX,LENGTH\n" // just to show the error
> "Loop:\n"
> "cmp EBX,':'\n"
> "jae End\n"
> "add EAX,aInt[EBX*4]\n" // int == 4 byte steps in RAM
> "jo Overflow\n"
> "inc EBX\n"
> "jmp Loop\n"
> "Overflow:\n"
> "mov EAX,':'\n"
> "sub EAX,10\n"
> "End:\n"
> "mov [iSum],EAX\n"
>
> ".att_syntax\n"
> );

Add this after your ".att_syntax\n" line:
: : "i" (LENGTH)

Then you should be able to refer to LENGTH as %0 inside the code. Though it
is a little confusing, see the GCC manual for more information:
http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_16.html#SEC175

-Matt



Relevant Pages

  • Re: Brian Kernighan, maybe Im not worthy, maybe Im scum
    ... conformant string. ... int repeats, reps; ... ref satisfierLength); ...
    (comp.programming)
  • RE: Controling Modal Dialogs (Solution)
    ... doesn't return until the 'modal' browser returns. ... string varOptions) ... public void DocumentComplete ... int rc = winDisp.Invoke(rgDispId, ref guid, 0, ...
    (microsoft.public.inetsdk.programming.webbrowser_ctl)
  • Gcc compatible header file
    ... A string collection is a table of zero terminated strings that will grow ... typedef struct _StringCollection StringCollection; ... int; ... bool; ...
    (comp.lang.c)
  • Kernighan and Pikes "Beautiful" Code
    ... conformant string. ... int repeats, reps; ... ref satisfierLength); ...
    (comp.programming)
  • Re: FTP CD command
    ... public const int GENERIC_WRITE = 0x40000000; ... string lpszProxyName, ... public static extern IntPtr InternetConnect ( ... public static extern bool FtpGetCurrentDirectory ( ...
    (microsoft.public.dotnet.languages.vb)