The MASM Forum

General => The Campus => Topic started by: gelatine1 on April 20, 2014, 05:26:25 AM

Title: Access is denied ?
Post by: gelatine1 on April 20, 2014, 05:26:25 AM
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
Title: Re: Access is denied ?
Post by: GoneFishing on April 20, 2014, 06:31:53 AM
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.
Title: Re: Access is denied ?
Post by: Vortex on April 20, 2014, 06:34:06 AM
To get the address of the string :


mov   eax,OFFSET HelloWorld
Title: Re: Access is denied ?
Post by: GoneFishing on April 20, 2014, 06:44:08 AM
...
Title: Re: Access is denied ?
Post by: jj2007 on April 20, 2014, 08:11:22 AM
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")
Title: Re: Access is denied ?
Post by: gelatine1 on April 20, 2014, 08:30:28 PM
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
Title: Re: Access is denied ?
Post by: jj2007 on April 20, 2014, 08:42:03 PM
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 (http://www.webalice.it/jj2006/Masm32_Tips_Tricks_and_Traps.htm)