This is the rust code I've written in the Compiler Explorer:
fn foo(a: u8, b: u8) -> u8 {
let sum: u8 = a + b;
sum
}
fn main() {
let x = 4;
let y = 6;
let _t = foo(x, y);
}
I can't understand the assembly code generated for this piece of code. For instance, the assembly for the foo
function is this:
example::foo:
push rax
mov cl,sil
mov al,dil
add al,cl
mov BYTE PTR [rsp+0x7],al
setb al
test al,0x1
jne 7ada <example::foo+0x1a>
mov al,BYTE PTR [rsp+0x7]
pop rcx
ret
From my background in assembly and C, at the beginning of a function call, it should push
the base pointer on the stack, and then mov
the stack pointer to the base pointer. I expected it to be started with some thing like this:
push rbp
mov rbp,rsp
But it pushes the rax
which I don't understand why. What is the difference between assembly code generated by the Rust compiler with assembly code generated by gcc
or llvm
for C
or C++
?
Is there any resource to learn more about assembly code generated by the Rust compiler?
This is link of my snippet on compiler explorer.