0

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.

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
4

2 回答 2

1

可变参数函数是 C 功能,因此最好通过查看 、 和 的开源实现以及您感兴趣的架构/ABI 来为您提供va_start最佳va_arg服务va_end

对于类似函数,您不需要显式的参数计数printf,因为该信息嵌入在格式字符串中 - 期望的可变参数的数量和类型由格式说明符的数量和详细信息给出。

需要了解 ABI 的过程调用方面是非常重要的细节,才能使所有这些正常工作。例如,浮点和整数参数是否进入同一个堆栈,或者是否有一些在寄存器中传递?你需要什么规模来提升类型以确保你的va_arg等价物总是在正确的时间为正确的类型获得正确的东西?等等...

于 2013-05-05T14:30:58.043 回答
1

我会这样做的方式如下:

您已经知道前两个论点。下一个参数将在 16(%ebp) 上,所以我会将这个地址放入寄存器并将其用作基地址。现在我解析我的字符串,因为这给了我需要多少寄存器的信息。对于遇到的每个参数,从我的基地址获取值并将其增加四,因为下一个参数将在那里。

我想没有真正需要同时注册所有参数,因为您可能会按顺序扫描格式。

于 2013-05-05T18:28:31.933 回答