Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Assembly 汇编、寄存器和返回值-m32/linux_Assembly_X86 - Fatal编程技术网

Assembly 汇编、寄存器和返回值-m32/linux

Assembly 汇编、寄存器和返回值-m32/linux,assembly,x86,Assembly,X86,我试图在汇编中编写一些简单的代码,这样我就可以更容易地理解。首先,我想做一个带参数的函数 我想给参数添加一个值,例如字母“a”(ascii值为65)。 我希望函数返回A,但是由于eax包含4个字节,而这个字母只需要一个字节,所以我尝试使用eax寄存器的AL部分 这里的问题是,当我使用它时,这个函数不起作用。我得到四个奇怪的字符作为返回值。有人知道为什么吗 我的代码是这样的: .globl letterprinter # Name: letterprint

我试图在汇编中编写一些简单的代码,这样我就可以更容易地理解。首先,我想做一个带参数的函数

我想给参数添加一个值,例如字母“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

“问题是……?对不起,@JensBjörnhager。我现在更新了帖子。你知道我做错了什么吗?首先我不明白你想做什么。取指向“I-not-know-where”的指针,改为写入值65,并使用位于地址65(%dl)的返回值退出。如果你想回答问题,就让打电话的人看看。我对大会很陌生,很抱歉。但我想我解决了。现在我们来解决这个问题,添加不止一个字符。不过,谢谢你看了这个:)@KimJonatanWesselBjørneset,很高兴知道你已经完成了:)也许这就是你最终使用的代码,但它实际上是如何回答你自己的问题的呢?事实上,我也不确定,但现在已经解决了。似乎我移动了(movl)而不是移动了(movb),因为char只有一个字节,但我不确定是否还有其他我做错的事情。我正在尝试解决这个问题,现在需要添加多个字符:)
                    .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