Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

x86 asm, dereferenced pointer not getting updated

Here's a test procedure from a program I'm working on, I pass in some parm's via the stack, one of which is a pointer. When I try to change the value of the dereferenced pointer, the variable isn't updated.

_testProc proc
    push bp                             ;Save base pointer to stack
    mov bp, sp                          ;Set new base pointer
    sub sp, 4                           ;Allocate stack space for locals
    pusha                               ;Save registers to stack

    mov di, [bp + 08]                   ;Parm 3 - ptr to variable

    mov word ptr [di], 10   ; <---- Doesn't work. di contains an address, 
                            ;       but what it points at doesn't get updated

    popa                                ;Restore registers from stack
    mov sp, bp                          ;Remove local vars by restoring sp
    pop bp                              ;Restore base pointer from stack
    ret 6                               ;Return and also clean up parms on stack
_testProc endp
like image 885
Mark Avatar asked Dec 02 '12 06:12

Mark


2 Answers

The 8086 produces and address by combining the contents of a segment register and an index register; I show that as [SR,IR].

Your update via register di is updating a location defined by [DS,DI]; mov instructions without any special prefix default to using the DS register. If you got the address DI as an offset for some other segment (ES? SS?) then you are in effect combining the wrong registers to hit the address you desire.

Your mistake is in not being clear about what the conventions are for passing a "pointer" to your routine. What you've define assume a relative offset from DS.

like image 95
Ira Baxter Avatar answered Sep 26 '22 23:09

Ira Baxter


The very best thing you can do is to abandon 16-bit segmented code as soon as you can! :)

Failing that, there's "far data" and a "far pointer" to point to it. Your "proc" doesn't say if it's near or far - I assume near (or Parm3 probably isn't where you think it is on the stack... since the far return address is 4 bytes). If the variable you intend to alter is on the stack, you're in for some more complication. mov word ptr ss:[di], 10 at least. If you need to handle either a local or static variable, I think you're going to need a far pointer (4 bytes, segment and offset) to find it.

What first came to my mind is that you say you're trying to change the value of a dereferenced pointer, you don't "dereference" it (as I understand it). Try mov di, [di] after you get the value off the stack. Easy to try, anyway. :)

If all else fails, show us the calling code. (and get into 32-bit code as soon as you can!)

like image 26
Frank Kotler Avatar answered Sep 24 '22 23:09

Frank Kotler