Widening C arguments without trusting excess register bits

Two assembly functions implement these C declarations on a 64-bit Linux target:


C
long widen_signed(int x);
long widen_unsigned(unsigned int x);


Each function must return the mathematical value of x as a signed long. On this target, int and unsigned int are 32 bits, long is 64 bits, and signed integers use two's complement representation.


Under the System V AMD64 ABI, the incoming argument occupies edi, the low 32 bits of rdi; the upper 32 bits of the incoming register are unspecified. Each function returns its 64-bit result in rax.


Complete each row. Enter sign or zero for the extension needed from 32 to 64 bits, and enter the returned value as an exact decimal integer. The hexadecimal patterns describe only the incoming low 32 bits.

FunctionIncoming low 32 bitsExtensionReturned long
widen_signed0xFFFFFFF6
widen_unsigned0xFFFFFFF6
widen_signed0x80000021
widen_unsigned0x80000021

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Assembler with C Questions: