汇编,多个参数 -m32/linux(与 C 中的 stdarg 相同) [英] Assembly, multiple arguments -m32 / linux (same as stdarg in C)
问题描述
为了解决这个问题,我了解 C,而且我还是汇编的初学者,所以我在这里遇到了一个小问题.
To solve this, I understand C, and I'm still a beginner in Assembly so I'm kinda stuck with a little problem here.
我在获取多个参数时遇到了一些麻烦,如果我应该这样做,也许可以计算它们,并在我的汇编代码中使用格式参数.
I'm having some trouble with taking multiple arguments, maybe count them if I should do that, and use the format arguments in my assembly code.
尝试将一些字节添加到具有许多参数的字符串中.我知道如何将两个第一个参数放在堆栈上,但第一个参数之后的其他参数是格式(如 %s、%d、%c 等),第一个参数应该是变量 i想写信给.在 C 中,标准 main 具有参数计数器.我可能也想计算这里的论点!?如果是这样,我该怎么做?
Trying to add some bytes to a string with many arguments. I know how to put the two first arguments on the stack, but the other arguments after the first is the format (like %s, %d, %c etc) and the first argument is the one that is supposed to be the variable i want to write to. In C, standard main has argument-counter. I might want to count the arguments here aswell!? How can I do that, if that's how It's done?
.globl minisprintf
# Name: minisprintf
# Synopsis: A simplified sprintf
# C-signature: int minisprintf(unsigned char *res, unsigned char *format, ...);
# Registers: AL: for characters
# %ECX: first argument, res
# %EDX: second argument, args
#
minisprintf: # minisprintf
pushl %ebp # start of
movl %esp, %ebp # function
movl 8(%ebp), %ecx # first argument
movl 12(%ebp), %edx # second argument
# other arguments
# checking last byte of string res
推荐答案
可变参数函数是 C 的一项特性,因此最好查看 va_start
的开源实现,va_arg
和 va_end
用于您感兴趣的架构/ABI.
Variadic functions are a C feature, so you might be best served by checking out how an open-source implementation of va_start
, va_arg
, and va_end
for the architecture/ABI you're interested in.
对于类似 printf
的函数,您不需要显式的参数计数,因为该信息嵌入在格式字符串中 - 期望的可变参数的数量和类型由格式说明符的数量和详细信息.
You don't need an explicit argument count for a printf
-like function, because that information is embedded in the format string - the number and types of the variadic arguments to expect are given by the number and details of the format specifiers.
您将需要了解 ABI 的过程调用方面是非常重要的细节,以使所有这些正常工作.例如,浮点和整数参数是否进入同一个堆栈,或者是否有一些在寄存器中传递?您需要提升类型的大小以确保您的 va_arg
等效项始终在正确的时间为正确的类型获取正确的内容?等等……
You will need to understand the procedure-call aspects of your ABI is pretty serious detail to get all of this working correctly. For example, do floating point and integer arguments go to the same stack, or are some passed in registers? What size do you need to promote types to to make sure that your va_arg
equivalent always gets the right thing for the right type at the right time? And so on...
这篇关于汇编,多个参数 -m32/linux(与 C 中的 stdarg 相同)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!