1

我对汇编编程很陌生,我用 C 编写了一个函数,需要在汇编中调用另一个函数。似乎寄存器想要返回四个字符(字节)而不是一个,这就是我想要的。

跳转后忽略代码,因为我跳转只是为了跳过这部分代码,直到我使它正常工作。

这实际上应该是我自己的sprintfC 语言简化版本的一部分。我删除了一些代码只是为了让事情正常工作。它应该返回带有%. 所以,当我在 C 中调用这个汇编函数时,我可以编写printf("%s", res);(或%c在本例中)并打印%

.globl printpercent

# Name:        printpercent
# Synopsis:    A simplified sprintf
# C-signature: int printpercent(unsigned char *res, unsigned char *format, ...);
# Registers:   %eax: first argument
#              %ebx: second argument

printpercent:                       # sprinter
    pushl       %ebp        # start of
    movl        %esp, %ebp  # function

    movl        8(%ebp), %eax   # first argument
    movl        12(%ebp), %ebx  # second argument

loop:
    movb        $37, %bl        # lowest bits to %
    movb        %bl, %al
    jmp         exit
    movb        (%ebx), %dl     # 
    cmp         $0, %dl         # Check if 0

    je              exit        # if 0 -> exit

    cmp     $37, %dl            # Check '%' 


    movb        %dl, (%eax)     # if it doesnt equal any above/or default
                                # add to register %eax

    jmp     loop                # jump back to the start of the loop


exit:
    popl        %ebp            # popping standard end of function
                                # 0-byte ?
    ret                         # return
4

2 回答 2

1

您的函数返回 int 所以当然编译将 alayws 将完整的寄存器作为返回值。毕竟 int == 4 字节在您的环境中。您必须清除 EAX 以确保其中没有随机值。

于 2013-05-05T08:02:00.060 回答
1

您可以使用 xor 轻松清除寄存器,以便在再次使用之前清除寄存器:

xor %eax, %eax

于 2015-09-26T10:18:43.710 回答