gpt4 book ai didi

assembly - 你如何比较汇编中的变量类型?

转载 作者:行者123 更新时间:2023-12-01 00:42:02 25 4
gpt4 key购买 nike

这可能是一个愚蠢的语法问题,但是有没有一种方法可以根据变量类型进行条件跳转?我正在尝试编写一个宏(用于类),它可以将字节、字或双字作为参数并将其写入屏幕。

mWriteInt MACRO integer:REQ
;cmp integer, DWORD
;je dwordOp
movsx eax, word ptr integer
call WriteInt
mov edx, OFFSET endl
call WriteString
; for a DWORD
; dwordOp:
ENDM

所以基本上,根据传递给宏的变量类型,执行的代码应该有所不同。无论我如何尝试执行此操作,都会遇到编译器错误。

我试过了:
 cmp integer, DWORD
cmp TYPE integer, DWORD

我真的不知道从哪里开始。我查看了我能想到的所有引用资料,但这似乎并不常见

编辑:
mWriteInt MACRO integer:REQ
IF (TYPE integer EQ TYPE DWORD)
call WriteInt
ENDIF

IF (TYPE integer EQ TYPE BYTE)
call WriteInt
ENDIF

IF (TYPE integer EQ TYPE WORD)
movsx eax, word ptr integer
call WriteInt
ENDIF

mov edx, OFFSET endl
call WriteString
ENDM

最佳答案

在 MASM 中有 OPATTR运算符(operator)。引自 the MASM reference :

Returns a word defining the mode and scope of expression. The low byte is identical to the byte returned by .TYPE. The high byte contains additional information.



值如下,取自 MASM Basic引用源代码 here at the MASM forum :
;     OPATTR guide
; Bit Set If...
; 0 References a code label
; 1 Is a memory expression or has a relocatable data label
; 2 Is an immediate expression
; 3 Uses direct memory addressing, i.e. is an absolute memory reference
; 4 Is a register expression
; 5 References no undefined symbols and is without error
; 6 References a stack location (usually a LOCAL variable or parameter)
; 7 References an external label
; 8-10 Language type (000=no type)
; 000 - no language type
; 001 - C/C++ language type
; 010 - SYSCALL language type
; 011 - STDCALL language type
; 100 - Pascal language type
; 101 - FORTRAN language type
; 110 - BASIC language type

提到了一些用法示例:
atMemory      = 34      ; 00100010      ; [edx+20], [ebx+20], [eax+edx+20], JWasm: [eax+4*eax+20], [eax+20]
atImmediate = 36 ; 00100100
atLabel = 37 ; 10100101
atOffset = 38 ; 10100110 ; offset CrLf$ (immediate and mem expression)
atGlobal = 42 ; 10101010 ; CrLf$, Masm: [eax+4*eax+20], [eax+20]
atRegLabel = 43 ; 10101011 ; Masm: [eax+start] (Jwasm yields 37)
atRegister = 48 ; 00110000 ; also xmm
atXmm = 77 ; xxxxxxxx ; reg starting with x
atLocal = 98 ; 01100010 ; [esp+20], [ebp+20]

你的宏代码的一个例子是
mWriteInt MACRO integer:REQ
IF(OPATTR(integer) EQ 24h AND SIZEOF(integer) EQ 4) ; immediate and no undefined symbols
; for a DWORD
mov eax, dword ptr integer
call WriteInt
ELSEIF (OPATTR(integer) EQ 24h AND SIZEOF(integer) EQ 2) ; immediate and no undefined symbols
; for a WORD
movsx eax, word ptr integer
call WriteInt
ELSEIF (OPATTR(integer) EQ 24h AND SIZEOF(integer) EQ 1) ; immediate and no undefined symbols
; for a BYTE
movsx eax, byte ptr integer
call WriteInt
ENDIF
mov edx, OFFSET endl
call WriteString
ENDM

如果此 MACRO 代码不完全符合您的预期,您可以调整 OPATTR值通过组合位值。

添加一件事来描述两种 IF 变体之间 MASM 的差异:
IF   --- is compile time comparison  
.IF --- is runtime comparison

关于assembly - 你如何比较汇编中的变量类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36973763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com