[英]Assembly, registers and return values -m32 / linux
I'm trying to make some simple codes in assembly so I easily can understand more. 我试图在汇编中编写一些简单的代码,以使我容易理解。 To start with I want to make a function that takes a parameter. 首先,我要创建一个带有参数的函数。
I want to add a value to the parameter, for example the letter 'A', (has value 65 in ascii). 我想为参数添加一个值,例如字母“ A”(ASCII中的值为65)。 I want the function to return the A, but since eax holds 4 bytes and this letter only needs ONE byte, I'm trying to use the AL part of the EAX register. 我希望函数返回A,但是由于eax保留4个字节,而该字母仅需要一个字节,因此我尝试使用EAX寄存器的AL部分。
The problem here is that this function doesn't work when I use it. 这里的问题是,当我使用它时此功能不起作用。 I get four strange-looking characters as a return value. 我得到了四个看起来很奇怪的字符作为返回值。 Anyone knows why? 有人知道为什么吗?
this is how my code looks like: 这是我的代码的样子:
.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
.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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.