如何比较汇编中的变量类型? [英] How do you compare variable types in assembly?

查看:129
本文介绍了如何比较汇编中的变量类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This might be a bit of a stupid syntax question, but is there a way you can make conditional jumps based on variable type? I'm trying to write a macro (for a class) that can take a byte, word, or double word as an argument and write it to the screen.

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

因此,基本上,执行的代码应根据将哪种类型的变量传递给宏而有所不同.无论我如何尝试执行此操作,都会出现编译器错误.

So basically, the code executed should be different based on what type of variable is passed to the macro. No matter how I try to execute this, I get compiler errors.

我尝试过:

 cmp integer, DWORD
 cmp TYPE integer, DWORD 

我真的不知道从这里去哪里.我看过我能想到的所有参考文献,但这似乎并不常见

and I don't really know where to go from here. I've looked in every reference I can think of, but it doesn't seem to be a common thing

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运算符.引自 MASM参考:

In MASM there is the OPATTR operator. Quoted from the MASM reference:

返回定义表达方式和范围的单词.低字节与.TYPE返回的字节相同.高字节包含其他信息.

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源代码. >在MASM论坛上:

The values are as follows, taken from the MASM Basic source code referenced 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]

您的MACRO代码示例为

An example for your MACRO code would be

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 this MACRO code is not exactly what you expect, you can adjust the OPATTR value by combining the bit values.

要添加一件事来描述IF的两个变体之间的MASM差异:

One thing to add to describe the difference in MASM between the two variants of IFs:

IF   --- is compile time comparison  
.IF  --- is runtime comparison

这篇关于如何比较汇编中的变量类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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