2011-04-02 99 views
0

有人可以将这个高级代码翻译成IA-32程序集而不使用任何全局变量吗?我只是需要它作为我必须做的其他工作的一个例子。谢谢。整数是32位,字符是8位。从高级语言翻译为IA-32

class MyString{ 
    char buff[100]; 
    int len; 

    void deleteChar(char ch){ 
     int to = 0; 
     for (int from = 0; from < this.length; from++){ 
      char nextch = this.buff[from]; 
      if (nextch != ch){ 
      this.buff[to] = nextch; 
      to++; 
      } 
     } 
    } 
} 
+0

作业...... – 2011-04-02 18:00:42

回答

0

这里是给定的,当什么GCC产生:

void delete_char(char ch, char buf[], int len) { 
     int from, to; 
     for (from=0,to=0; from<len; ++from) 
       if (buf[from] != ch) 
         buf[to++] = buf[from]; 
} 


_delete_char: 
     pushl %ebp 
     movl %esp, %ebp 
     subl $40, %esp 
     movl 8(%ebp), %eax 
     movb %al, -28(%ebp) 
     movl $0, -12(%ebp) 
     movl $0, -16(%ebp) 
     jmp  L2 
L3: 
     movl -12(%ebp), %eax 
     addl 12(%ebp), %eax 
     movb (%eax), %al 
     cmpb -28(%ebp), %al 
     je  L4 
     movl -16(%ebp), %eax 
     movl %eax, %edx 
     addl 12(%ebp), %edx 
     movl -12(%ebp), %eax 
     addl 12(%ebp), %eax 
     movb (%eax), %al 
     movb %al, (%edx) 
     incl -16(%ebp) 
L4: 
     incl -12(%ebp) 
L2: 
     movl -12(%ebp), %eax 
     cmpl 16(%ebp), %eax 
     jl  L3 
     leave 
     ret 
+0

为什么子40从ESP?如果你的方法有buff [],并且我把它作为buff [100],那么没有区别吗? ebp + 12是该方法的第一个参数吗? – 2011-04-02 19:03:09

+0

@Sorin,-40最有可能适应堆栈中分配的局部变量。 – 2011-04-02 19:51:47