Vik78
Vik78

Reputation: 294

I'm trying to translate x86-64 assembly code into equivalent C code. How can I get the value contained in %rsp?

My idea was that a pointer to the most recently initialized local variable would contain the current value of %rsp. Is this correct?

Upvotes: 0

Views: 92

Answers (2)

Peter Cordes
Peter Cordes

Reputation: 364358

If your "decompiled" C version of a function still does anything with the stack pointer directly, you did it wrong. Make up a name for the C variable that the asm is accessing through the stack pointer.

If your C still looks like the asm, you're writing an x86 simulator.

Upvotes: 0

Keith Thompson
Keith Thompson

Reputation: 263307

C doesn't have a concept of registers (the nearly obsolete register keyword notwithstanding). There is no portable way to read a specific register in C.

The closest you could get is to use some compiler-specific mechanism for inline assembly code, but then you might as well just use assembly language.

A particular compiler will use the %rsp register for whatever it chooses (possibly affected by an ABI). And of course a compiler for a different CPU won't use %rsp at all.

I suggest you take a step back and decide what you're actually trying to accomplish.

Upvotes: 2

Related Questions