Setting value stored at address using x86 ASM

user1727141

I am trying to get my head around some inline ASM but for some reason this isn't behaving as I would have expected. Why isn't this setting the value of x equal to 62?

#include <stdio.h>

int main()
{
    int x = 525;
    int* y = &x;

    _asm
    {
        mov eax, 62
        mov [y], eax
    }

    printf("%i", x);
    getchar();
    return 0;
}

The code results in 525 being output. I expected it to be 62.

Notlikethat

There's a perfectly excusable misunderstanding here:

surely [y] would mean [0xCCCCCCCC] (assuming the address of x was 0xCCCCCCCC)

In high-level theory, yes. The trouble is, in actual assembly [0xCCCCCCCC] makes no sense - the CPU can't dereference a memory address directly - it can only load a value from that address into a register, then dereference that.

Similarly, since y is a variable, not a register, it's implicitly treated as an address1 i.e. y inside the asm block is the equivalent of &y in the C code outside2. As you can see by stepping through in a debugger, what happened is the assembler simply ignored the brackets that don't make sense (rather than throwing a helpful error) and assembled the equivalent of mov y, eax.

The way to get what you expect would be something like this:

asm {
    mov eax, 62
    mov edx, y
    mov [edx], eax
}

[1] this clearly isn't GCC. GCC extended asm is a whole different ball game...

[2] somewhat of a simplification - it's an "address" from a C point of view, but in assembly context it's a memory operand, which is really more like the use of an address. When I compiled this, y came out as [ebp-20], which from the high level view is "an address on the stack".

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Manual Null-Termination on Unix using Intel x86 Assembly (address vs value in memory operands)?

From Dev

x86 - setting a bit using inline assembly

From Dev

ASM x86 Push and pop

From Dev

x86 ASM - Read line by line

From Dev

ASM x86 Push and pop

From Dev

ROL in x86 ASM in PROC parameter

From Dev

asm X86 - segmentation fault?

From Dev

Get register value by detouring specific address [x86 assembly on Windows]

From Dev

gdb:How to print value at memory address in ASM

From Dev

Branch to an address using GCC inline ARM asm

From Dev

Checking if an address is writable in x86 assembly

From Dev

CMP in x86 with parentheses and address

From Dev

load overflow topmost address on x86

From Dev

CMP in x86 with parentheses and address

From Dev

[x86 ASM]Running a graphical debugger with arguments

From Dev

Writing to stack as local variable in _start function (x86 ASM)

From Dev

Latency semantics of read registers on x86 ASM

From Dev

Inline x86 asm for dividing by 2 in C

From Dev

setting a value using ISNULL?

From Dev

Gcc inline ASM, set EBX to char array address using LEA

From Dev

What prevents me from jumping to any absolute address using ASM?

From Dev

What prevents me from jumping to any absolute address using ASM?

From Dev

(x86) Is the value of ESP realtive to EBP, or not?

From Dev

Using BIOS interrupts in x86

From Dev

x86: using memory / swapping values?

From Dev

using atof function in x86 NASM

From Dev

Semaphore implementation using x86 assembly

From Dev

Using x86 CUPS filter on Raspbian

From Dev

Setting up interrupts in protected mode (x86)