简体   繁体   English

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

[英]It is possible to convert given C code to Assembly x86?

I'm working in AWD obstacle avoidance robot in assembly x86. 我在装配x86的AWD避障机器人中工作。 I can find out some program which is already executed in C language but can't find executed in assembly x86. 我可以找到一些已经用C语言执行但不能在程序集x86中执行的程序。 How do convert these C codes to Assembly x86 code? 如何将这些C代码转换为Assembly x86代码? The whole part of codes here: http://www.mertarduino.com/arduino-obstacle-avoiding-robot-car-4wd/2018/11/22/ 此处的整个代码部分: 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;
}

How do convert these C codes to Assembly x86 code? 如何将这些C代码转换为Assembly x86代码?

Converting source code to assembly is basically what a compiler does, so just compile it. 将源代码转换为汇编基本上是编译器的工作,因此只需对其进行编译。 Most (if not all) compilers have the option of outputting the intermediate assembly code. 大多数(如果不是全部)编译器可以选择输出中间汇编代码。

If you use gcc -S main.c you will get a file called main.s containing the assembly code. 如果使用gcc -S main.c ,将获得一个名为main.s的文件, main.s包含汇编代码。

Here is an example: 这是一个例子:

$ 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

How do convert these C codes to Assembly x86 code? 如何将这些C代码转换为Assembly x86代码?

You can use the gcc -m32 -S main.c command to do that, where : 您可以使用gcc -m32 -S main.c命令执行以下操作:

  • the -S flag indicates that the output must be assembly, -S标志指示输出必须是汇编,
  • the -m32 flag indicates that you want to produce i386 (32-bit) output. -m32标志指示您要生成i386(32位)输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM