Unhandled exception at 0x777745BA (ntdll.dll) in MASM1.exe: 0xC0000005: Access violation writing location 0x00000014. I'm using x86 assembly in visual studios 2017 and it keeps returning this error
I have included all of the libraries and installed windows 10 sdk. I am basically stumped as of why this is returning this error on line 21. It even opens a blank window and then immediately closes it returning the error.
.586
.MODEL FLAT
.STACK 4096
includelib libcmt.lib
includelib libvcruntime.lib
includelib libucrt.lib
includelib legacy_stdio_definitions.lib
EXTERN printf:PROC
EXTERN scanf:PROC
.DATA
format BYTE "Enter a number", 0
.CODE
main PROC
sub esp, 4
push offset format
call printf
add esp, 4
ret
main ENDP
END
I created a VS 2017 C++ project generating a Win32 console program. In the project properties / Linker
/ Advanced
/ entry point
option I have set the entry point to main
.
ret
; this version has an obvious huge bug. – Geriatricianwriting location 0x00000014
as the error withret
? On which instruction? Is it insideprintf
, or after main returns? – Geriatricianprintf
. So you need toadd esp, 8
before returning to clean up both the pushedformat
argument and to adjust for thesub esp, 4
– Conners.MODEL FLAT, C
for it to properly linkmain
since it really should resolve to_main
. You say you are using Visual Studio 2017. Are you using custom assemble and link commands and if so what are the commands you are using to assemble and link? I have a suspicion you didn't create a VS2017 MSVC project with the IDE the normal way (by adding MASM as a build dependency) and then add an ASM file with this code. – Cosecprintf
won't run properly) and you have managed to force the entry point for your program to be main (with a command line option?). This is on top of the factadd esp, 4
should beadd esp, 8
. I was able to reproduce your problem by forcingmain
to be the entry point by linking with the/ENTRY
option. The other possibility is that what you are showing here for assembly code is not what you are actually using. – Cosec.MODEL FLAT
to.MODEL FLAT, C
then change theADD ESP, 4
(afterprintf
) toADD ESP, 8
and if you are linking with/ENTRY
remove that option. That's what I recommend as long as you don't show your assembling and linking commands (and all the options). – Cosec.MODEL FLAT, C
so for this code to assemble and link without undefined references something external had to be set to override the default behaviour. For instance without changing the default behaviorEXTERN printf:PROC
would have had to have beenEXTERN _printf:PROC
andmain PROC
would have had to have been_main PROC
etc. – Cosec