I have a function that is called by main. Assume that function’s name is funct1. funct1 calls another function named read_input.
Now assume that funct1 starts as follows:
push %rbp
push %rbx
sub $0x28, %rsp
mov $rsp, %rsi
callq 4014f0 read_input
cmpl $0x0, (%rsp)
jne (some terminating function)
So just a few of questions:
- In this case, does
read_inputonly have one argument, which is
%rbx? - Furthermore, if the stack pointer is being decreased by
0x28, this means a string of size0x28is getting pushed onto the
stack? (I know it’s a string). - And what is the significance of
mov %rsp, %rsibefore calling a function? - And lastly, when
read_inputreturns, where is the return value put?
Thank you and sorry for the questions but I am just starting to learn x86!
It looks like your code is using the Linux/AMD ABI. I’ll answer your questions in that context.
rbxis a callee-saved (nonvolatile) register. Your function is saving it so that it doesn’t disturb the caller’s value. It’s not being restored in the code you’ve shown, but that’s because you haven’t shown the whole function. If there’s more to this function, and I think there is, it’s becauserbxis being used somewhere later on in this routine.read_inputis taking a string as a parameter, your description is reasonable. It’s not necessarily accurate, however. Some of that data might be used for other local variables aside from just the buffer being allocated to pass toread_input.rsi.rsiis the second parameter register for the AMD x64 calling convention. That means you’re going to be callingread_inputwith whatever the first parameter passed to this function is, along with a pointer to your new stack buffer.rax, if it’s a 64-bit value or smaller, inrax&rdxif it’s larger. Or if it’s floating point, inxmm0,ymm0, orst(0). You probably should look at a description of your calling convention to get a handle on this stuff – there’s a great PDF file at this link. Check out Table 4.