是否可以将给定的C代码转换为Assembly x86?

时间:2019-04-22 20:33:22

标签: c assembly

我在装配x86的AWD避障机器人中工作。我可以找到一些已经用C语言执行但不能在程序集x86中执行的程序。 如何将这些C代码转换为Assembly x86代码? 整个代码在这里: http://www.mertarduino.com/arduino-obstacle-avoiding-robot-car-4wd/2018/11/22/

void compareDistance()   // find the longest distance
{
  if (leftDistance>rightDistance) //if left is less obstructed 
  {
    turnLeft();
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    turnRight();
  }
   else //if they are equally obstructed
  {
    turnAround();
  }
}

int readPing() { // read the ultrasonic sensor distance
  delay(70);   
  unsigned int uS = sonar.ping();
  int cm = uSenter code here/US_ROUNDTRIP_CM;
  return cm;
}

2 个答案:

答案 0 :(得分:2)

  

如何将这些C代码转换为Assembly x86代码?

将源代码转换为汇编基本上是编译器的工作,因此只需对其进行编译。大多数(如果不是全部)编译器可以选择输出中间汇编代码。

如果使用gcc -S main.c,将得到一个名为main.s的文件,其中包含汇编代码。

这里是一个例子:

$ cat hello.c
#include <stdio.h>

void print_hello() {
    puts("Hello World!");
}

int main() {
    print_hello();
}

$ gcc -S hello.c 

$ cat hello.s
    .file   "hello.c"
    .text
    .section    .rodata
.LC0:
    .string "Hello World!"
    .text
    .globl  print_hello
    .type   print_hello, @function
print_hello:
.LFB0:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    leaq    .LC0(%rip), %rdi
    call    puts@PLT
    nop
    popq    %rbp
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE0:
    .size   print_hello, .-print_hello
    .globl  main
    .type   main, @function
main:
.LFB1:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    movl    $0, %eax
    call    print_hello
    movl    $0, %eax
    popq    %rbp
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE1:
    .size   main, .-main
    .ident  "GCC: (Debian 8.3.0-6) 8.3.0"
    .section    .note.GNU-stack,"",@progbits

答案 1 :(得分:2)

  

如何将这些C代码转换为Assembly x86代码?

您可以使用gcc -m32 -S main.c命令来执行此操作,其中:

  • -S标志指示输出必须是汇编,
  • -m32标志表示您要生成i386(32位)输出。