Skip to main content
added 69 characters in body
Source Link
pudi
  • 180
  • 1
  • 2
  • 13

These two QWORDS are notThese two QWORDS are not arguments but local variables. You can recognize this, because of the negative offset (variables are above the base pointer and the stack is growing towards lower addresses). x64 assembler gives the first arguments but local variables. You can recognize thisin registers, because of the negative offset (variables are above the base pointer andso the stackfirst line is growing towards lower addresses)a function argument. The second one is a local variable. Since they are QWORDS, the datatype has a length of 8 byte. The first instruction assigns the value of rdi to a local variable (say var_18) and the second assigns 0 to another local variable (say var_8).

And then we are at the comparison to 0 again. So going over all these lines, it is obvious that this is a loop where the value of rdiargument is loaded into var_18a number which then acts as a counter, from which is counted down to zero. The second local variable var_8 is simply the sum of the different counter values. For example if var_18 has 5 then the result would be 5+4+3+2+1=15.

The corresponding C code would be similar to this for loading var_18 with 5.

long foo() {
    long var_18 =nr) 5;{
    long var_8 = 0;

    while(var_18nr != 0) {
        var_8 += var_18nr--;
    } 
    return var_8;
}

These two QWORDS are not arguments but local variables. You can recognize this, because of the negative offset (variables are above the base pointer and the stack is growing towards lower addresses). Since they are QWORDS, the datatype has a length of 8 byte. The first instruction assigns the value of rdi to a local variable (say var_18) and the second assigns 0 to another local variable (say var_8).

And then we are at the comparison to 0 again. So going over all these lines, it is obvious that this is a loop where the value of rdi is loaded into var_18 which then acts as a counter, from which is counted down to zero. The second local variable var_8 is simply the sum of the different counter values. For example if var_18 has 5 then the result would be 5+4+3+2+1=15.

The corresponding C code would be similar to this for loading var_18 with 5.

long foo() {
    long var_18 = 5;
    long var_8 = 0;

    while(var_18 != 0) {
        var_8 += var_18--;
    } 
    return var_8;
}

These two QWORDS are not arguments but local variables. You can recognize this, because of the negative offset (variables are above the base pointer and the stack is growing towards lower addresses). x64 assembler gives the first arguments in registers, so the first line is a function argument. The second one is a local variable. Since they are QWORDS, the datatype has a length of 8 byte. The first instruction assigns the value of rdi to a local variable (say var_18) and the second assigns 0 to another local variable (say var_8).

And then we are at the comparison to 0 again. So going over all these lines, it is obvious that this is a loop where the argument is a number which then acts as a counter, from which is counted down to zero. The second local variable var_8 is simply the sum of the different counter values. For example if var_18 has 5 then the result would be 5+4+3+2+1=15.

The corresponding C code would be similar to this.

long foo(long nr) {
    long var_8 = 0;

    while(nr != 0) {
        var_8 += nr--;
    } 
    return var_8;
}
deleted 1 character in body
Source Link
pudi
  • 180
  • 1
  • 2
  • 13

First of all, this is 64bit assembly because of the registers (rax,..) and the memory address in the first line. So with the option -m32 that compiles to 32bit executables, you will get some problems.

You're right about the first part, push rbp and mov rbp,rsp is the function preamble.

mov    QWORD PTR [rbp-0x18],rdi
mov    QWORD PTR [rbp-0x8],0x0

These two QWORDS are not arguments but local variables. You can recognize this, because of the negative offset (variables are above the base pointer and the stack is growing towards lower addresses). Since they are QWORDS, the datatype has a length of 8 byte. The first instruction assigns the value of rdi to a local variable (say var_18) and the second assigns 0 to another local variable (say var_8).

jmp    400733 <foo+0x22>

This is an unconditional jump to the location 400733 meaning you will always jump there. So we need to continue at this location.

cmp    QWORD PTR [rbp-0x18],0x0
jne    400723 <foo+0x12>

At 400733 we see a compare instruction, followed by a conditional jump. The compare checks if the local variable var_18 is 0, if so the value of var_8 is returned (writing the value it to rax and return). Otherwise if not 0, we jump to 400723.

mov    rax,QWORD PTR [rbp-0x18]
lea    rdx,[rax-0x1]
mov    QWORD PTR [rbp-0x18],rdx
add    QWORD PTR [rbp-0x8],rax

Continuing at 400723 the first three instructions decrease the value of var_18 by 1. But the initial value before the subtraction is stored inadded to var_8. This seems a little confusing, but if you are familiar with C/C++, an instruction like var-- comes in mind, where you use the value of the variable before it is subtracted.

And then we are at the comparison to 0 again. So going over all these lines, it is obvious that this is a loop where the value of rdi is loaded into var_18 which then acts as a counter, from which is counted down to zero. The second local variable var_8 is simply the sum of the different counter values. For example if var_18 has 5 then the result would be 5+4+3+2+1=15.

The corresponding C code would be similar to this for loading var_18 with 5.

long foo() {
    long var_18 = 5;
    long var_8 = 0;

    while(var_18 != 0) {
        var_8 += var_18--;
    } 
    return var_8;
}

In addition, is there a fast way (e.g. a decompiler) that can do this for me in the future? I couldn't find any of those either.

Yes, Hex-Rays Decompiler would do the job.

First of all, this is 64bit assembly because of the registers (rax,..) and the memory address in the first line. So with the option -m32 that compiles to 32bit executables, you will get some problems.

You're right about the first part, push rbp and mov rbp,rsp is the function preamble.

mov    QWORD PTR [rbp-0x18],rdi
mov    QWORD PTR [rbp-0x8],0x0

These two QWORDS are not arguments but local variables. You can recognize this, because of the negative offset (variables are above the base pointer and the stack is growing towards lower addresses). Since they are QWORDS, the datatype has a length of 8 byte. The first instruction assigns the value of rdi to a local variable (say var_18) and the second assigns 0 to another local variable (say var_8).

jmp    400733 <foo+0x22>

This is an unconditional jump to the location 400733 meaning you will always jump there. So we need to continue at this location.

cmp    QWORD PTR [rbp-0x18],0x0
jne    400723 <foo+0x12>

At 400733 we see a compare instruction, followed by a conditional jump. The compare checks if the local variable var_18 is 0, if so the value of var_8 is returned (writing the value it to rax and return). Otherwise if not 0, we jump to 400723.

mov    rax,QWORD PTR [rbp-0x18]
lea    rdx,[rax-0x1]
mov    QWORD PTR [rbp-0x18],rdx
add    QWORD PTR [rbp-0x8],rax

Continuing at 400723 the first three instructions decrease the value of var_18 by 1. But the initial value before the subtraction is stored in var_8. This seems a little confusing, but if you are familiar with C/C++, an instruction like var-- comes in mind, where you use the value of the variable before it is subtracted.

And then we are at the comparison to 0 again. So going over all these lines, it is obvious that this is a loop where the value of rdi is loaded into var_18 which then acts as a counter, from which is counted down to zero. The second local variable var_8 is simply the sum of the different counter values. For example if var_18 has 5 then the result would be 5+4+3+2+1=15.

The corresponding C code would be similar to this for loading var_18 with 5.

long foo() {
    long var_18 = 5;
    long var_8 = 0;

    while(var_18 != 0) {
        var_8 += var_18--;
    } 
    return var_8;
}

In addition, is there a fast way (e.g. a decompiler) that can do this for me in the future? I couldn't find any of those either.

Yes, Hex-Rays Decompiler would do the job.

First of all, this is 64bit assembly because of the registers (rax,..) and the memory address in the first line. So with the option -m32 that compiles to 32bit executables, you will get some problems.

You're right about the first part, push rbp and mov rbp,rsp is the function preamble.

mov    QWORD PTR [rbp-0x18],rdi
mov    QWORD PTR [rbp-0x8],0x0

These two QWORDS are not arguments but local variables. You can recognize this, because of the negative offset (variables are above the base pointer and the stack is growing towards lower addresses). Since they are QWORDS, the datatype has a length of 8 byte. The first instruction assigns the value of rdi to a local variable (say var_18) and the second assigns 0 to another local variable (say var_8).

jmp    400733 <foo+0x22>

This is an unconditional jump to the location 400733 meaning you will always jump there. So we need to continue at this location.

cmp    QWORD PTR [rbp-0x18],0x0
jne    400723 <foo+0x12>

At 400733 we see a compare instruction, followed by a conditional jump. The compare checks if the local variable var_18 is 0, if so the value of var_8 is returned (writing the value it to rax and return). Otherwise if not 0, we jump to 400723.

mov    rax,QWORD PTR [rbp-0x18]
lea    rdx,[rax-0x1]
mov    QWORD PTR [rbp-0x18],rdx
add    QWORD PTR [rbp-0x8],rax

Continuing at 400723 the first three instructions decrease the value of var_18 by 1. But the initial value before the subtraction is added to var_8. This seems a little confusing, but if you are familiar with C/C++, an instruction like var-- comes in mind, where you use the value of the variable before it is subtracted.

And then we are at the comparison to 0 again. So going over all these lines, it is obvious that this is a loop where the value of rdi is loaded into var_18 which then acts as a counter, from which is counted down to zero. The second local variable var_8 is simply the sum of the different counter values. For example if var_18 has 5 then the result would be 5+4+3+2+1=15.

The corresponding C code would be similar to this for loading var_18 with 5.

long foo() {
    long var_18 = 5;
    long var_8 = 0;

    while(var_18 != 0) {
        var_8 += var_18--;
    } 
    return var_8;
}

In addition, is there a fast way (e.g. a decompiler) that can do this for me in the future? I couldn't find any of those either.

Yes, Hex-Rays Decompiler would do the job.

added 1 character in body
Source Link
pudi
  • 180
  • 1
  • 2
  • 13

First of all, this is 64bit assembly because of the registers (rax,..) and the memory address in the first line. So with the option -m32 that compiles to 32bit executables, you will get some problems.

You're right about the first part, push rbp and mov rbp,rsp is the function preamble.

mov    QWORD PTR [rbp-0x18],rdi
mov    QWORD PTR [rbp-0x8],0x0

These two linesQWORDS are not arguments but local variables. You can recognize this, because of the negative offset (variables are above the base pointer and the stack is growing towards lower addresses). Since they are QWORDS, the datatype has a length of 8 byte. The first instruction assigns the value of rdi to a local variable (say var_18) and the second assigns 0 to another local variable (say var_8).

jmp    400733 <foo+0x22>

This is an unconditional jump to the location 400733 meaning you will always jump there. So we need to continue at this location.

cmp    QWORD PTR [rbp-0x18],0x0
jne    400723 <foo+0x12>

At 400733 we see a compare instruction, followed by a conditional jump. The compare checks if the local variable var_18 is 0, if so the value of var_8 is returned (writing the value it to rax and return). Otherwise if not 0, we jump to 400723.

mov    rax,QWORD PTR [rbp-0x18]
lea    rdx,[rax-0x1]
mov    QWORD PTR [rbp-0x18],rdx
add    QWORD PTR [rbp-0x8],rax

Continuing at 400723 the first three instructions decrease the value of var_18 by 1. But the initial value before the subtraction is stored in var_8. This seems a little confusing, but if you are familiar with C/C++, an instruction like var-- comes in mind, where you use the value of the variable before it is subtracted.

And then we are at the comparison to 0 again. So going over all these lines, it is obvious that this is a loop where the value of rdi is loaded into var_18 which then acts as a counter, from which is counted down to zero. The second local variable var_8 is simply the sum of the different counter values. For example if var_18 has 5 then the result would be 5+4+3+2+1=15.

The corresponding C code would be similar to this for loading var_18 with 5.

long foo() {
    long var_18 = 5;
    long var_8 = 0;

    while(var_18 != 0) {
        var_8 += var_18--;
    } 
    return var_8;
}

In addition, is there a fast way (e.g. a decompiler) that can do this for me in the future? I couldn't find any of those either.

Yes, Hex-Rays Decompiler would do the job.

First of all, this is 64bit assembly because of the registers (rax,..) and the memory address in the first line. So with the option -m32 that compiles to 32bit executables, you will get some problems.

You're right about the first part, push rbp and mov rbp,rsp is the function preamble.

mov    QWORD PTR [rbp-0x18],rdi
mov    QWORD PTR [rbp-0x8],0x0

These two lines are not arguments but local variables. You can recognize this, because of the negative offset (variables are above the base pointer and the stack is growing towards lower addresses). Since they are QWORDS, the datatype has a length of 8 byte. The first instruction assigns the value of rdi to a local variable (say var_18) and the second assigns 0 to another local variable (say var_8).

jmp    400733 <foo+0x22>

This is an unconditional jump to the location 400733 meaning you will always jump there. So we need to continue at this location.

cmp    QWORD PTR [rbp-0x18],0x0
jne    400723 <foo+0x12>

At 400733 we see a compare instruction, followed by a conditional jump. The compare checks if the local variable var_18 is 0, if so the value of var_8 is returned (writing the value it to rax and return). Otherwise if not 0, we jump to 400723.

mov    rax,QWORD PTR [rbp-0x18]
lea    rdx,[rax-0x1]
mov    QWORD PTR [rbp-0x18],rdx
add    QWORD PTR [rbp-0x8],rax

Continuing at 400723 the first three instructions decrease the value of var_18 by 1. But the initial value before the subtraction is stored in var_8. This seems a little confusing, but if you are familiar with C/C++, an instruction like var-- comes in mind, where you use the value of the variable before it is subtracted.

And then we are at the comparison to 0 again. So going over all these lines, it is obvious that this is a loop where the value of rdi is loaded into var_18 which then acts as a counter, from which is counted down to zero. The second local variable var_8 is simply the sum of the different counter values. For example if var_18 has 5 then the result would be 5+4+3+2+1=15.

The corresponding C code would be similar to this for loading var_18 with 5.

long foo() {
    long var_18 = 5;
    long var_8 = 0;

    while(var_18 != 0) {
        var_8 += var_18--;
    } 
    return var_8;
}

In addition, is there a fast way (e.g. a decompiler) that can do this for me in the future? I couldn't find any of those either.

Yes, Hex-Rays Decompiler would do the job.

First of all, this is 64bit assembly because of the registers (rax,..) and the memory address in the first line. So with the option -m32 that compiles to 32bit executables, you will get some problems.

You're right about the first part, push rbp and mov rbp,rsp is the function preamble.

mov    QWORD PTR [rbp-0x18],rdi
mov    QWORD PTR [rbp-0x8],0x0

These two QWORDS are not arguments but local variables. You can recognize this, because of the negative offset (variables are above the base pointer and the stack is growing towards lower addresses). Since they are QWORDS, the datatype has a length of 8 byte. The first instruction assigns the value of rdi to a local variable (say var_18) and the second assigns 0 to another local variable (say var_8).

jmp    400733 <foo+0x22>

This is an unconditional jump to the location 400733 meaning you will always jump there. So we need to continue at this location.

cmp    QWORD PTR [rbp-0x18],0x0
jne    400723 <foo+0x12>

At 400733 we see a compare instruction, followed by a conditional jump. The compare checks if the local variable var_18 is 0, if so the value of var_8 is returned (writing the value it to rax and return). Otherwise if not 0, we jump to 400723.

mov    rax,QWORD PTR [rbp-0x18]
lea    rdx,[rax-0x1]
mov    QWORD PTR [rbp-0x18],rdx
add    QWORD PTR [rbp-0x8],rax

Continuing at 400723 the first three instructions decrease the value of var_18 by 1. But the initial value before the subtraction is stored in var_8. This seems a little confusing, but if you are familiar with C/C++, an instruction like var-- comes in mind, where you use the value of the variable before it is subtracted.

And then we are at the comparison to 0 again. So going over all these lines, it is obvious that this is a loop where the value of rdi is loaded into var_18 which then acts as a counter, from which is counted down to zero. The second local variable var_8 is simply the sum of the different counter values. For example if var_18 has 5 then the result would be 5+4+3+2+1=15.

The corresponding C code would be similar to this for loading var_18 with 5.

long foo() {
    long var_18 = 5;
    long var_8 = 0;

    while(var_18 != 0) {
        var_8 += var_18--;
    } 
    return var_8;
}

In addition, is there a fast way (e.g. a decompiler) that can do this for me in the future? I couldn't find any of those either.

Yes, Hex-Rays Decompiler would do the job.

added 1 character in body
Source Link
pudi
  • 180
  • 1
  • 2
  • 13
Loading
Source Link
pudi
  • 180
  • 1
  • 2
  • 13
Loading