A C program passes an array of three records to an assembly function on a 64-bit Linux target using the System V AMD64 ABI.
struct Record { unsigned char kind; int count; long total; unsigned char valid; }; long read_total(const struct Record *records, unsigned long index);
The function is intended to return records[index].total. An object's alignment is the byte-address multiple required at its start. Use these target properties:
C type
Size in bytes
Alignment in bytes
unsigned char
1
int
4
long
8
Members remain in declaration order, and each begins at the lowest available offset satisfying its alignment. The structure's alignment is its largest member alignment, and its size is rounded up to a multiple of that alignment. No packing options are enabled.
The array begins at decimal byte address 8192. A debugger snapshot immediately before the call shows that records[1].valid is 1, every padding byte in records[1] is zero, and records[2].total is 900. No memory changes during the call. Memory is little-endian.
At function entry, rdi contains 8192 and rsi contains 2. The implementation below uses Intel syntax; QWORD PTR requests an 8-byte load, and the returned signed long is in rax.
mov rax, rsi shl rax, 4 mov rax, QWORD PTR [rdi + rax + 8] ret
Find the actual size of one C array element in bytes, the correct decimal byte address of records[2].total, and the signed integer actually returned by this implementation. Enter all three answers as exact decimal integers, with no unit suffixes.
Unlock full course notes by signing up
Finding arguments after the prologue
An x86-64 Linux assembly function uses the System V AMD64 convention and receives eight long arguments named a1 through a8, in that order. Each long and return address occupies 8 bytes. At entry, a7 and a8 occupy consecutive stack slots immediately above the return address.
The function executes this prologue without changing any argument registers:
Complete the table for the instant after these instructions. For register locations, enter the lowercase register name. For stack locations, enter the exact integer byte displacement from the current RSP, so a displacement of 32 means [rsp + 32].
Protecting values across a helper call
An x86-64 Linux caller using the System V AMD64 convention holds a needed counter value of 19 in R10 and a needed index value of 31 in RBX. It will use both values after calling helper. The helper follows the convention but may otherwise use any permitted registers. The caller already has a dedicated 8-byte local stack slot in its allocated frame and correct call-site alignment.
Which actions correctly protect the values they address? Select all that apply. Treat each proposed action separately, without assuming any other register save.
Widening C arguments without trusting excess register bits
Two assembly functions implement these C declarations on a 64-bit Linux target:
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.
Accounting for recursive call frames
An x86-64 Linux assembly implementation uses the System V AMD64 convention to reproduce this C function for nonnegative inputs:
Every invocation, including the base case, pushes one 8-byte copy of its incoming RBX, then stores its own n in RBX. A non-base invocation calls total(n - 1), adds its saved n to the returned RAX, restores RBX, and returns. The base case returns zero after restoring RBX.
There are no other pushes, stack allocations, or calls. Every call adds an 8-byte return address; each frame's single register push provides the required alignment for its recursive call. The outer call is total(6), and the implementation performs all recursive calls without optimization.