问题描述
为了解决这个问题,我懂 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 相同)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!