在下面的C++源代码的汇编中.为什么RAX被推入堆栈?
正如我从ABI理解的那样,RAX可以包含来自调用函数的任何内容.但是我们将它保存在这里,然后将堆栈移回8个字节.所以堆栈上的RAX,我认为只与std::__throw_bad_function_call()操作相关......?
代码:-
#include <functional>
void f(std::function<void()> a)
{
a();
}
Run Code Online (Sandbox Code Playgroud)
gcc.godbolt.org使用Clang 3.7.1 -O3 输出:
f(std::function<void ()>): # @f(std::function<void ()>)
push rax
cmp qword ptr [rdi + 16], 0
je .LBB0_1
add rsp, 8
jmp qword ptr [rdi + 24] # TAILCALL
.LBB0_1:
call std::__throw_bad_function_call()
Run Code Online (Sandbox Code Playgroud)
我确定原因很明显,但我很难弄清楚.
这是一个没有std::function<void()>包装器的尾部调用,用于比较:
void g(void(*a)())
{
a();
}
Run Code Online (Sandbox Code Playgroud)
琐碎的:
g(void (*)()): # @g(void (*)())
jmp rdi # TAILCALL
Run Code Online (Sandbox Code Playgroud) 我正在阅读http://www.realworldtech.com/sandy-bridge/,我在理解一些问题时面临一些问题:
专用堆栈指针跟踪器也存在于Sandy Bridge中并重命名堆栈指针,消除了串行依赖性并删除了多个uop.
什么是dedicated stack pointer tracker实际?
对于Sandy Bridge(和P4),英特尔仍然使用术语ROB.但重要的是要理解,在这种情况下,它只引用了飞行中uops的状态数组
事实上它意味着什么?请说清楚.
我正在尝试在x86_64程序集中打印浮点数,但它只是将值打印为零.
关于这个问题已经存在一些问题.通过确保设置在%al中使用的向量寄存器的数量,似乎可以解决一个问题.另一个表明你需要一个16字节的堆栈对齐.但是,我正在做这两件事而仍然没有得到正确的输出.
这是我的计划:
# prints a floating point value
.section .rodata
.fmt: .string "num: %f\n"
.num: .float 123.4
.section .text
.global main
.type main, @function
main:
subq $8, %rsp # 16-byte alignment
# print my number
movss .num, %xmm0 # load float value
movq $.fmt, %rdi # load format string
movb $1, %al # use 1 vector register
call printf
# exit
addq $8, %rsp # undo alignment
movq $0, %rax # return 0
ret
Run Code Online (Sandbox Code Playgroud) 编译以下代码时:
global main
extern printf, scanf
section .data
msg: db "Enter a number: ",10,0
format:db "%d",0
section .bss
number resb 4
section .text
main:
mov rdi, msg
mov al, 0
call printf
mov rsi, number
mov rdi, format
mov al, 0
call scanf
mov rdi,format
mov rsi,[number]
inc rsi
mov rax,0
call printf
ret
Run Code Online (Sandbox Code Playgroud)
使用:
nasm -f elf64 example.asm -o example.o
gcc -no-pie -m64 example.o -o example
Run Code Online (Sandbox Code Playgroud)
然后运行
./example
Run Code Online (Sandbox Code Playgroud)
它运行,打印:输入数字: 但随后崩溃并打印: 分段错误(核心已转储)
因此,printf可以正常工作,而scanf则不能。我对scanf有什么错呢?
我想用printf打印浮点值
global main
extern printf
section .data
string: db `%f\n`, 0
section .bss
rs: resq 1
[...]
movq xmm0, [rs]
mov rdi, string
mov rax, 0
call printf
Run Code Online (Sandbox Code Playgroud)
rs包含浮点值1.6
(gdb) x/fg &rs
0x600ad8 <rs>: 1.6000000000000001
Run Code Online (Sandbox Code Playgroud)
但程序打印
[username@localhost folder]$ ./programname
0.000000
Run Code Online (Sandbox Code Playgroud)
谁能让程序打印1.6?我究竟做错了什么?