跳轉到內容

x86 反彙編/浮點示例

來自華夏公益教科書

示例:浮點運算

[編輯 | 編輯原始碼]

以下是 C 原始碼,以及一個簡單 C 語言函式的 GCC 彙編列表,該函式執行簡單的浮點運算。你能確定 LC5 和 LC6 的數值嗎?

 __fastcall double MyFunction2(double x, double y, float z)
 {
 	return (x + 1.0) * (y + 2.0) * (z + 3.0);
 }
 	.align 8
 LC5:
 	.long	0
 	.long	1073741824
 	.align 8
 LC6:
 	.long	0
 	.long	1074266112
 .globl @MyFunction2@20
 	.def	@MyFunction2@20;	.scl	2;	.type	32;	.endef
 @MyFunction2@20:
 	pushl	%ebp
 	movl	%esp, %ebp
 	subl	$16, %esp
 	fldl	8(%ebp)
 	fstpl	-8(%ebp)
 	fldl	16(%ebp)
 	fstpl	-16(%ebp)
 	fldl	-8(%ebp)
 	fld1
 	faddp	%st, %st(1)
 	fldl	-16(%ebp)
 	fldl	LC5
 	faddp	%st, %st(1)
 	fmulp	%st, %st(1)
 	flds	24(%ebp)
 	fldl	LC6
 	faddp	%st, %st(1)
 	fmulp	%st, %st(1)
 	leave
 	ret	$20

為此,我們甚至不需要浮點數計算器,儘管如果你願意(並且如果你能找到一個好的計算器),你可以隨意使用它。LC5 被加到 [ebp - 16],我們知道它是 y,LC6 被加到 [ebp - 24],我們知道它是 z。因此,LC5 是數字“2.0”,LC6 是數字“3.0”。請注意,fld1 指令會自動將浮點堆疊的頂部載入為常數值“1.0”。

華夏公益教科書