当前位置: 首页 > 工具软件 > FASM > 使用案例 >

FASM汇编编译器

叶声
2023-12-01

FASM支持x86/x64指令集,有DOS、Windows、Linux版本。输出格式也有多种:MZ, PE, COFF or ELF。

此编译器免费开源,源代码全部使用汇编语言编写。还带一个IDE,支持语法高亮,IDE同样也开放源码。另外还有一个命令行模式的编译器,使用也极为简单,基本上除输入输出文件外不需要任何其它参数,不像MASM的ML有一堆参数,FASM编译用的几个主要参数在源文件中用伪指令来设置。

 

The flat assembler is a fast and efficient self-assembling x86 assembler for DOS, Windows and Linux operating systems. Currently it supports x86 and x86-64 instructions sets with MMX, 3DNow!, SSE up to SSE4, AVX and XOP extensions, can produce output in binary, MZ, PE, COFF or ELF format. It includes the powerful but easy to use macroinstruction support and does multiple passes to optimize the instruction codes for size.

 

Click here to download the latest version of FASM.

 

一般编译程序会在参数中设置输出类型,比如是dll还是exe,是GUI还是Console,是16位还是32位等。

在FASM中这些设置都不在参数中,而是使用伪指令实现。

These directives are actually also a kind of control directives, with the purpose of controlling the format of generated code.

format directive followed by the format identifier allows to select the output format. This directive should be put at the beginning of the source. Default output format is a flat binary file, it can also be selected by using format binary directive. This directive can be followed by theas keyword and the quoted string specifying the default file extension for the output file. Unless the output file name was specified from the command line, assembler will use this extension when generating the output file.

use16 and use32 directives force the assembler to generate 16-bit or 32-bit code, omitting the default setting for selected output format.use64 enables generating the code for the long mode of x86-64 processors.

Below are described different output formats with the directives specific to these formats.

MZ executable, 编写16位代码时默认格式.

format MZ 

 

Portable Executable ,32位编程时一般使用PE。

format PE GUI 4.0 DLL at 7000000h on 'stub.exe'   ; dll project.
format PE GUI 4.0  ; GUI project.
format PE Console  ; console project.

 

Common Object File Format, 输出obj文件。可以使用其它连接器进行连接。

format COFF
; or
format MS COFF
; To create the file in Microsoft's COFF format for the x86-64 architecture.
format MS64 COFF


Executable and Linkable Format 

format ELF
; To create ELF file for the x86-64 architecture
format ELF64


 

编写FASM汇编曲程序只需要一个ASM文件就可以,编译好后直接生成目标文件,不需要link程序连接库或obj文件,就是调用Win32 API也不要lib文件,它导入API也有特别。

 

; 在源码中增加一个导入数据节
section '.idata' import data readable writeable

  library kernel32, 'kernel32.dll', \
    user32, 'user32.dll', \
    gdi32, 'gdi32.dll', \
    comctl32, 'comctl32.dll'

  include 'apia\kernel32.inc'
  include 'apia\user32.inc'
  include 'apia\gdi32.inc'
  include 'apia\comctl32.inc'

 library是宏定义,也可以通过宏import自定义导入函数。

它的格式macro import name,[label,string]

在代码中import user32 代码中使用的名称, '实际dll导出的函数名'

The import macroinstructions help to build the import data for PE file (usually put in the separate section). There are two macroinstructions for this purpose. The first one is calledlibrary, must be placed directly in the beginning of the import data and it defines from what libraries the functions will be imported. It should be followed by any amount of the pairs of parameters, each pair being the label for the table of imports from the given library, and the quoted string defining the name of the library. For example:

library kernel32,'KERNEL32.DLL',\
          user32,'USER32.DLL'

 

declares to import from the two libraries. For each of libraries, the table of imports must be then declared somewhere inside the import data. This is done withimport macroinstruction, which needs first parameter to define the label for the table (the same as declared earlier to thelibrary macro), and then the pairs of parameters each containing the label for imported pointer and the quoted string defining the name of function exactly as exported by library. For example the abovelibrary declaration may be completed with followingimport declarations:

import kernel32,\
         ExitProcess,'ExitProcess'

  import user32,\
         MessageBeep,'MessageBeep',\
         MessageBox,'MessageBoxA'

 

在编写dll工程时需要导出函数,可以使用export宏。

The export macroinstruction constructs the export data for the PE file (it should be either placed in the section marked as export, or within thedata export block. The first argument should be quoted string defining the name of library file, and the rest should be any number of pairs of arguments, first in each pair being the name of procedure defined somewhere inside the source, and the second being the quoted string containing the name under which this procedure should be exported by the library. This sample:

export 'MYLIB.DLL',\
         MyStart,'Start',\
         MyStop,'Stop'


 FASM在代码中也能直接将资源文件放在代码中。代码中的节和PE文件中的一一对应。

其中directory、resource 、menu 、menuitem 、icon 、bitmap 、dialog 、dialogitem 等都是宏定义。定义在include\macro\RESOURCE.INC文件中。

 

The directory macroinstruction must be placed directly in the beginning of manually built resource data and it defines what types of resources it contains. It should be followed by the pairs of values, the first one in each pair being the identifier of the type of resource, and the second one the label of subdirectory of the resources of given type. 

 

The subdirectories can be placed anywhere in the resource area after the main directory, and they have to be defined with theresource macroinstruction, which requires first parameter to be the label of the subdirectory (corresponding to the entry in main directory) followed by the trios of parameters - in each such entry the first parameter defines the identifier of resource (this value is freely chosen by the programmer and is then used to access the given resource from the program), the second specifies the language and the third one is the label of resource. Standard equates should be used to create language identifiers.

 

; 在代码中添加一个资源节。
section '.rsrc' resource data readable

  ; resource directory

  ; macro directory [type, label]
  directory RT_MENU, menus, \
	    RT_GROUP_ICON, igroups, \
	    RT_VERSION, versions, \
	    RT_BITMAP, bitmaps, \
	    RT_DIALOG, dialogs, \
	    RT_ICON, icons
  ; resource subdirectories
  ; macro resource dir, [id, lang, label]
  resource menus, IDR_MENU_MAIN, LANG_ENGLISH + SUBLANG_DEFAULT, main_menu

  resource igroups, IDR_ICON_MAIN, LANG_NEUTRAL, igroup1

  resource icons, IDR_ICON_USER1, LANG_NEUTRAL, icon1, \
                  IDR_ICON_USER2, LANG_NEUTRAL, icon2

  resource bitmaps, IDR_BMP_USER1, LANG_NEUTRAL, bitmap1, \
                    IDR_BMP_USER2, LANG_NEUTRAL, bitmap2, \
                    IDR_BMP_USER3, LANG_NEUTRAL, bitmap3

  resource dialogs, IDR_DLG_USER1, LANG_ENGLISH + SUBLANG_DEFAULT, dialog1

  resource versions, $1, LANG_NEUTRAL, version1

  ; macro fileres label,file_name
  ; ...

  ; macro accelerator label,[fvirt,key,cmd]
  ; ...

  ; macro menu label
  ; macro menuitem string, id, resinfo, type, status
  ; macro menuseparator resinfo, status
  menu main_menu
    menuitem '&File', $0, MFR_POPUP
		menuitem '&New', IDM_NEW, $0
		menuseparator
		menuitem 'E&xit', IDM_EXIT, MFR_END
    menuitem '&Help', $0, MFR_POPUP + MFR_END
		menuitem '&About...', IDM_ABOUT, MFR_END

  ; macro icon group, [label, icon_file], ...
  icon igroup1, icon1, 'icon.ico', \
                icon2, 'icon.ico'

  ; macro bitmap label, bitmap_file
  bitmap bitmap1, 'toolbar.bmp'

  ; macro dialog label, title, x, y, cx, cy, style, menu, exstyle, fontname, fontsize
  ; macro dialogitem class, title, id, x, y, cx, cy, style, exstyle
  dialog dialog1, 'Exception', $0, $0, $120, $80, WS_CAPTION or WS_POPUP or WS_SYSMENU or DS_MODALFRAME
    dialogitem 'STATIC', 'Information:', -1, $a, $5, 70, 8, WS_VISIBLE
    dialogitem 'EDIT', '', IDR_EDIT1, 10, $f, $10c, 85, WS_VISIBLE or WS_BORDER or WS_TABSTOP or ES_MULTILINE or ES_READONLY or ES_AUTOVSCROLL
    dialogitem 'BUTTON', '&Break', IDR_BUTTON1, 10, $6a, $3d, $10, WS_VISIBLE or WS_TABSTOP or BS_DEFPUSHBUTTON
    dialogitem 'BUTTON', '&Continue', IDR_BUTTON2, 100, $6a, $3d, $10, WS_VISIBLE or WS_TABSTOP or BS_DEFPUSHBUTTON
    dialogitem 'BUTTON', 'C&ancel', IDR_BUTTON3, 190, $6a, $3d, $10, WS_VISIBLE or WS_TABSTOP or BS_PUSHBUTTON
  enddialog

  ; macro version label, fileos, filetype, filesubtype, lang, cp, [name,value]
  version version1, VOS__WINDOWS32, VFT_APP, VFT2_UNKNOWN, LANG_ENGLISH + SUBLANG_DEFAULT, $0, \
	  'CompanyName', 'perry', \
	  'FileDescription', 'Process manager', \
	  'LegalCopyright', 'perry <ppsoft268@gmail.com>', \
	  'LegalTrademarks', 'perry <ppsoft268@gmail.com>', \
	  'OriginalFilename', 'taskmgr.exe', \
	  'InternalName', 'taskmgr.exe', \
	  'ProductVersion', '1.0.0.0', \
	  'ProductName', '1.0.0.0', \
	  'FileVersion', '1.0.0.0'
 类似资料: