FASM程序在编译后崩溃 [英] FASM Programs crash after compiling them

查看:129
本文介绍了FASM程序在编译后崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我刚刚开始学习Windows的平面汇编程序(很好尝试).每次我尝试为变量赋值时,都会收到内存访问冲突错误c0000005.这让我发疯了.

Hi I’ve just started learning flat assembler for windows (well trying). Every time I try to assign a variable a value I get a memory access violation error c0000005. It''s driving me crazy.

mov [var],4


示例程序可以正常运行,但是我不能设置一个变量而不会崩溃.有什么想法吗?

代码


The example programs will run fine but I can''t set a variable without it crashing. Any ideas?

The code

; example of simplified Windows programming using complex macro features
include 'win32ax.inc' ; you can simply switch between win32ax, win32wx, win64ax and win64wx here

.code
var   Dd  3
  start:
mov eax,5

mov [var],4 ;<----- causes the crash (will work without)

invoke  MessageBox,HWND_DESKTOP,var,invoke GetCommandLine,MB_OK
invoke  ExitProcess,0
.end start

推荐答案

访问冲突意味着您试图写入不属于程序地址空间的内存地址.这就是CPU的保护模式应该保护的内容:任何运行amok的程序都不会侵犯另一个程序的内存空间.

一旦您确实想做一些更复杂的事情,就必须了解PC上的内存寻址和分页.现在,以正确的方式设置变量"就足够了.

在汇编中,您只需要为要存储的值保留正确的字节数即可.标签(您将其称为变量名)表示第一个保留字节的内存地址.由于您遇到访问冲突,因此此标签表示一个无效的地址,该地址不属于您的程序.

显然,当您为自己的值保留内存时,您做错了什么.请发布您代码的那一部分(包括所有汇编指令),我们应该尽快解决.


感谢您发布代码.

An access violation means that you are trying to write to a memory address that does not belong to your program''s address space. That''s the what the protected mode of the CPU is supposed to protect: No program running amok can violate another program''s memory space.

Memory addressing and paging on a PC is a matter you must understand once you really want to do some more complicated things. For now it will be sufficient to set up the ''variables'' the right way.

In assembly you simply have to reserve the right amount of bytes for the value you want to store. The label (you would call it the variable name) represents the memory address of the first reserved byte. Since you get an access violation, this label represents an invalid address which does not belong to your program.

Obviously you did something wrong when you reserved the memory for your value. Please post that part of your code (including any assembly directives) and we should resolve this quickly.


Thanks for posting the code.

Sam Homer写道:
Sam Homer wrote:

var Dd 3



像这样尝试:



Try it like this:

Var Dd ?



我从汇编器的主页上得到了一个简短的示例.查看_hheap标签以及如何声明和使用它.



I have gotten a short sample from the assembler''s homepage. Look at the _hheap label and how it is declared and used.

; GetMainArgs v1.01
; Copyright © 2003 Theodor-Iulian Ciobanu

format PE GUI 4.0
entry start

include ''win32a.inc''
include ''cmd.inc''

  start:
        invoke  GetProcessHeap
        mov     [_hheap],eax
        invoke  HeapAlloc,[_hheap],HEAP_ZERO_MEMORY,1000h
        mov     [_strbuf],eax

        call    GetMainArgs
        mov     esi,[_argv]
        cinvoke wsprintf,[_strbuf],_fmt1,[_argc]
        mov     ebx,[_argc]
    @@:
        cinvoke wsprintf,[_strbuf],_fmt2,[_strbuf],[esi]
        add     esi,4
        dec     ebx
        cmp     ebx,0
        jnz     @b
        invoke  MessageBox,0,[_strbuf],_msgcap,MB_ICONINFORMATION+MB_OK

        invoke  HeapFree,[_hheap],0,[_argv]
        invoke  HeapFree,[_hheap],0,[_strbuf]
        invoke  ExitProcess,0

_strbuf  dd ?
_fmt1    db ''%u'',0
_fmt2    db ''%s, "%s"'',0
_msgcap  db ''Command line parameters'',0
_hheap   dd ?

data import
 library kernel,''KERNEL32.DLL'',\
         user,''USER32.DLL''

 import kernel,\
         GetCommandLine,''GetCommandLineA'',\
         GetProcessHeap,''GetProcessHeap'',\
         HeapAlloc,''HeapAlloc'',\
         HeapFree,''HeapFree'',\
         ExitProcess,''ExitProcess''

 import user,\
        MessageBox,''MessageBoxA'',\
        wsprintf,''wsprintfA''
end data


您确定您的变量应位于.code段中吗?自从我执行汇编程序已经很长时间了,但是我始终将变量保存在.data段中.
Are you sure your variable should be in the .code segment? It''s a long time since I did assembler but I always kept my variables in the .data segment.


这篇关于FASM程序在编译后崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆