Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between retq and ret?

Tags:

Let's consider the following program, which computes an unsigned square of the argument:

.global foo .text foo:     mov %rdi, %rax     mul %rdi     ret 

This is properly compiled by as, but disassembles to

0000000000000000 <foo>:    0:   48 89 f8                mov    %rdi,%rax    3:   48 f7 e7                mul    %rdi    6:   c3                      retq    

Is there any difference between ret and retq?

like image 259
marmistrz Avatar asked Mar 07 '17 16:03

marmistrz


People also ask

What does RETQ do in assembly?

The retq instruction pops the return address from the stack into the destination %rip , thus resuming at the saved return address.

What does RETQ do to RSP?

After the pop command, now %rsp points to a new address and retq takes this address as return address.

What is CLTQ x86?

cltq is the AT&T mnemonic for CDQE, which sign-extends EAX into RAX. It's a short-form of movslq %eax, %rax , saving code bytes. It exists because of how x86-64 evolved from 8086 to 386 to AMD64. It copies the sign bit of EAX to all the upper bits of the wider register, because that's how 2's complement works.

What is the RAX register used for?

The RAX register is used for return values in functions regardless of whether you're working with Objective-C or Swift.


1 Answers

In long (64-bit) mode, you return (ret) by popping a quadword address from the stack to %rip.

In 32-bit mode, you return (ret) by popping a dword address from the stack to %eip.

Some tools like objdump -d call the first one retq. It's just a name, the instruction encoding is the same either way (C3).

like image 54
ephemient Avatar answered Sep 29 '22 13:09

ephemient