Re: Question about AMD64 ABI

From: Daniel Cervus <DanieltheDeer_at_outlook.com>
Date: Sun, 13 Nov 2022 09:07:53 UTC
Hi Paul,

  You mean I need to ensure higher bits are zeros (including signed int, short, char...) when passing the parameters? Okay, I get it.
  By the way, is there any good resources that I can learn 64-bit FB (or *nix) assembly programming from?

Regards,
Daniel

在 2022年11月13日,下午4:52,Paul Procacci <pprocacci@gmail.com> 写道:




On Sat, Nov 12, 2022 at 10:31 PM Daniel Cervus <DanieltheDeer@outlook.com<mailto:DanieltheDeer@outlook.com>> wrote:
Hi everyone,

I’m trying to do assembly programming on FB in 64-bit mode. I have a question, 64-bit mode requires parameters to be passed on 64-bit registers. But when a parameter is 32-bit or smaller, do I need to sign-extend (or zero-extend) them to 64-bit? The System V ABI specifications only says "The size of each argument gets rounded up to eightbytes." It’s somewhat ambiguous. How to round up 'float', when they are passed on stack?

Thanks,
Daniel

(Didn't Reply all)

Hi Daniel,

There are a handful of operations that operate on 32bit registers that automatically clear the high bits for you.

32-bit operands generate a 32-bit result, zero-extended to a 64-bit result in the destination general-purpose register.
16 and 8 bit operands don't have this "built-in" so you would indeed need to ensure the higher bits are cleared.

mov dword edi, 1  is effectively setting rdi to the value of 0x0000000000000001

As for sign extending the values, the answer is `no'.....under most circumstances.  If you are sticking to widths of 32 and 64 bits then you are fine.
The moment you mess with 16bits or smaller, then yes, you need to ensure no garbage lives in your higher bits because the cpu doesn't clear this for you.

"The size of each argument gets rounded up to eight bytes."

The size of ALL arguments passed to the callee via general purpose registers is 8 bytes "regardless of what a function def says".  It's HOW the callee operates upon the register arguments that matters.

As for passing arguments on the stack ...  you shouldn't have to.  Not only are there the GPR's rdi, rsi, rdx, rcx, r8, r9, r10 at your disposal for int/scalar types there are also xmm0-xmm7 for your floats.

Thanks,
~Paul

--
__________________

:(){ :|:& };: