简体   繁体   English

GNU Assembler GAS 检查宏参数是否已注册

[英]GNU Assembler GAS check if macro paramater is register

I want handle in case if macro argument (parameter) is a register.如果宏参数(参数)是一个寄存器,我想要处理。

I expect I have similiar code like this but I'm sure it doesn't work due to lack my knowledge of directive syntax.我希望我有类似这样的代码,但我确信由于缺乏对指令语法的了解,它不起作用。

.macro myMacro myParameter
  .if \myParamater == "register"
    //a block code in case if paramater is register
    mov rax, 1
  .else
    //a block code in case if paramater is not register (maybe immadiately value)
    mov rax, 2
  .endif
.endm

So based on that code I can use macro like this因此,基于该代码,我可以像这样使用宏

myMacro rdi //rax will be 1 because parameter is a register
myMacro 2 //rax will be 2 because parameter is not a register (immadiately value)

EDIT: So what is correct .if condition syntax to check if macro parameter is a register or not?编辑:那么检查宏参数是否为寄存器的正确.if条件语法是什么? Because I'm not sure in .if condition syntax.因为我不确定.if条件语法。

You maybe want check another else (not only register) for example check if it's a memory that store floating point or not.您可能想检查另一个其他(不仅是寄存器),例如检查它是否是存储浮点的内存。

First, you need define your own data type identifier.首先,您需要定义自己的数据类型标识符。

You can set whatever number ID you want as long they are different each other.您可以设置任何您想要的数字 ID,只要它们彼此不同即可。

// Define Identifier (ID) for your data type
.set REGISTER_TYPE_ID, 1
.set INTEGER_TYPE_ID, 2
.set FLOAT_TYPE_ID, 3
.set STRING_TYPE_ID, 4
.set FUNCTION_TYPE_ID, 5

Second, define every possible registers name with prefix TypeOf.其次,使用前缀TypeOf. to the REGISTER_TYPE_ID .REGISTER_TYPE_ID

And again you can change prefix to whatever you want as long consistent to the macro.再次,您可以将前缀更改为您想要的任何内容,只要与宏保持一致即可。

// Define TypeOf for every possible registers in x64 machine
.set TypeOf.rax, REGISTER_TYPE_ID
.set TypeOf.rbx, REGISTER_TYPE_ID
.set TypeOf.rcx, REGISTER_TYPE_ID
// and so on...

Third, define your macro.第三,定义你的宏。

If you just want debug macro or assembler you don't have to use machine instruction like mov rax, 1 , instead use .print "put string here"如果您只想调试宏或汇编程序,则不必使用mov rax, 1之类的机器指令,而是使用.print "put string here"

.macro myMacro myParam
  .ifnotdef "TypeOf.\myParam"
    .print "This parameter type is an IMMEDIATELY value (constant)!"
  .elseif "TypeOf.\myParam" == REGISTER_TYPE_ID
    .print "This parameter type is a REGISTER!"
  .elseif "TypeOf.\myParam" == FLOAT_TYPE_ID
    .print "This parameter type is a FLOAT!"
  .else
    .print "Unknown type ID \myParam (elseif not implemented)"
  .endif
.endm

Example Usage示例用法

In this case there are another type to check for example float .在这种情况下,还有另一种类型要检查,例如float

// Define Identifier (ID) for your data type
.set REGISTER_TYPE_ID, 1
.set INTEGER_TYPE_ID, 2
.set FLOAT_TYPE_ID, 3
.set STRING_TYPE_ID, 4
.set FUNCTION_TYPE_ID, 5

// Define TypeOf for every possible registers in x64 machine
.set TypeOf.rax, REGISTER_TYPE_ID
.set TypeOf.rbx, REGISTER_TYPE_ID
.set TypeOf.rcx, REGISTER_TYPE_ID
// and so on...

.macro myMacro myParam
  .ifnotdef "TypeOf.\myParam"
    .print "This parameter type is an IMMEDIATELY value (constant)!"
  .elseif "TypeOf.\myParam" == REGISTER_TYPE_ID
    .print "This parameter type is a REGISTER!"
  .elseif "TypeOf.\myParam" == FLOAT_TYPE_ID
    .print "This parameter type is a FLOAT!"
  .else
    .print "Unknown type ID \myParam (elseif not implemeted)"
  .endif
.endm

.data
  myVar: 
    .set TypeOf.myVar, FLOAT_TYPE_ID
    .float 1234

.text 
.global _start

myFunc:
  .set TypeOf.myFunc, FUNCTION_TYPE_ID
  mov rax, 123
  ret

_start:
  myMacro 453 //"This parameter type is an IMMEDIATELY value (constant)!"
  myMacro rax //"This parameter type is a REGISTER!"
  myMacro myVar //"This parameter type is a FLOAT!"
  myMacro myFunc "Unknown type ID myFunc (elseif not implemeted)"

_exit:
  mov rax, 60 //linux sys_exit
  mov rdi, 0
  syscall

OUTPUT:输出:

wawan@LAPTOP-DJH6CVKH:~/asmi-fw$ as utama.s -o utama.o -msyntax=intel -mnaked-reg
This parameter type is an IMMEDIATELY value (constant)!
This parameter type is a REGISTER!
This parameter type is an FLOAT!
Unknown type ID myFunc (elseif not implemeted)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM