我尝试使用scanf
输入四个浮点数,将它们存储到堆栈中,然后使用vmovupd
将它们复制到寄存器中以供使用。我的问题是当我尝试输出这4个数字时,程序在printf
处出现故障。
我认为它与堆栈有关,但我尝试过多次弹出(多次指示一次)无济于事。我对汇编编码还不熟悉所以使用gdb
对我来说有点太高级了。
您会注意到我添加了一个名为debug
的文件。它允许我查看寄存器和堆栈(这就是为什么有dumpstack
指令。)这是由我的教授提供的,它确实帮助了一些但显然不够(或者我可能是只是遗漏了一些东西)。
这里是.cpp
:
#include <iostream>
using namespace std;
extern "C" double ComputeElectricity();
int main()
{
cout << "Welcome to electric circuit processing by Chris Tarazi." << endl;
double returnValue = ComputeElectricity();
cout << "The driver received this number: " << returnValue << endl;
return 0;
}
这里是ASM
代码:
%include "debug.inc"
extern printf
extern scanf
global ComputeElectricity
;---------------------------------Declare variables-------------------------------------------
segment .data
greet db "This progam will help you analyze direct current circuits configured in parallel.", 10, 0
voltage db "Please enter the voltage of the entire circuit in volts: ", 0
first db "Enter the power consumption of device 1 (watts): ", 0
second db "Enter the power consumption of device 2 (watts): ", 0
third db "Enter the power consumption of device 3 (watts): ", 0
fourth db "Enter the power consumption of device 4 (watts): ", 0
thankyou db "Thank you. The computations have completed with the following results.", 10, 0
circuitV db "Curcuit total voltage: %1.18lf v", 10, 0
deviceNum db "Device number: 1 2 3 4", 10, 0
power db "Power (watts): %1.18lf %1.18lf %1.18lf %1.18lf", 10, 0
current db "Current (amps): %1.18lf %1.18lf %1.18lf %1.18lf", 10, 0
totalCurrent db "Total current in the circuit is %1.18lf amps.", 10, 0
totalPower db "Total power in the circuit is %1.18lf watts.", 10, 0
bye db "The analyzer program will now return total power to the driver.", 10, 0
string db "%s", 0
floatfmt db "%lf", 0
fourfloat db "%1.18lf %1.18lf %1.18lf %1.18lf", 0
;---------------------------------Begin segment of executable code------------------------------
segment .text
dumpstack 20, 10, 10
ComputeElectricity:
;dumpstack 30, 10, 10
;---------------------------------Output greet message------------------------------------------
mov qword rax, 0
mov rdi, string
mov rsi, greet
call printf
;---------------------------------Prompt for voltage--------------------------------------------
mov qword rax, 0
mov rdi, string
mov rsi, voltage
call printf
;---------------------------------Get voltage--------------------------------------------------
push qword 0
mov qword rax, 0
mov rdi, floatfmt
mov rsi, rsp
call scanf
vbroadcastsd ymm15, [rsp]
pop rax
;---------------------------------Prompt for watts 1--------------------------------------------
mov qword rax, 0
mov rdi, string
mov rsi, first
call printf
;---------------------------------Get watts 1---------------------------------------------------
push qword 0
mov qword rax, 0
mov rdi, floatfmt
mov rsi, rsp
call scanf
;---------------------------------Prompt for watts 2--------------------------------------------
mov qword rax, 0
mov rdi, string
mov rsi, second
call printf
;---------------------------------Get watts 2---------------------------------------------------
push qword 0
mov qword rax, 0
mov rdi, floatfmt
mov rsi, rsp
call scanf
;---------------------------------Prompt for watts 3--------------------------------------------
mov qword rax, 0
mov rdi, string
mov rsi, third
call printf
;---------------------------------Get watts 3---------------------------------------------------
push qword 0
mov qword rax, 0
mov rdi, floatfmt
mov rsi, rsp
call scanf
;---------------------------------Prompt for watts 4--------------------------------------------
mov qword rax, 0
mov rdi, string
mov rsi, fourth
call printf
;---------------------------------Get watts 4---------------------------------------------------
push qword 0
mov qword rax, 0
mov rdi, floatfmt
mov rsi, rsp
call scanf
;dumpstack 50, 10, 10
;---------------------------------Move data into correct registers------------------------------
vmovupd ymm14, [rsp] ; move all 4 numbers from the stack to ymm14
pop rax
pop rax
pop rax
pop rax
;dumpstack 55, 10, 10
vextractf128 xmm10, ymm14, 0 ; get lower half
vextractf128 xmm11, ymm14, 1 ; get upper half
;---------------------------------Move data into low xmm registers------------------------------
movsd xmm1, xmm11 ; move ymm[128-191] (3rd value) into xmm1
movhlps xmm0, xmm11 ; move from highest value from xmm11 to xmm0
movsd xmm3, xmm10
movhlps xmm2, xmm10
;showymmregisters 999
;---------------------------------Output results-------------------------------------------------
;dumpstack 60, 10, 10
mov rax, 4
mov rdi, fourfloat
push qword 0
call printf
pop rax
ret
答案 0 :(得分:5)
问题在于您的堆栈使用情况。
首先,ABI文档授权rsp
在 call
之前与进行16字节对齐。
由于call
将在堆栈上推送一个8字节的返回地址,因此需要将rsp
调整为16加8的倍数才能返回16字节对齐。 16 * n + 8
包含任何push
指令或对RSP的其他更改,而不仅仅是sub rsp, 24
。这是段错误的直接原因,因为printf
将使用对齐的SSE
指令,这些指令会对未对齐的地址造成错误。
如果你解决了这个问题,那么你的堆栈仍然是不平衡的,因为你继续推动值但从不弹出它们。很难理解你想用堆栈做什么。
通常的方法是在你的函数开头(序言)为本地人分配空间,并在结尾(结语)释放它。如上所述,此数量(包括任何推送)应该是16加8的倍数,因为函数条目上的RSP (在调用者call
之后)距离8个字节一个16字节的边界。
在glibc的大多数版本中,当{!= 0}时,printf
只关心16字节堆栈对齐。(因为这意味着有FP args,所以它将所有XMM寄存器转储到堆栈中,因此它可以为%f
次转化编制索引。)
如果您使用未对齐的堆栈调用它,即使它恰好在您的系统上运行,它仍然是一个错误;未来的glibc版本可以包含依赖于16字节堆栈对齐的代码,即使没有FP args也是如此。例如,scanf
已经在未对齐的堆栈上崩溃,即使在大多数GNU / Linux发行版上都使用AL=0
。