Tracing an assembly load through C structure padding

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.


C
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

1

int

4

4

long

8

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.


Three consecutive C array elements begin at byte address 8192 and extend toward increasing addresses.


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.


asm
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.

Your answers

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: