0

First of all, I apologize if my English is not very good, and if there are any parts of my description that are unclear.

I am currently learning how to write MASM and referring to Irvine's textbook. I have written a program that takes an integer input and prints out an integer. However, I'm not sure why the instruction mov var1, eax is causing an error with the message "Exception thrown: 0xC0000005: Access violation - writing to address.".

I don't understand why this error is occurring because the textbook states that "mov mem, reg" should be valid. I would appreciate it if someone could help me with this issue. Thank you.

Here is the code:

INCLUDE Irvine32.inc

INCLUDELIB Irvine32.lib

.data

str1 BYTE "enter a number:",0

.code

main PROC
call Clrscr
var1 DWORD ?     ;Declare the variable var1 as DWORD type.

mov edx,OFFSET str1

call WriteString ;Output string.

call ReadInt     ;Read an integer.



mov var1,eax     ;erroe,Exception thrown: 0xC0000005
call WriteInt    ;Display an integer.

exit

main ENDP

END main

I have tried changing the line var1 DWORD ? to var1 DWORD 0 in an attempt to address the issue, but it didn't resolve the problem. The issue still persists.

Michael
  • 57,169
  • 9
  • 80
  • 125
JoeYang
  • 9
  • 2
  • 1
    Don't put data declarations in the middle of code like that. Either move it to the `.data?` or `.data` section if you want `var1` to be global. Or if you want `var1` to be local to `main` then declare it as `LOCAL var1:DWORD`, in which case you probably should put the declaration at the top of the procedure (i.e. on the first line after `main PROC`). – Michael May 25 '23 at 11:14
  • The `.code` section is read-only. So storing to a `dword` in it will definitely fault. Of course, if you use a debugger, you'll probably see it fault earlier, when it tries to decode the 4 bytes of zeros you inserted into the machine code of your function with `dword ?`! `00 00` is `add [eax], al` in 32-bit mode, and EAX probably isn't a valid pointer at that point. – Peter Cordes May 25 '23 at 11:59

0 Answers0