MASM32连接程序时error A2006: undefined symbol : u

简介: MASM32连接程序时error A2006: undefined symbol : u

为了检测Windows操作系统是32位还是64位的,用MASM32编写了一个调用Windows API函数GetNativeSystemInfo的程序(完整代码附后),GetNativeSystemInfo函数会将指向SYSTEM_INFO结构体的地址存到GetNativeSystemInfo传递的参数中。

微软官网中的STEM_INFO定义为:

typedef struct _SYSTEM_INFO {
  union {
    DWORD dwOemId;
    struct {
      WORD wProcessorArchitecture;
      WORD wReserved;
    } DUMMYSTRUCTNAME;
  } DUMMYUNIONNAME;
  DWORD     dwPageSize;
  LPVOID    lpMinimumApplicationAddress;
  LPVOID    lpMaximumApplicationAddress;
  DWORD_PTR dwActiveProcessorMask;
  DWORD     dwNumberOfProcessors;
  DWORD     dwProcessorType;
  DWORD     dwAllocationGranularity;
  WORD      wProcessorLevel;
  WORD      wProcessorRevision;
} SYSTEM_INFO, *LPSYSTEM_INFO;

SYSTEM_INFO中的wProcessorArchitecture存储了已安装操作系统的处理器体系结构。

访问代码为:    

mov  eax, g_lpSI
                 mov  ax,  (SYSTEM_INFO ptr [eax]).DUMMYUNIONNAME.DUMMYSTRUCTNAME.wProcessorArchitecture
                 and  eax, 0ffffh
                 .if (eax==PROCESSOR_ARCHITECTURE_AMD64) || (eax==PROCESSOR_ARCHITECTURE_IA64) || (eax==PROCESSOR_ARCHITECTURE_ARM64)
                     mov eax, OFFSET g_sz64Bit
                 .else
                     mov eax, OFFSET g_sz32Bit
                 .endif

需要注意的两点,一是wProcessorArchitecture是word型数据,只有16位。二是MASM32中没有定义PROCESSOR_ARCHITECTURE_ARM64,在微软网官上其值为12。

PROCESSOR_ARCHITECTURE_ARM64   equ 12     ;ARM64

但是在连接程序时总是提示 32or64.asm(54) : error A2006: undefined symbol : u

image.png

第54行代码就是

mov  ax,  (SYSTEM_INFO ptr [eax]).DUMMYUNIONNAME.DUMMYSTRUCTNAME.wProcessorArchitecture

里面并没有使用符号u。

MASM32中的SYSTEM_INFO结构定义存在windows.inc中,居然跟微软官网上的不同:

SYSTEM_INFO STRUCT
  wProcessorArchitecture        WORD       ?
  wReserved                     WORD       ?
  dwPageSize                    DWORD      ?
  lpMinimumApplicationAddress   DWORD      ?
  lpMaximumApplicationAddress   DWORD      ?
  dwActiveProcessorMask         DWORD      ?
  dwNumberOfProcessors          DWORD      ?
  dwProcessorType               DWORD      ?
  dwAllocationGranularity       DWORD      ?
  wProcessorLevel               WORD       ?
  wProcessorRevision            WORD       ?
SYSTEM_INFO ENDS

MASM32中的SYSTEM_INFO结构定义代码中省略了微软官网其中的DUMMYUNIONNAME联合体和DUMMYSTRUCTNAME结构体,难怪使用.DUMMYUNIONNAME.DUMMYSTRUCTNAME.wProcessorArchitecture来访问在连接时会出错,奇怪的是出错信息里没有提示DUMMYUNIONNAME.DUMMYSTRUCTNAME未定义,而是莫明奇妙的u

将第54行代码改为

mov  ax,  (SYSTEM_INFO ptr [eax]).wProcessorArchitecture

后顺利完成连接,生成了EXE文件,但是更大的麻烦来了……

附完整·代码:

;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; 32or64.asm
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

.586
.model     flat, stdcall
option     casemap:none
include    \masm32\include\windows.inc
include    \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include    \masm32\include\user32.inc
includelib \masm32\lib\user32.lib

;sssssssssssssssssssssssssssssssssssssss
;.const
;sssssssssssssssssssssssssssssssssssssss
;PROCESSOR_ARCHITECTURE_INTEL   equ 0      ;x86
;PROCESSOR_ARCHITECTURE_ARM     equ 5      ;ARM
;PROCESSOR_ARCHITECTURE_IA64    equ 6      ;基于 Intel Itanium 的
;PROCESSOR_ARCHITECTURE_AMD64   equ 9      ;x64 (AMD 或 Intel)
PROCESSOR_ARCHITECTURE_ARM64   equ 12     ;ARM64
;PROCESSOR_ARCHITECTURE_UNKNOWN equ 0ffffh ;未知体系结构。


_SYSTEM_INFO STRUCT
   UNION
     dwOemId DWORD ?
     STRUCT
        wProcessorArchitecture        WORD       ?
        wReserved                     WORD       ?
     ENDS
  ENDS
  dwPageSize                    DWORD      ?
  lpMinimumApplicationAddress   DWORD      ?
  lpMaximumApplicationAddress   DWORD      ?
  dwActiveProcessorMask         DWORD      ?
  dwNumberOfProcessors          DWORD      ?
  dwProcessorType               DWORD      ?
  dwAllocationGranularity       DWORD      ?
  wProcessorLevel               WORD       ?
  wProcessorRevision            WORD       ?
_SYSTEM_INFO ENDS

;sssssssssssssssssssssssssssssssssssssss
.data
;sssssssssssssssssssssssssssssssssssssss
g_szCaption               db    "Windows",0
g_sz32Bit                 db    "32位",0
g_sz64Bit                 db    "32位",0
g_szGetNativeSystemInfo   db    "GetNativeSystemInfo", 0
g_szkernel32Dll           db    "kernel32.dll", 0
g_szFailGetModuleHandle   db    "GetModuleHandle失败", 0
g_szFailGetProcAddress    db    "GetProcAddress失败", 0
g_lpfnGetNativeSystemInfo dword ?
g_lpSI                    dword ?  ; SYSTEM_INFO


;sssssssssssssssssssssssssssssssssssssss
.code
;sssssssssssssssssssssssssssssssssssssss
start:
      invoke GetModuleHandle, OFFSET g_szkernel32Dll
      .if    eax==NULL
             mov eax, OFFSET g_szFailGetModuleHandle
      .else
             invoke GetProcAddress, eax, OFFSET g_szGetNativeSystemInfo
             .IF eax==NULL
                 mov eax, OFFSET g_szFailGetProcAddress
             .ELSE
                 mov  g_lpfnGetNativeSystemInfo, eax
                 push OFFSET g_lpSI
                 call g_lpfnGetNativeSystemInfo
                 mov  eax, g_lpSI
                 mov  ax, (SYSTEM_INFO ptr [eax]).wProcessorArchitecture
                 and  eax, 0ffffh 
                 ;mov  eax, (_SYSTEM_INFO ptr [eax]).dwOemId
                 .if (eax==PROCESSOR_ARCHITECTURE_IA64) || (eax==PROCESSOR_ARCHITECTURE_AMD64) || (eax==PROCESSOR_ARCHITECTURE_ARM64)
                     mov eax, OFFSET g_sz64Bit
                 .else
                     mov eax, OFFSET g_sz32Bit
                 .endif
             .ENDIF
      .endif
      invoke MessageBox, NULL, eax, OFFSET g_szCaption, MB_OK
      invoke ExitProcess, NULL
end start
相关文章
|
7月前
|
小程序 前端开发 API
【微信小程序】TypeError: Cannot read property ‘get‘ of undefined & Error: MiniProgramError
【微信小程序】TypeError: Cannot read property ‘get‘ of undefined & Error: MiniProgramError
|
7月前
|
资源调度 前端开发
编译第三方前端项目时候出现Syntax Error: TypeError: Cannot set properties of undefined (setting ‘parent‘)
编译第三方前端项目时候出现Syntax Error: TypeError: Cannot set properties of undefined (setting ‘parent‘)
505 0
|
6月前
|
JavaScript 前端开发 索引
JavaScript有7个数据类型:Number, String, Boolean, Null, Undefined, Symbol(BES6)和BigInt(ES10)组成基本类型
【6月更文挑战第25天】JavaScript有7个数据类型:Number, String, Boolean, Null, Undefined, Symbol(BES6)和BigInt(ES10)组成基本类型,而Object包括Array、Function等是引用类型。Objects可以包含键值对,Array是特殊的Object。Functions也是对象。`null`和`undefined`被视为特殊的原始值。
59 1
|
6月前
|
定位技术 数据库
程序技术好文:符号化Symbol(符号)体系
程序技术好文:符号化Symbol(符号)体系
46 0
Error:express-session deprecated undefined resave option; provide resave option app.js:17:10
Error:express-session deprecated undefined resave option; provide resave option app.js:17:10
Error:express-session deprecated undefined resave option; provide resave option app.js:17:10
|
7月前
|
JavaScript 前端开发
js基础语法:包括变量声明、数据类型(Number, String, Boolean, Null, Undefined, Symbol, Object)、运算符、流程控制语句(if...else, switch, for, while, do...while)等。具体案例使用演示
js基础语法:包括变量声明、数据类型(Number, String, Boolean, Null, Undefined, Symbol, Object)、运算符、流程控制语句(if...else, switch, for, while, do...while)等。具体案例使用演示
89 1
|
7月前
|
JavaScript 前端开发
JavaScript基础语法:包括变量声明、数据类型(Number, String, Boolean, Null, Undefined, Symbol, Object)、运算符、流程控制语句(if...else, switch, for, while, do...while)等。
JavaScript基础语法:包括变量声明、数据类型(Number, String, Boolean, Null, Undefined, Symbol, Object)、运算符、流程控制语句(if...else, switch, for, while, do...while)等。
71 0
|
4月前
|
存储 JavaScript 前端开发
成功解决:Cannot read properties of undefined (reading ‘commit‘)
这篇文章提供了解决Vuex中"Cannot read properties of undefined (reading 'commit')"错误的两种方法:检查模板中的数据属性是否存在,以及确保在Vue实例中正确挂载了store对象。
成功解决:Cannot read properties of undefined (reading ‘commit‘)
|
4月前
|
定位技术 Apache
Echarts——Invalid geoJson format Cannot read property 'length' of undefined
Echarts——Invalid geoJson format Cannot read property 'length' of undefined
110 0
|
4月前
|
JavaScript
VUE——filemanager-webpack-plugin报错TypeError: Cannot read property 'isFile' of undefined
VUE——filemanager-webpack-plugin报错TypeError: Cannot read property 'isFile' of undefined
93 0