[英]Do I need to use brackets when calling a procedure in Assembly?
I am just starting to learn Assembly (x86 NASM) and I am currently going over function calls.我刚刚开始学习汇编(x86 NASM),目前正在学习函数调用。 Wherever I looked on the internet I saw everyone calling functions like this:无论我在互联网上的什么地方,我都看到每个人都像这样调用函数:
call power
Where power
is the label where the function starts.其中power
是功能开始的标签。 But what I am trying to see is how to print something in Assembly, and interestingly enough, calling a function like in the above case doesn't seem to work.但是我想看到的是如何在Assembly 中打印一些东西,有趣的是,在上面的例子中调用一个函数似乎不起作用。 We'll use the printf
function from C. Say I already used extern printf
and import printf msvcrt.dll
in my program (so I can actually use printf
), also say I already defined a symbol in my data segment msg db "Hello World", 0
and now I am trying to print this message.我们将使用 C 中的printf
函数。假设我已经在我的程序中使用了extern printf
和import printf msvcrt.dll
(所以我实际上可以使用printf
),还说我已经在我的数据段中定义了一个符号msg db "Hello World", 0
现在我正在尝试打印此消息。 If I do this:如果我这样做:
push dword msg
call printf
Nothing happens, it doesn't work.什么都没有发生,它不起作用。 I have no idea why.我不知道为什么。 However, if I do this:但是,如果我这样做:
push dword msg
call [printf]
The message is printed just as expected.该消息按预期打印。
This doesn't make much sense to me after all the articles that I read used just the label, without brackets.这对我来说没有多大意义,因为我阅读的所有文章都只使用标签,没有括号。 It also made a lot of sense to me when using just the label as we're using the call
instruction to perform a jump to that label, so we needed the address of the label.当我们使用call
指令执行跳转到该标签时,仅使用标签对我来说也很有意义,所以我们需要标签的地址。 But here it doesn't make sense at all to me why we're using the brackets and what exactly happens.但在这里,我完全不明白为什么我们要使用括号以及究竟发生了什么。 I mean, what is [printf]
and what would [power]
be, for the example I presented at the start of my question.我的意思是,什么是[printf]
以及[power]
是什么,例如我在问题开始时提出的示例。 However, despite my confusion, this is what works and the method I initially used doesn't work.然而,尽管我很困惑,但这是有效的,我最初使用的方法不起作用。
Can you please tell me exactly what is going on?你能告诉我具体是怎么回事吗? (PS: I am using Olly Debugger if that makes any difference) (PS:如果有任何区别,我正在使用 Olly Debugger)
It depends on what is "printf" in your assembly.这取决于程序集中的“printf”是什么。 If it is a function pointer (aka, the address of some function is stored at the address named "printf"), then you need brackets [].如果它是一个函数指针(也就是某个函数的地址存储在名为“printf”的地址中),那么你需要括号 []。 If "printf" is a function, that is, if the machine code is stored at the address that your assembler calls "printf", then you must not put brackets (or else you will probably end up with a segmentation fault, as the first 32 of 64 bits of machine code of "printf" probably don't accidentally contain an address of an executable code).如果“printf”是一个函数,也就是说,如果机器码存储在你的汇编程序调用“printf”的地址,那么你一定不要加括号(否则你可能会以分段错误告终,作为第一个“printf”的 64 位机器代码中的 32 位可能不会意外地包含可执行代码的地址)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.