Friday, 10 May 2019

x86 - What does cltq do in assembly?

0x0000000000400553 :   mov    -0x4(%rbp),%eax

0x0000000000400556 : cltq
0x0000000000400558 : shl $0x3,%rax
0x000000000040055c : mov %rax,%rdx


In fact my programe is as simple as :



5   int main(int argc, char *argv[]) { 
6 int i = 0;
7 while(environ[i]) {

8 printf("%s\n", environ[i++]);
9 }
10 return 0;


But the assembly output is pretty long:



Dump of assembler code for function main:
0x0000000000400518 : push %rbp
0x0000000000400519 : mov %rsp,%rbp

0x000000000040051c : sub $0x20,%rsp
0x0000000000400520 : mov %edi,-0x14(%rbp)
0x0000000000400523 : mov %rsi,-0x20(%rbp)
0x0000000000400527 : movl $0x0,-0x4(%rbp)
0x000000000040052e : jmp 0x400553
0x0000000000400530 : mov -0x4(%rbp),%eax
0x0000000000400533 : cltq
0x0000000000400535 : shl $0x3,%rax
0x0000000000400539 : mov %rax,%rdx
0x000000000040053c : mov 0x2003e5(%rip),%rax # 0x600928

0x0000000000400543 : lea (%rdx,%rax,1),%rax
0x0000000000400547 : mov (%rax),%rdi
0x000000000040054a : addl $0x1,-0x4(%rbp)
0x000000000040054e : callq 0x400418
0x0000000000400553 : mov -0x4(%rbp),%eax
0x0000000000400556 : cltq
0x0000000000400558 : shl $0x3,%rax
0x000000000040055c : mov %rax,%rdx
0x000000000040055f : mov 0x2003c2(%rip),%rax # 0x600928
0x0000000000400566 : lea (%rdx,%rax,1),%rax

0x000000000040056a : mov (%rax),%rax
0x000000000040056d : test %rax,%rax
0x0000000000400570 : jne 0x400530
0x0000000000400572 : mov $0x0,%eax
0x0000000000400577 : leaveq
0x0000000000400578 : retq
End of assembler dump.


What I don't understand is this block:




0x000000000040052e :   jmp    0x400553 
0x0000000000400530 : mov -0x4(%rbp),%eax
0x0000000000400533 : cltq
0x0000000000400535 : shl $0x3,%rax
0x0000000000400539 : mov %rax,%rdx
0x000000000040053c : mov 0x2003e5(%rip),%rax # 0x600928
0x0000000000400543 : lea (%rdx,%rax,1),%rax
0x0000000000400547 : mov (%rax),%rdi
0x000000000040054a : addl $0x1,-0x4(%rbp)

0x000000000040054e : callq 0x400418
0x0000000000400553 : mov -0x4(%rbp),%eax
0x0000000000400556 : cltq
0x0000000000400558 : shl $0x3,%rax
0x000000000040055c : mov %rax,%rdx
0x000000000040055f : mov 0x2003c2(%rip),%rax # 0x600928
0x0000000000400566 : lea (%rdx,%rax,1),%rax
0x000000000040056a : mov (%rax),%rax
0x000000000040056d : test %rax,%rax
0x0000000000400570 : jne 0x400530

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...