1

I'm trying to create a little example of 'How to use asm block in C code'. In my example, i'm trying to increment a value of variable which I created in my C code.

This is my code:

int main()
{
    unsigned int i = 0;
    unsigned int *ptr1;

    // Get the address of the variable i.
    ptr1 = &i;

    // Show ECHO message.
    printf_s("Value before '_asm' block:");
    printf_s("\ni = %d (Address = ptr1: %d)\n\n", i, ptr1);

    _asm {

        // Copy the value of i from the memory.
        mov bx, word ptr [ptr1]

        // Increment the value of i.
        inc bx

        // Update the new value of i in memory.
       mov word ptr [ptr1], bx
    }

    // Show ECHO message.
    printf_s("Value after '_asm' block:");
    printf_s("\ni = %d (Address = ptr1: %d)\n\n", i, ptr1);

    // Force the console to stay open.
    getchar();

    return 0;
}

This is the result of the code in the console:

Values before '_asm' block: i = 0 (Address = ptr1: 1441144)

Values after '_asm' block: i = 0 (Address = ptr1: 1441145)

This is very wierd. I only want to update the value of the 'i' variable, but it doesn't work. In addition, the pointer 'ptr1' now points to the next memory block...

Why is this happening ? And how should I solve this problem?

EDIT:

Thanks to the comments below, I solved the problem. The main change is in this line:

// Increment the value of i.
inc bx

Due to the fact that we want to increment the VALUE of the variable 'i', we should use brackets. In addition, the bx register should be changed now to 'ebx', that is a 32-bit register. Because of using the 'ebx' register, the expression 'word ptr' should be replaced with 'dword ptr'.

The code of the asm block, after the editings:

_asm {

    // Copy the value of i from the memory.
    mov ebx, dword ptr [ptr1]

    // Increment the value of i.
    inc [ebx]

    // Update the new value of i in memory.
    mov dword ptr [ptr1], ebx
}
Aviv
  • 456
  • 1
  • 7
  • 16
  • `inc bx` should probably read `inc [bx]`. – NPE Sep 29 '14 at 12:50
  • I believe your mov should be `mov bx, word [ptr1]` as with the additional ptr you are doing two levels of indirection and not one. – Richard Chambers Sep 29 '14 at 12:52
  • @NPE in my example, i'm saving the VALUE of the variable 'i' in bx. so if I will do it like this: inc [bx] The code will refer to the value of 'i' as an address.. – Aviv Sep 29 '14 at 12:56
  • 1
    No, you are not. You're loading the pointer value into bx, incrementing that pointer value, and then storing it back in `ptr1`. You need do anything to the pointed-to value. – NPE Sep 29 '14 at 12:58
  • see also [ptr usage confusion](http://stackoverflow.com/questions/688799/dword-ptr-usage-confusion) and [What does dword ptr mean](http://stackoverflow.com/questions/2987876/what-does-dword-ptr-mean) and [What does mov eax dword ptr mean and what does it do](http://stackoverflow.com/questions/3224524/what-does-mov-eax-dword-ptr-dsesi-mean-and-what-does-it-do) – Richard Chambers Sep 29 '14 at 12:58
  • 1
    In addition to what NPE said: It looks like you're using a 32-bit or 64-bit compiler (the value of `ptr1` won't fit in 16 bits), but your code is reading the pointer into `bx` and treating it as 16 bits, which is wrong. – interjay Sep 29 '14 at 13:01
  • **NPE** & **RichardChambers**, thank you both for your help. You were right, and now I solved the problem. I also edited the topic with little explanation. – Aviv Sep 29 '14 at 13:25

0 Answers0