1

I'm currently trying to push the address of a data variable to the stack. In a separate procedure, how would I change the actual value of this data variable located on the stack?

I'm using Visual Studio as my assembler and using the Irvine32.lib library for this code. This is what I've been trying but there is an error.

I expect the value of 'counter' to be changed from 1 to 2 but it doesn't work. How would I go about this? Thanks!

include Irvine 32.inc

.data
counter dword 1

.code
main proc
    mov eax, counter
    call writeDec       ; should print 1

    push offset counter ; push address of 'counter' on stack
    call foo            ; this procedure changes counter from 1 to 2

    mov eax, counter    ; store counter in eax for writeDec procedure
    call writeDec       ; should print 2
    exit
main endp

foo proc
    push eax            ; save eax register state
    mov eax, esp        ; store the stack pointer into eax
    add eax, 8          ; navigate through the stack

    ; this doesn't assemble
    mov [eax], 2        ; dereference the stack pointer and set it to 2
    pop eax             ; restore eax register state
    ret
foo endp
end main
Kyle
  • 63
  • 1
  • 6
  • 1
    Although this code isn't doing what you expect (you need ot dereference the pointer on the stack), I assume it doesn't assemble because it has to be`mov dword ptr [eax], 2` ? – Michael Petch May 22 '19 at 00:52
  • 3
    Your passed the address on the stack. You need to fetch that address from there. E.g. your procedure could look like: `mov eax, [esp+4]; mov dword ptr [eax], 2; ret`. – Jester May 22 '19 at 00:54
  • Awesome, thank you for your comments! – Kyle May 22 '19 at 04:21

0 Answers0