News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Access is denied ?

Started by gelatine1, April 20, 2014, 05:26:25 AM

Previous topic - Next topic

gelatine1

Hi I have the code below but something weird happens. It compiles without any problem but when I run it I get the message 'Access is denied.' Why am I getting this message?
This is my code:

.386
.model flat, stdcall
option casemap :none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib

.data
HelloWorld db "Hello World!", 0

.code
tst:
pop eax ;get address of HelloWorld from stack
inc eax ;increase that address
xor edx,edx ;edx is set to 0
mov [eax],edx ; the second character of HelloWorld is set to 0
ret

start:

lea eax,HelloWorld
push eax ;invoke tst, addr HelloWorld
call tst ;invoke tst, addr HelloWorld

push eax
call StdOut ;invoke StdOut, addr HelloWorld

push 0
call ExitProcess ;invoke exitprocess,0

end start



Thanks in advance

GoneFishing

Learn to use a debugger
First, when I assembled your code the real entry point was label tst . So I moved it below the start label.
Next, in your tst code you get the return address of the caller in eax and not an address of the string.

Vortex

To get the address of the string :


mov   eax,OFFSET HelloWorld

GoneFishing

#3
...

jj2007

With a few changes, you will see the "H" as expected:
tst:
      pop edx  ; yep, you forgot the return address!!
      pop eax ;get address of HelloWorld from stack
      push edx
      inc eax ;increase that address
      xor edx,edx ;edx is set to 0
      mov [eax],edx ; the second character of HelloWorld is set to 0
      ret
     
start:
      lea eax, HelloWorld
      push eax
      push eax ;invoke tst, addr HelloWorld
      call tst ;invoke tst, addr HelloWorld
     
      ; push eax?? The inc'ed one?? No...
      call StdOut ;invoke StdOut, addr HelloWorld


Quote from: vertograd on April 20, 2014, 06:44:08 AM
It might be a good idea to give yourself  and us a time to look at output:

Sleep(3000) is somewhat suboptimal. Either use an intelligent IDE (like RichMasm) that keeps the console open if it detects a console app with no inkey, or
   include \masm32\macros\macros.asm
   includelib \masm32\lib\msvcrt.lib
   inkey chr$(13,10, 10, "bye")

gelatine1

So in fact eax remains a global variable ? I assumed that i had a local storage of eax in my tst 'function' and that it got removed after the ret but clearly it doesnt work like that.
Thanks jj2007 and others too

jj2007

Quote from: gelatine1 on April 20, 2014, 08:30:28 PM
So in fact eax remains a global variable ?

No, eax is a register. This is not C/C++...

Read about register preservation in ttt