[Top] [All Lists]

Re: Assembly macro with parameters

To: Fabrizio Fazzino <>
Subject: Re: Assembly macro with parameters
From: David Daney <>
Date: Thu, 30 Jun 2005 10:32:23 -0700
In-reply-to: <>
Original-recipient: rfc822;
References: <> <> <> <> <>
User-agent: Mozilla Thunderbird 1.0.2-6 (X11/20050513)
Fabrizio Fazzino wrote:
After three months I still have the same problem...

Suppose I want to generate my own opcode, let's say 0xC4000000,
inside a C program. Suppose this value is NOT a constant in
the macro I want to write since it will contain three
variable fields for the rd,rs,rt registers, so I need to calculate
the opcode at least at compilation time (at runtime is NOT

Daniel suggested using .word and writing the function by hand,
but which is the syntax I have to use?

#define myopcode(rs,rt,rd) { \
  int opcode_number = 0xC4000000 | (rs<<21) | (rt<<16) | (rd<<11); \
  char opcode_string[20]; \
  sprintf(opcode_string, ".word 0x%X", opcode_number); \
  asm(opcode_string); \

The arguments to the asm() statement are strings not char*. They are evaluated at compile time not run time.

You will probably have to use the C preprocessor stringification and concatination operators ('#' and '##').

David Daney

<Prev in Thread] Current Thread [Next in Thread>