計算機組織與結構›Ch1 指令集架構(ISA)第 2 題/共 39 題
2. ISA、Addressing Mode、Disassembly、x86-64
#CC-01-002難ISAAddressing ModeDisassemblyx86-64
假設有下列 C 語言片段的原始碼,以及該程式碼在 x86-64 平台上編譯後物件檔的反組譯(disassembly)結果。假設在 func(i, up) 中:i 存於 %rdi,up 存於 %rsi。請回答 CN 的值以及 struct np_struct 的完整宣告。
HINT:對於指令 lea (a, b, num), c,其運算結果 (b*num + a) 會被搬移到 c。
反組譯結果:
mov 0x120(%rsi), %ecx
add (%rsi), %ecx
lea (%rdi,%rdi,4), %rax
lea (%rsi,%rax,8), %rax
mov 0x8(%rax), %rdx
movslq %ecx, %rcx
mov %rcx, 0x10(%rax,%rdx,8)
retq
對應的 C 程式碼:
extern struct np_struct;
typedef struct {
int fst;
nt_struct a[CN];
int lst;
} u_struct;
void func(long i, u_struct *up) {
int n = up->fst + up->lst;
np_struct *np = &up->a[i];
np->x[np->idx] = n;
}
參考答案與解析
答案 (D)。
mov 0x120(%rsi),%ecx讀up->lst,add (%rsi),%ecx加上up->fst:fst在位移 0,lst在位移 0x120 = 288。- 兩個
lea算出%rax = up + 40i。 mov 0x8(%rax),%rdx讀np->idx,所以&up->a[i] = up + 8 + 40i:陣列a從位移 8 開始(int fst之後補齊到 8 bytes),每個元素 40 bytes;idx用 64 位元暫存器讀取,是long。mov %rcx,0x10(%rax,%rdx,8)寫入up + 40i + 16 + 8·idx:x的元素是 8 bytes(scale 為 8),是long。lst緊接在陣列後面:,得 。
所以 nt_struct 大小是 40 bytes、CN = 7、x 是 long 陣列。四個選項中只有 (D) 同時符合:(A)(C) 的 CN 是 8;(B) 的 x 是 int,整個結構只有 32 bytes;(D) {long idx, long idx1, long x[3]} 是 40 bytes。
(照組合語言逐位元組推算,x 從 np 的位移 8 開始,精確的宣告是 {long idx; long x[4];};選項沒有這個寫法,(D) 是唯一大小與 CN 都正確的選項。)
📄 台大114
▤完整推導請見《WH 資工筆記 · 計算機組織與結構》Ch1 指令集架構(ISA)