2,250 questions
2
votes
1
answer
46
views
Assistance with inline Assembly porting to SDCC
I am porting the GetCurrentMicro function in the Arduino Core STM8 to my SDCC code and I noticed the Cosmic C Compiler _asm function allows me to return a result as well as an input in a C var.
Is ...
1
vote
1
answer
181
views
Can you somehow grab a value from a register and put it into a C variable in C with the asm keyword for arm64 macOS assembly?
Can you somehow grab a value from a register and put it into a C variable in C with the asm keyword for arm64 macOS assembly?
I have seen code from other stack overflows that have already answered ...
1
vote
0
answers
89
views
This program with inline assembly works in CLANG with optimization enabled but does not work in GCC. Does GCC ignore `volatile` on global variables? [duplicate]
So, this piece of C++ code:
/*
* Compile on 64-bit Linux or Solaris (I guess it will probably also work on
* FreeBSD), like this:
* g++ -o bin2dec bin2dec.cpp -std=c++11 #Don't put -O3
* here, as ...
6
votes
2
answers
126
views
Rust core::arch::asm throwing error where type u64 doesn't fit into class reg
I have recently been learning how to program bare metal to write a kernal for the rpi4b. The last thing I needed to add was a way to have the main script only run on one core. The method I decided to ...
2
votes
1
answer
113
views
"Non-ASM statement in naked function is not supported" error by clangd
I'm following bare metal programming guide:
https://github.com/cpq/bare-metal-programming-guide
Clangd produces error
"Non-ASM statement in naked function is not supported"
in Helix editor ...
1
vote
0
answers
89
views
Remote process code injection with dynamic addresses
I've made a small mod app for the game Elden Ring. With this mod, whenever the player loses health, I reduce their level by 1. Everything works so far.
What I want to do now is make it happen when ...
2
votes
1
answer
249
views
Forcing a C program to take a forged epilogue made with inline-assembly to jump to arbitrary function
This comes from a post about invoking a trivial buffer overflow (to jump to a function present in the source but not called explicitly in any place of the program (2333909/how-can-i-invoke-buffer-...
8
votes
1
answer
210
views
Segfault simulating #embed on GCC 14 with .incbin into .rodata and defining a global span<>
I have a project that has to use GCC 14 which does not have #embed support.
I attempted to simulate it as follows.
namespace {
namespace {
extern "C" {
extern std::...
1
vote
1
answer
175
views
Why calling longjmp in a non-main stack causes the program to crash?
The following code attempts to create a simple stackful coroutine. It allocates a stack frame in the heap space by setting the rsp register and then calling a function. Afterwards, it exits the ...
-1
votes
1
answer
94
views
Use c++ dll to hook to an instruction and still use the registers (x86) [closed]
NOTE: This is decompiled ghidra code, the imageBase start at 0x00400000, so we are working with Program.exe+001a0d39 relative to image base.
005a0d39 01 87 94 ADD dword ptr [EDI + ...
4
votes
2
answers
186
views
C with inline assembly - recursive function that calls within the asm statement
I am fairly new to assembly.
After compiling, when I run the code below, I get a segmentation fault.
I am trying inline assembly with recursion. I am compiling this code with cxxdroid.
int ...
2
votes
2
answers
213
views
Keep Delphi In-line Assembly Labels in Assembly Blocks
Assembly is always faster than high-level languages. For example, I tested an empty loop in Delphi repeating 1 billion times, and same loop but written in assembly. The Delphi loop took 6 billion (x64 ...
4
votes
3
answers
295
views
How can I further optimize this C++ function that takes integer square roots?
In an algorithm I am using, most of the time is spent in the evaluation of square roots using the sqrt function in cmath. In my application I only need the integer part of the square root of integers, ...
1
vote
1
answer
170
views
error: impossible constraint in 'asm' with -m32 for an "i" ("string") operand: string address as immediate
I am just wondering why this code:
void test(void) {
asm volatile( "ud2\n\t"
".long " "%c0" "\n\t"
".word %c1\n\t&...
3
votes
0
answers
163
views
How to make a macro in C to parameterize register?
I want to define a assembly to read register from arm64. Code is like the following:
asm volatile(
"str <reg> [%0]\n"
:
: "r"(value)
: "...
0
votes
0
answers
92
views
Writing horizontal sum of AVX2 in assembly
I try to write this simple C function for AVX2 horizontal sum in assembly.
static inline float hsum256_ps(__m256 v) {
__m128 lo = _mm256_castps256_ps128(v);
__m128 hi = _mm256_extractf128_ps(v,...
1
vote
1
answer
93
views
Segmentation fault with inline assembly code for ARM Cortex-A7
I am trying to accelerate the computation of the dot product of 2 float arrays on an ARM Cortex A7. I benchmark the pure C code, the code using intrinsics and the assembly code.
But my assembly code ...
3
votes
1
answer
101
views
Does GCC guarantee state of DF before inline assembly on x86?
Does GCC, when compiling for x86 or x86_64, make sure that the Direction Flag has some specific value before it starts executing an Extended Asm block? I couldn't find any information on that in the ...
0
votes
0
answers
91
views
invalid operand for inline asm constraint 'i'
I wrote some inline assembly functions a few years ago which worked fine.
And now with the most recent version of Android Studio (dec 2024) it spits error messages:
error: invalid operand for inline ...
0
votes
1
answer
294
views
ambiguous operand size for `mov' while benchmarking x86 operations
I'm writing up a benchmark to compare the movbe operation against mov; bswap to find out which is faster.
I used to write "extern C function ..." up until now, but for specific reasons I can'...
1
vote
0
answers
44
views
Unable to pass input-argument to assembly-statement
I got stuck when porting our codebase from GCC => LLVM/CLANG.
After much attempts, I have narrowed the problem to a trivial reproducer. Surprisingly, I can't even seem to make things work with GCC :...
1
vote
2
answers
114
views
Why must there be an instruction before out statements in Rust in-line assembly?
I just want to take the value in a1 and assign it to a variable, so why does it make me put an instruction before using an out, or in this case a lateout? I've written the following assembly, but I ...
1
vote
2
answers
82
views
Assembler code doesn't work - reverse number
I wrote a code in ASM to calculate the reverse number. For example: input: 123, the result should be 321. The ebx register should have the result, but it aways shows 0. Also, I have an exception "...
2
votes
1
answer
210
views
Is this inline RISC-V Rust assembly correct?
So I'm trying to use the RISC-V SBI debug console extension of OpenSBI on Qemu using inline Rust assembly, but it isn't working. I've come to learn that the issue may be because of memory placement, ...
0
votes
1
answer
142
views
How to manipulating C variable directly (not via register) via inline assembly "incl" instruction in gcc?
I have the following code
#include <stdio.h>
volatile int global_counter = 0;
void increment_counter() {
for (int i = 0; i < 100000; ++i) {
//global_counter++;
asm (&...
1
vote
1
answer
221
views
ARM V7 inline assembly - moving a C variable into a register
There are no questions on ARM V7 inline assembly that answer mine so I made a post.
I want to move the value of C variables into r0-r2 and the other way round - from the registers into C variables. ...
1
vote
0
answers
68
views
how can I write the lr_usr register in supervisor mode with C inline assembly
I have searched for the specific question how to write banked registers in inline assembly but could not find an answer so my question seems novel.
I want to write the lr_usr register in supervisor ...
4
votes
1
answer
186
views
Why does an `invlpg` instruction not work when I pass a pointer as a memory operand in inline assembly?
I use BOCHS to emulate Intel-80386 and try to write an operating system, but when I use an element of PCB after destroying PCB, it won't cause a page fault. It confused me for a few days. Maybe ...
6
votes
0
answers
88
views
Validation of clobber constraints in inline assembly
I maintain C code that has inline assembly used by GCC, Clang and sufficiently compatible compilers¹. Inline assembly statements require constraints to tell the compiler what registers and memory are ...
2
votes
1
answer
124
views
What does this inline-assembly code snippet in Valgrind mean?
What does this mean in Valgrind's VALGRIND_DO_CLIENT_REQUEST_EXPR?
__asm__ volatile (
__SPECIAL_INSTRUCTION_PREAMBLE
/* %RDX = client_request ( %RAX ) */
"xchgq %rbx, %rbx"
: ...
3
votes
2
answers
126
views
Is this inline asm correct for bitcasting int as float?
I am trying to familiarise myself with gnu inline assembly. I wrote a single line inline asm to reinterpret int as float. While this prints the correct result, I am wondering whether this is correct.
#...
1
vote
1
answer
214
views
non-ASM statement in naked function
I am trying to create a class containing custom prologue/epilogues that shall be used by methods of other "consumer" classes.
It should look like the following example. (Keep in mind that ...
2
votes
0
answers
159
views
What's wrong with my implementation of c-style coroutine
I try to implement a simple coroutine using c.
The platform is:
M3 Pro MacBook Pro 16
apple native gcc
macOS 14.3.1
Here is my code:
// main.c
#include <stdio.h>
#include <stdlib.h>
#...
2
votes
1
answer
107
views
Rewriting MSAsmStmt with llvm plugin segfaults
I have this C code:
#include <inttypes.h>
#include <stdio.h>
uint8_t count1(uint32_t x) {
int out;
__asm {
mov edx, [x]
mov al, 0
next: cmp edx, 0
je ...
3
votes
1
answer
426
views
Debugging inline ASM with LLDB - treat instructions as separate statements for the step command?
In LLDB, the step command steps over a whole asm{} block as a single "statement". Is there a way to make it treat each instruction separately so you don't have to use si to step ...
1
vote
1
answer
199
views
Power function in inline 64-bits Assembly on C++ Builder for floating points base
I am using Embarcadero C++ Builder 12 and I am having a giant problem with my power function in Assembly, its called vpow(base, exp). It works perfectly with integers, but I need it to work with ...
2
votes
1
answer
195
views
gcc thumb2 inline assembly for fixed point conversion
I would like to write some gcc inline assembly for armv7em on Cortex-M7 to perform conversion between floating point numbers and fixed point numbers. ARM provides the vcvt instruction with #fbits to ...
1
vote
0
answers
67
views
Inline assembly: writing an unrolled loop to process arrays with correct constraints
Context
I'm trying to write a piece of code in inline assembly, which processes all elements of a "small" array (say ~10 elements) as a fully-unrolled loop. I want to avoid falling into the ...
1
vote
0
answers
155
views
Is there a method for getting a keyboard input thru ASM with protected mode in C++, from the x86 PC keyboard controller?
I've been working on a kernel that my system needs. I've implement some functions that acts as a wrapper for I/O.
But somehow, the get_char() only returns 0.
Code:
uint8_t inb(uint16_t port) {
...
1
vote
1
answer
122
views
How can I restore the cpu status? (restoring the cpu registers x86_64)
I am implementing context switching for my first os and I came into a little problem I cannot solve which is restoring the cpu status after saving it from a struct. As all the registers have to be ...
1
vote
1
answer
119
views
gnu inline assembly constraint `i` for memory address
Recetly, this function rip_rel_ptr has been added to Linux kernel.
https://elixir.bootlin.com/linux/latest/source/arch/x86/include/asm/asm.h#L118.
I can compile the kernel, but when I copy this ...
2
votes
1
answer
121
views
how do I turn AT&T syntax into intel syntax?
In AT&T syntax you can do something like asm("mov %%eax, %0\n\t":"=r" (a[0])); but not in intel syntax.
I want to translate this AT&T syntax to intel syntax, it gets the ...
8
votes
1
answer
273
views
Purpose of '*&x' in inline assembly?
Looking at some x86_64 GCC inline assembly, I have come across the following construct:
int x;
__asm__( "<opcode> %0" : "=m" (*&x) );
^^^
...
2
votes
1
answer
196
views
What does `+&` mean in gcc inline assembly?
I'm aware that when using gcc inline assembly, if you don't specify otherwise, it assumes that you consume all your inputs before you write any ouput operand. If you actually want to write to an ...
-1
votes
3
answers
130
views
Code in assembler, which must calculate the scalar product
Im new with an assembler.Couldn't find the solution in the ethernet so asking here. This code outputs wrong numbers and i can't get why.
#include <iostream>
long DotProduct(short int* vec1, ...
1
vote
1
answer
368
views
Using FSIN through inline-assembly
I want to use x87's FSIN through gcc's / clang's inline assembly. How would a sin()-function using an __asm__-block internally for that look like with a 64 bit double parameter ? Using __builtin_sin() ...
0
votes
1
answer
199
views
error: ‘asm’ operand has impossible constraints
I want to calculate length of the word, but I have error. I don't understand why.
int new_strlen(char* word)
{
int len = 0;
__asm__ ("mov ecx, 100\n\t"
&...
1
vote
0
answers
69
views
Difference of sets and read access violation
I'm trying to find a difference of sets (I use vectors but idea is the same: arr1 \ arr2) using inline assembly in VS. But I can't get rid of the following error: "An exception was thrown: read ...
0
votes
1
answer
65
views
How can I create an Inline assembly command with a multi-variable register offset?
The following code, using gcc for ARMv4, is perfectly fine:
asm("strb.w r2, [r0, #24 + 8 * 1]");
Now, if I try the following:
asm("strb.w r2, [r0, %[offset] + %[delta] * %[scale]]"...
2
votes
0
answers
248
views
ASM register-variable from existing register-value in clang
I'm having slight issues with trying to use register-variables in clang-cl, on windows. I'm trying to declare a variable that simply reuses a (non-volatile) register that has been setup by a prior ...