0

我正在尝试在汇编中制作一些简单的代码,以便我可以轻松理解更多。首先,我想创建一个带有参数的函数。

我想为参数添加一个值,例如字母“A”(在 ascii 中的值为 65)。我希望函数返回 A,但由于 eax 包含 4 个字节,而这个字母只需要一个字节,我试图使用 EAX 寄存器的 AL 部分。

这里的问题是这个功能在我使用的时候不起作用。我得到四个看起来很奇怪的字符作为返回值。有谁知道为什么?

这就是我的代码的样子:

        .globl  letterprinter
# Name:             letterprinter
# Synopsis:         prints the character 'A' (or at least, it is supposed to)
# C-signature:      int letterprinter(unsigned char *res);
# Registers:        %EDX: for the argument that is supposed to get the value
#                   %EAX: for the return value(s)
#



letterprinter:                      # letterprinter

    pushl       %ebp                # start of
    movl        %esp, %ebp          # function

    movl        8(%ebp), %edx       # first argument

    movl        $65, %dl            # the value 'A' in lowest part of edx, dl

    movb        (%dl), %al          # moves it to the return register eax
    jmp     exit                    # ending function

exit:

    popl        %ebp                # popping - standard end of a function
                                    # 0-byte ? should there be one?
    ret                             # return
4

1 回答 1

0
                    .globl  char

# Name:         char
# Synopsis:         A simplified sprintf
# C-signature:      int sprinter(unsigned char *res, unsigned char);
# Registers:        %EDX: first argument
#               %EBX: second argument
#



char:                   # char

    pushl       %ebp            # start of
    movl        %esp, %ebp      # function

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


add_res:
    movb        %al, (%edx)     # eax low gets the val at edx
    movb        $65, (%edx)     # puts A to the string
    jmp     exit            # jump to exit  

exit:

    popl        %ebp            # popping standard end of function
    movb        $0,1(%edx)      # adds the zero-byte at end                     
    ret                 # return
于 2013-05-04T18:38:42.317 回答