Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trying to understand the assembly instruction: cltd on x86

Tags:

x86

assembly

att

I found the assembly instruction

cltd

by disassembling code on an Intel architecture. The description I found was, that it clears the %edx register, but I don't understand what happens.... can anyone explain what the command exactly does?

like image 341
Muten Roshi Avatar asked Jun 18 '13 13:06

Muten Roshi


People also ask

Why is x86 assembly language programming so messy?

The topic of x86 assembly language programming is messy because: There are many different assemblers out there: MASM, NASM, gas, as86, TASM, a86, Terse, etc. All use radically different assembly languages.

What is the best assembly language for writing x86 machine code?

There are several different assembly languages for generating x86 machine code. The one we will use in CS216 is the Microsoft Macro Assembler (MASM) assembler. MASM uses the standard Intel syntax for writing x86 assembly code.

How complex is the x86 instruction set?

The full x86 instruction set is large and complex (Intel's x86 instruction set manuals comprise over 2900 pages), and we do not cover it all in this guide. For example, there is a 16-bit subset of the x86 instruction set. Using the 16-bit programming model can be quite complex.

How difficult is it to write x86 assembly code in MASM?

MASM uses the standard Intel syntax for writing x86 assembly code. The full x86 instruction set is large and complex (Intel's x86 instruction set manuals comprise over 2900 pages), and we do not cover it all in this guide. For example, there is a 16-bit subset of the x86 instruction set. Using the 16-bit programming model can be quite complex.


2 Answers

cltd is an alias for cdq (reference), which sign-extends eax into edx:eax.

What this means in practice is that edx is filled with the most significant bit of eax (the sign bit). For example, if eax is 0x7F000000 edx would become 0x00000000 after cdq. And if eax is 0x80000000 edx would become 0xFFFFFFFF.

like image 126
Michael Avatar answered Dec 08 '22 08:12

Michael


cltd converts signed long to signed double long

If you want to see a diagram of what happens, jump to page 160 of http://download.intel.com/products/processor/manual/325462.pdf (more details on page 681)

like image 39
SheetJS Avatar answered Dec 08 '22 08:12

SheetJS