Comparing macro parameters in gnu asm (gas) - arm

I'm trying to create macros for arm assembly (using GNU tools).
.macro FUNCTION name, attrs
.ifc \attrs\(),"global"
.global \name
.endif
// other stuff
.endm
So macros FUNCTION myFunc, global can be evaluated with .global attribute.
However, the marcro does not handle this attribute compare. Simply stated .if is never evaluated?
Is there a way to compare such string-like macro parameters?

Gas uses single quotes to denote strings and not double quotes.
.ifc string1,string2
Assembles the following section of code if the two strings are the same. The strings may be optionally quoted with single quotes. If they are not quoted, the first string stops at the first comma, and the second string stops at the end of the line. Strings which contain whitespace should be quoted. The string comparison is case sensitive.
If there is no spaces or other parsing issue, as with global, you can just use the name as is without any quoting. So an acceptable solutions is,
.macro FUNCTION name, attrs
.ifc \attrs\(),global
.global \name
.endif
// other stuff
.endm

Related

What is # as part of main function argument

I see following code on this page:
int main(string[]#a)
{print("Manganese");return 0;}
Why it is not following:
int main(string[] args)
{print("Manganese");return 0;}
What is difference between string[]#a and string[] args and when is it used?
The # symbol is used to prefix identifier names when the name either begins with a digit, or is a keyword.
Identifier names may be any combination of letters ([a-z], [A-Z]), underscores and digits. However, to define or refer to an identifier with a name that either starts with a digit or is a keyword, you must prefix it with the '#' character. This character is not considered a part of the name. For example, you can name a method foreach by writing #foreach, even though this is a reserved Vala keyword. You can omit the '#' character when it can be unambiguously interpreted as an identifier name, such as in "foo.foreach()".
See: Vala Tutorial under the Syntax section
To answer your question "What is difference between string[]#a and string[] args and when is it used?", well, not much. Other than simply using the variable name a instead of args, it's not a compiler error to use the # symbol in front of other variable names, even when the criteria above aren't met (although certainly not good practice). The author could safely prefix the variable a as #a, even though it's not the normal usage of the prefix.

Understanding an x86 ASM function in C

I'm currently working through the pintos project and had a question about some assembly macros the project has included
#define syscall1(NUMBER, ARG0) \
({ \
int retval; \
asm volatile \
("pushl %[arg0]; pushl %[number]; int $0x30; addl $8, %%esp" \
: "=a" (retval) \
: [number] "i" (NUMBER), \
[arg0] "g" (ARG0) \
: "memory"); \
retval; \
})
This macro is called to set up the stack for a syscall with only one argument. We push the one argument, the syscall number and trap to kernel. We only pass NUMBER and ARG0, I was wondering where the [number] and [arg0] (lowercase) come from. I have read some docs but didnt find answers. Would love some help!
Thanks
In GCC’s extended assembly syntax, the notation [name] "constraints" (expression) says:
Make expression available to the assembly code.
Put the expression in a place satisfying the constraints. The constraints describe acceptable places to use, such as general processor registers, floating-point registers, and memory. They may also include symbols telling GCC that the expression will be changed by the assembly code or both read and changed. (For output operands, the expression should be an lvalue so that it provides a place for the new value to be written.)
Use name as the name of the place. Then, when GCC sees %[name] in the assembly code, it replaces that with the assembly expression that refers to the place, such as %rax or 16(r3). The [name] part of the operand notation is optional. If you do not give it, GCC gives the operands names of 0, 1, 2,…, so the assembly code would refer to them with %0, %1, %2,...
The part enclosed in square brackets is a symbolic name used in the ASM tempate only. The part in the parentheses is the reference to the variable name in your C program. (More detailed description below)
From the GCC documentation for the ASM template:
[ [asmSymbolicName] ] constraint (cvariablename)
asmSymbolicName
Specifies a symbolic name for the operand. Reference the name in the assembler template by enclosing it in square brackets (i.e. ‘%[Value]’). The scope of the name is the asm statement that contains the definition.* Any valid C variable name is acceptable, including names already defined in the surrounding code. *No two operands within the same asm statement can use the same symbolic name.
When not using an asmSymbolicName, use the (zero-based) position of the operand in the list of operands in the assembler template. For example if there are three output operands, use ‘%0’ in the template to refer to the first, ‘%1’ for the second, and ‘%2’ for the third.
constraint
A string constant specifying constraints on the placement of the operand; See Constraints, for details.
Output constraints must begin with either ‘=’ (a variable overwriting an existing value) or ‘+’ (when reading and writing). When using ‘=’, do not assume the location contains the existing value on entry to the asm, except when the operand is tied to an input; see Input Operands.
After the prefix, there must be one or more additional constraints (see Constraints) that describe where the value resides. Common constraints include ‘r’ for register and ‘m’ for memory. When you list more than one possible location (for example, "=rm"), the compiler chooses the most efficient one based on the current context. If you list as many alternates as the asm statement allows, you permit the optimizers to produce the best possible code. If you must use a specific register, but your Machine Constraints do not provide sufficient control to select the specific register you want, local register variables may provide a solution (see Local Register Variables).
cvariablename
Specifies a C lvalue expression to hold the output, typically a variable name. The enclosing parentheses are a required part of the syntax.*
...
Extended Asm - Assembler Instructions with C Expression Operands

Call MASM function in StdCall convention [duplicate]

I'm programming for Windows in assembly in NASM, and i found this in the code:
extern _ExitProcess#4
;Rest of code...
; ...
call _ExitProcess#4
What does the #4 mean in the declaration and call of a winapi library function?
The winapi uses the __stdcall calling convention. The caller pushes all the arguments on the stack from right to left, the callee pops them again to cleanup the stack, typically with a RET n instruction.
It is the antipode of the __cdecl calling convention, the common default in C and C++ code where the caller cleans up the stack, typically with an ADD ESP,n instruction after the CALL. The advantage of __stdcall is that it is generates more compact code, just one cleanup instruction in the called function instead of many for each call to the function. But one big disadvantage: it is dangerous.
The danger lurks in the code that calls the function having been compiled with an out-dated declaration of the function. Typical when the function was changed by adding an argument for example. This ends very poorly, beyond the function trying to use an argument that is not available, the new function pops too many arguments off the stack. This imbalances the stack, causing not just the callee to fail but the caller as well. Extremely hard to diagnose.
So they did something about that, they decorated the name of the function. First with a leading _underscore, as is done for __cdecl functions. And appended #n, the value of n is the operand of the RET instruction at the end of the function. Or in other words, the number of bytes taken by the arguments on the stack.
This provides a linker diagnostic when there's a mismatch, a change in a foo(int) function to foo(int, int) for example generates the name _foo#8. The calling code not yet recompiled will look for a _foo#4 function. The linker fails, it cannot find that symbol. Disaster avoided.
The name decoration scheme for C is documented at Format of a C Decorated Name. A decorated name containing a # character is used for the __stdcall calling convention:
__stdcall: Leading underscore (_) and a trailing at sign (#) followed by a number representing the number of bytes in the parameter list
Tools like Dependency Walker are capable of displaying both decorated and undecorated names.
Unofficial documentation can be found here: Name Decoration
It's a name decoration specifying the total size of the function's arguments:
The name is followed by the at sign (#) followed by the number of bytes (in decimal) in the argument list.
(source)

what does the `TEXT` around the format string mean in "printf"

The following prints the percentage of memory used.
printf (TEXT("There is %*ld percent of memory in use.\n"),
WIDTH, statex.dwMemoryLoad);
WIDTH is defined to be equal to 7.
What does TEXT mean, and where is this sort of syntax defined in printf?
As others already said, TEXT is probably a macro.
To see what they become, simply look at the preprocessor output. If are using gcc:
gcc -E file.c
Just guessing but TEXT is a char* to char* function that takes care of translating a text string for internationalization support.
Note that if this is the case then may be you are also required to always use TEXT with a string literal (and not with expressions or variables) to allow an external tool to detect all literals that need translations by a simple scan of the source code. For example may be you should never write:
puts(TEXT(flag ? "Yes" : "No"));
and you should write instead
puts(flag ? TEXT("Yes") : TEXT("No"));
Something that is instead standard but not used very often is the parameteric width of a field: for example in printf("%*i", x, y) the first parameter x is the width used to print the second parameter y as a decimal value.
When used with scanf instead the * special char can be used to specify that you don't want to store the field (i.e. to "skip" it instead of reading it).
TEXT() is probably a macro or function which returns a string value. I think it is user defined and does some manner of formatting on that string which is passed as an argument to the TEXT function. You should go to the function declaration for TEXT() to see what exactly it does.
TEXT() is a unicode support macro defined in winnt.h. If UNICODE is defined then it prepends L to the string making it wide.
Also see TEXT vs. _TEXT vs. _T, and UNICODE vs. _UNICODE blog post.
_TEXT() or _T() is a microsoft specific macro.
This MSDN link says
To simplify code development for various international markets,
the Microsoft run-time library provides Microsoft-specific "generic-text" mappings for many data types, routines, and other objects.
These mappings are defined in TCHAR.H.
You can use these name mappings to write generic code that can be compiled for any of the three kinds of character sets:
ASCII (SBCS), MBCS, or Unicode, depending on a manifest constant you define using a #define statement.
Generic-text mappings are Microsoft extensions that are not ANSI compatible.
_TEXT is a macro to make a strings "character set neutral".
For example _T("HELLO");
Characters can either be denoted by 8 bit ANSI standards or the 16 bit Unicode notation.
If you define _TEXT for all strings and define a preprocessor symbol "_UNICODE", all such strings will follow UNICODE encoding. If you don’t define _UNICODE, the strings will all be ANSI.
Hence the macro _TEXT allows you to have all strings as UNICODE or ANSI.
So no need to change every time you change your character set.

c macro, token pasting without quotes

I need to generate some variable name with macro in C.
It seems that # token-pasting operator does the job, but the result is always a string.
#define create_var( name ) char #name
will not work because name is expanding in "name" (as string).
#define create_var( name ) char prefix##name
will work, but all my vars will have a prefix.
Is there any trick available to obtain a simple name?
create(test) to expand in
char test;
Thanks very much in advance,
If you would like your variable name to appear unmodified (without prefix) in your preprocessed code, just use the formal parameter name of the macro, without # and without ##.
You can # in the macro definition if you want to convert some argument to a string constant. And can use ## to concatenate tokens to build new tokens (for example to build new variable name with prefixes and/or suffixes and other stuff). With out any of these the preprocessor will just insert the sequence of tokens to pass to the macro unmodified (*).
(*): C preprocessor semantics ar subtle. Preprocessor macros are replaced at multiple stages during macro expansion which can have quite unobvious results.

Resources