了解一些汇编代码?

时间:2014-03-26 00:39:16

标签: assembly

我需要一些帮助来破译这意味着什么,我对我认为它做的评论,但我不完全确定。

movl 12(%ebp), %eax  //variable (x) moves into eax register
addl $4, %eax        // add the value 4 to x
movl (%eax), %eax    //eax = *x;
movl %eax, (%esp)    //stack pointer = *x
call strlen          //calls to gets length of string 
movl %eax, %edx      //copy *x to edx register, name it *y
movl %edx, %eax      //copy *y to eax
sall $2, %eax        //shift *x left 2 (eax)
addl %edx, %eax      //*x + *y = *x (x is shifted 2, remember)
movl %eax, (%esp)    // move the new *x string onto stack pointer
call malloc          // memory allocate for the string
movl %eax, 28(%esp)  //move this string onto a new variable, lets say z
movl $.LC1, %edx     //move string in LC1 into edx
movl 12(%ebp), %eax  //repeat what was at the top
addl $4, %eax
movl (%eax), %eax
movl 28(%esp), %ecx  // move z into ecx register
movl %ecx, 8(%esp)   // move z closer to stack pointer
movl %edx, 4(%esp)   // move y closest to stack pointer
movl %eax, (%esp)    // set stack pointer to x. now the stack goes: x, y, z
call __isoc99_sscanf //return the number of input items successfully matched
cmpl $1, %eax        //if x == 1,
je   .L20            //jump to L20
movl $.LC2, (%esp)   //else, LC2 becomes stack pointer
call puts            //calls procedure, which is LC2
movl $1, %eax        // makes x = 1
jmp  .L19            // jump to end

2 个答案:

答案 0 :(得分:1)

这不太正确。这是我的尝试:

movl    12(%ebp), %eax  //[esp] = [[ebp+12]+4]
addl    $4, %eax
movl    (%eax), %eax
movl    %eax, (%esp)
call    strlen          //eax = length of string 
movl    %eax, %edx      //edx = length of string
movl    %edx, %eax      //useless instruction
sall    $2, %eax        //eax = length x 4
addl    %edx, %eax      //eax = length x 5
movl    %eax, (%esp)    //allocate length x 5 bytes
call    malloc
movl    %eax, 28(%esp)  //[ebp+28] = ptr to allocated memory
movl    $.LC1, %edx     //edx = offset .LC1
movl    12(%ebp), %eax  //eax = [[ebp+12]+4]
addl    $4, %eax
movl    (%eax), %eax
movl    28(%esp), %ecx  //[esp+8] = ptr to allocated memory
movl    %ecx, 8(%esp)
movl    %edx, 4(%esp)   //[esp+4] = offset .LC1
movl    %eax, (%esp)    //[esp]   = [[ebp+12]+4]
call    __isoc99_sscanf //return the number of input items successfully matched
cmpl    $1, %eax        //if result == 1
je  .L20                //jump to L20
movl    $.LC2, (%esp)   //[esp] = offset .LC2
call    puts            //display .LC2 string
movl    $1, %eax        //eax = 1
jmp .L19                //jump to ???

答案 1 :(得分:0)

就我看来,它看起来是正确的。如果它只保留一个范例,那么第三条指令就不那么容易混淆了;

 //x = *x;

此外,这可能是一个误解:

movl %eax, (%esp) //stack pointer = *x
call strlen       //calls to gets length of string
movl %eax, %edx   //copy *x to edx register, name it *y

这不是改变堆栈指针,只改变堆栈顶部的字。堆栈指针仍然指向同一个地方。它调用strlen,它希望它的参数位于堆栈顶部。 strlen()在%eax中返回其值,与所有函数的自定义一样。所以这些可能会更好地重写为

movl %eax, (%esp) // p1 = x (pointer to string)
call strlen       // length of string
movl %eax, %edx   // copy strlen(x) to edx register, name it y

也许你可以用这么多来自己解决更多问题。