Is there a way to make a macro name case insensitive when defining it?
For example,
Consider the input stream: Mov MOV moV mOv
I want the m4 to output to be: mov mov mov mov
The naive way to do this is to define the following m4 macros:
define(Mov,mov)
define(MOV,mov)
define(moV,mov)
define(mOv,mov)
This method becomes tedious when we want to do the same for a 4 or 5 letter word. Is there a better way to do this?
If you want only string transform (want the m4 to output to be) you can use translit:
translit(string, mapfrom, mapto)
Transliterate the characters in the first argument from the
set given by the second argument to the set given by the
third. You cannot use tr(1) style abbreviations.
Your case:
translit(`MoV',`ABCDEFGHIJKLMNOPQRSTUVWXYZ',`abcdefghijklmnopqrstuvwxyz')
Let's create an m4 macro called to_lowercase. Its definition looks as shown below.
define(`to_lowercase',`translit($1,`ABCDEFGHIJKLMNOPQRSTUVWXYZ',`abcdefghijklmnopqrstuvwxyz')')
Now, we can call our macro using to_lowercase(Mov)', to_lowercase(mOV)'.
Related
I'm trying to run a simple code in assembly - I want to save an address to memory.
I'm moving the address into a register and then moving it into the memory, but for some reason the memory isn't updated.
.data
str1: .asciz "atm course number is 234118"
str2: .asciz "234118"
result: .space 8
.text
.global main
main:
xorq %rax, %rax
xorq %rbx, %rbx
leaq str1, %rax
mov %rax, result(,%rbx,1)
ret
What am I doing wrong?
Your debugger is looking at the wrong instance of result. Your code was always fine (although inefficient; use mov %rax, result(%rip) and don't zero an index, or use mov %rax, result(%rbx,,) to use the byte offset as a "base", not "index", which is more efficient).
glibc contains several result symbols, and in GDB info var result shows:
All variables matching regular expression "result":
Non-debugging symbols:
0x000000000040404b result # in your executable, at a normal static address
0x00007ffff7f54f20 result_type
0x00007ffff7f821b8 cached_result
0x00007ffff7f846a0 result # in glibc, at a high address
0x00007ffff7f85260 result # where the dynamic linker puts shared libs
0x00007ffff7f85660 result
0x00007ffff7f86ab8 result
0x00007ffff7f86f48 result
When I do p /x &result to see what address the debugger resolved that symbol to, I get one of the glibc instances, not the instance in your .data section. Specifically, I get 0x7ffff7f85660 as the address, with the content = 0.
When I print the value with a cast to p /x (unsigned long)result, or dump the memory with GDB's x command, I find a 0 there after the store.
(gdb) x /xg &result
0x7ffff7f85660 <result>: 0x0000000000000000
It looks like your system picked a different instance, one that contained a pointer to a libc address or something. I can't copy-paste from your image. These other result variables are probably static int result or whatever inside various .c files in glibc. (And BTW, that looks like a sign of poor coding style; usually you want to return a value instead of set a global or static. But glibc is old and/or maybe there's some justification for some of those.)
Your result: is the asm a compiler would make for static void* result if it didn't get optimized away. Except it would put it in .bss instead of .data because it's zero-initialized.
You're using SASM. I used GDB to get more details on exactly what's going on. Looking at the address of result in SASM's debug pane might have helped. But now that we've identified the problem using GDB, we can change your source to fix it for SASM.
You can use .globl result to make it an externally-visible symbol so it "wins" when the debugger is looking for symbols.
I added that and compiled again with gcc -g -no-pie store.s. It works as expected now, with p /x (unsigned long)result giving 0x404028
I was wondering if there was any way that would allow me to specify anything other than eax, ebx, ecx and edx as output operands.
Lets say I want to put the content of r8 in a variable, Is it possible to write something like this :
__asm__ __volatile__ (""
:"=r8"(my_var)
: /* no input */
);
It is not clear why would you need to put contents of a specific register into a variable, given a volatile nature of the most of them.
GNU C only has specific-register constraints for the original 8 registers, like "=S"(rsi). For r8..r15, your only option (to avoid needing a mov instruction inside the asm statement) is a register-asm variable.
register long long my_var __asm__ ("r8");
__asm__ ("" :"=r"(my_var)); // guaranteed that r chooses r8
You may want to use an extra input/output constraint to control where you sample the value of r8. (e.g. "+rm"(some_other_var) will make this asm statement part of a data dependency chain in your function, but that will also prevent constant-propagation and other optimizations.) asm volatile may help with controlling the ordering, but that's not guaranteed.
It sometimes works to omit the __asm__ ("" :"=r"(my_var)); statement using the register local as an operand, but it's only guaranteed to work if you do use it: https://gcc.gnu.org/onlinedocs/gcc/Local-Register-Variables.html#Local-Register-Variables. (And see discussion in comments on a previous version of this answer which suggested you could skip that part.) It doesn't make your code any slower, so don't skip that part to make sure your code is safe in general.
The only supported use for this feature is to specify registers for input and output operands when calling Extended asm (see Extended Asm). This may be necessary if the constraints for a particular machine don’t provide sufficient control to select the desired register. To force an operand into a register, create a local variable and specify the register name after the variable’s declaration. Then use the local variable for the asm operand and specify any constraint letter that matches the register
P.S. This is a GCC extension that may not be portable, but should be available on all compilers that support GNU C inline asm syntax.
gcc doesn't have specific-register constraints at all for some architectures, like ARM, so this technique is the only way for rare cases where you want to force specific registers for input or output operands.
Example:
int get_r8d(void) {
register long long my_var __asm__ ("r8");
__asm__ ("" :"=r"(my_var)); // guaranteed that r chooses r8
return my_var * 2; // do something interesting with the value
}
compiled with gcc7.3 -O3 on the Godbolt compiler explorer
get_r8d():
lea eax, [r8+r8] # gcc can use it directly without a MOV first
ret
It should be possible, based on the answer here:
https://stackoverflow.com/a/43197401/3569229
#include <stdint.h>
uint64_t getsp( void )
{
uint64_t sp;
asm( "mov %%r8, %0" : "=rm" ( sp ));
return sp;
}
You can find a list of register names here: https://www3.nd.edu/~dthain/courses/cse40243/fall2015/intel-intro.html
So your code above would be changed to:
__asm__ __volatile__ ("mov %%r8, %0"
:"=rm"(my_var)
: /* no input */
);
I have to write Windows program in x86 assembler (I'm using flat assembler) which will be printing contents of predefined memory. I wrote:
format PE console 4.0
include "win32ax.inc"
start:
mov eax, [0x00850095]
cinvoke printf,formatstring, eax
invoke Sleep,-1
formatstring db "%#x"
section '.idata' import data readable
library msvcrt,'msvcrt.dll',\
kernel32,'kernel32.dll'
import msvcrt,printf,'printf'
import kernel32,Sleep,'Sleep'
It's not working :C; can you help me with that?
Your format string is not quite correct. "%#x" should probably be "%08X" if you want to print the value of eax. If you want to print the value located at the address in eax then you need to load the value pointed to by eax and pass that to printf.
The %x format specifier to printf expects an int passed in the varargs, so if you want to print one byte at a time, you need to load a byte, zero-extended, into eax and then pass eax to printf.
Can I make a gcc to place the specific function (C or C++) parameter to the specific register? I want to define the function prototype to use by some naked code, that exploits this register, say, as the base. I mean something like this, but towards a function parameter.
You can't request that a particular function parameter be placed in a given register. This is because the way function parameters are passed to a function is defined by the ABI for your architecture. For example, on x86-64, the first 6 (integral) parameters are passed in the registers %rdi, %rsi, %rdx, %rcx, %r8, and %r9.
What you can do is use local register vars, as you indicated, to assign a particular local variable to a register, and then copy the parameter into that local variable. This should cause the parameter to be copied into that register.
That said, why do you even want this?
Consider the following code:
#include <stdio.h>
void main() {
uint32_t num = 2;
__asm__ __volatile__ ("CPUID");
__asm__ __volatile__ ("movl $1, %%ecx":);
__asm__ __volatile__ ("andl $0, %%ecx": "=r"(num));
printf("%i\n", num);
}
My initial expectation was that this code would print 0, and it does if I comment out the CPUID line, but as-is it was giving me garbage. After some trial, error, and research I realized that I was getting the value of a random register. Apparently GCC doesn't assume that I want the result of the statement being executed.
The problem is that I've seen (other people's) code that relies on that statement properly getting the result of the AND, regardless of what is going on with the other registers. Obviously such code is broken, given my observations, and the "=r" should be replaced with "=c".
My question is, can we ever rely on the "=r" constraint behaving consistently or according to the obvious expectation? Or is GCC's implementation too opaque/weird/other and it's best just to avoid it in every situation?
In order to use the =r output specifier you need to give gcc the freedom to pick the register that it wants to use. You do that by specifying the inputs and outputs generically with %0 for the output and the inputs starting with %1 for the first input.
In your case you are saying that num can be in a register. But there is nothing in the asm instruction that uses the output register. So gcc will essentially ignore this.
The reason that you are getting a different value if you comment or don't comment the CPUID instruction is that CPUID can write to eax,ebx,ecx, and edx. I tried your example on my system and got 0 as the result in both cases. But I noticed that the assembly that is generated is printing the value of eax. So I guess when I ran this program CPUID was writing 0 to eax.
If you did want to use the =r constraint you would need to do something like this:
asm("CPUID \n\t"
"movl $1, %0 \n\t"
"andl $0, %0 \n\t"
:"=r"(num) );
Otherwise if your asm code specifically mentions a register then you will need to specify it in the constraint list. In your example that means using =c.