Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with translating C code into assembler code 2

Status
Not open for further replies.

sHeZ123

Programmer
Apr 28, 2009
2
GB
Hi im doing a project that moves a robot around a maze avoiding walls and need some help with converting C code into assembly code.




#include <stdio.h>
int left_feeler;
int right_feeler
int left_speed;
in right_speed;

void main ()
{
While (1)
{
printf(" left feeler=");
scanf( "%d/n", &left_feeler);
printf(" right feeler=");
scanf( "%d", &right_feeler);
if (( left_feeler ==0) && ( right_feeler ==0))
{
left_speed = 10 ;
right_speed = 10 ;
}
if (( left_feeler ==1) && ( right_feeler ==0))
{
left_speed = 10 ;
right_speed = 0 ;
}
if (( left_feeler ==0) && ( right_feeler ==1))
{
left_speed = 0 ;
right_speed = 10 ;
}
if (( left_feeler ==1) && ( right_feeler ==1))
{
left_speed = 10 ;
right_speed = 0 ;
}
printf("right speed = %d \n", right_speed) ;
printf("left speed = %d \n", left_speed) ;
 
For your robot.c (I corrected some syntax errors)
Code:
[COLOR=#a020f0]#include [/color][COLOR=#ff00ff]<stdio.h>[/color]
[COLOR=#2e8b57][b]int[/b][/color] left_feeler;
[COLOR=#2e8b57][b]int[/b][/color] right_feeler;
[COLOR=#2e8b57][b]int[/b][/color] left_speed;
[COLOR=#2e8b57][b]int[/b][/color] right_speed;

[COLOR=#2e8b57][b]void[/b][/color] main ()
{
  [COLOR=#804040][b]while[/b][/color] ([COLOR=#ff00ff]1[/color])
  {
  printf([COLOR=#ff00ff]" left feeler="[/color]);
  scanf( [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]%d[/color][COLOR=#ff00ff]/n"[/color], &left_feeler);
  printf([COLOR=#ff00ff]" right feeler="[/color]);
  scanf( [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]%d[/color][COLOR=#ff00ff]"[/color], &right_feeler);
  [COLOR=#804040][b]if[/b][/color] (( left_feeler ==[COLOR=#ff00ff]0[/color]) && ( right_feeler ==[COLOR=#ff00ff]0[/color]))
     {
        left_speed = [COLOR=#ff00ff]10[/color] ;
        right_speed = [COLOR=#ff00ff]10[/color] ;
     }
  [COLOR=#804040][b]if[/b][/color] (( left_feeler ==[COLOR=#ff00ff]1[/color]) && ( right_feeler ==[COLOR=#ff00ff]0[/color]))
     {
        left_speed = [COLOR=#ff00ff]10[/color] ;
        right_speed = [COLOR=#ff00ff]0[/color] ;
     }
  [COLOR=#804040][b]if[/b][/color] (( left_feeler ==[COLOR=#ff00ff]0[/color]) && ( right_feeler ==[COLOR=#ff00ff]1[/color]))
     {
        left_speed = [COLOR=#ff00ff]0[/color] ;
        right_speed = [COLOR=#ff00ff]10[/color] ;
     }
  [COLOR=#804040][b]if[/b][/color] (( left_feeler ==[COLOR=#ff00ff]1[/color]) && ( right_feeler ==[COLOR=#ff00ff]1[/color]))
     {
        left_speed = [COLOR=#ff00ff]10[/color] ;
        right_speed = [COLOR=#ff00ff]0[/color] ;
     }
     printf([COLOR=#ff00ff]"right speed = [/color][COLOR=#6a5acd]%d[/color][COLOR=#ff00ff] [/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], right_speed) ;
     printf([COLOR=#ff00ff]"left speed = [/color][COLOR=#6a5acd]%d[/color][COLOR=#ff00ff] [/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], left_speed) ;
  }
}
if i use gcc, then this command
Code:
$ gcc -S robot.c
robot.c: In function `main':
robot.c:8: warning: return type of 'main' is not `int'
generates the assembler code - see this file
robot.s
Code:
  [COLOR=#804040][b].file[/b][/color]  "[COLOR=#008080]robot[/color].[COLOR=#008080]c[/color]"
  [COLOR=#804040][b].def[/b][/color] [COLOR=#008080]___main[/color];[COLOR=#0000ff]  .scl  2;  .type 32; .endef[/color]
  [COLOR=#804040][b].section[/b][/color] [COLOR=#804040][b].rdata[/b][/color],"[COLOR=#008080]dr[/color]"
[COLOR=#008080]LC0[/color]:
  [COLOR=#804040][b].ascii[/b][/color] " [COLOR=#008080]left[/color] [COLOR=#008080]feeler[/color]=\0"
[COLOR=#008080]LC1[/color]:
  [COLOR=#804040][b].ascii[/b][/color] "%[COLOR=#008080]d[/color]/[COLOR=#008080]n[/color]\0"
[COLOR=#008080]LC2[/color]:
  [COLOR=#804040][b].ascii[/b][/color] " [COLOR=#008080]right[/color] [COLOR=#008080]feeler[/color]=\0"
[COLOR=#008080]LC3[/color]:
  [COLOR=#804040][b].ascii[/b][/color] "%[COLOR=#008080]d[/color]\0"
[COLOR=#008080]LC4[/color]:
  [COLOR=#804040][b].ascii[/b][/color] "[COLOR=#008080]right[/color] [COLOR=#008080]speed[/color] = %[COLOR=#008080]d[/color] \[COLOR=#ff00ff]12[/color]\0"
[COLOR=#008080]LC5[/color]:
  [COLOR=#804040][b].ascii[/b][/color] "[COLOR=#008080]left[/color] [COLOR=#008080]speed[/color] = %[COLOR=#008080]d[/color] \[COLOR=#ff00ff]12[/color]\0"
  [COLOR=#804040][b].text[/b][/color]
[COLOR=#804040][b].globl[/b][/color] [COLOR=#008080]_main[/color]
  [COLOR=#804040][b].def[/b][/color] [COLOR=#008080]_main[/color];[COLOR=#0000ff]  .scl  2;  .type 32; .endef[/color]
[COLOR=#008080]_main[/color]:
  [COLOR=#008080]pushl[/color]  %[COLOR=#008080]ebp[/color]
  [COLOR=#008080]movl[/color] %[COLOR=#008080]esp[/color], %[COLOR=#008080]ebp[/color]
  [COLOR=#008080]subl[/color] $[COLOR=#ff00ff]24[/color], %[COLOR=#008080]esp[/color]
  [COLOR=#008080]andl[/color] $-[COLOR=#ff00ff]16[/color], %[COLOR=#008080]esp[/color]
  [COLOR=#008080]movl[/color] $[COLOR=#ff00ff]0,[/color] %[COLOR=#008080]eax[/color]
  [COLOR=#008080]addl[/color] $[COLOR=#ff00ff]15[/color], %[COLOR=#008080]eax[/color]
  [COLOR=#008080]addl[/color] $[COLOR=#ff00ff]15[/color], %[COLOR=#008080]eax[/color]
  [COLOR=#008080]shrl[/color] $[COLOR=#ff00ff]4[/color], %[COLOR=#008080]eax[/color]
  [COLOR=#008080]sall[/color] $[COLOR=#ff00ff]4[/color], %[COLOR=#008080]eax[/color]
  [COLOR=#008080]movl[/color] %[COLOR=#008080]eax[/color], -[COLOR=#ff00ff]4[/color](%[COLOR=#008080]ebp[/color])
  [COLOR=#008080]movl[/color] -[COLOR=#ff00ff]4[/color](%[COLOR=#008080]ebp[/color]), %[COLOR=#008080]eax[/color]
  [COLOR=#008080]call[/color] [COLOR=#008080]__alloca[/color]
  [COLOR=#008080]call[/color] [COLOR=#008080]___main[/color]
[COLOR=#008080]L2[/color]:
  [COLOR=#008080]movl[/color] $[COLOR=#008080]LC0[/color], (%[COLOR=#008080]esp[/color])
  [COLOR=#008080]call[/color] [COLOR=#008080]_printf[/color]
  [COLOR=#008080]movl[/color] $[COLOR=#008080]_left_feeler[/color], [COLOR=#ff00ff]4[/color](%[COLOR=#008080]esp[/color])
  [COLOR=#008080]movl[/color] $[COLOR=#008080]LC1[/color], (%[COLOR=#008080]esp[/color])
  [COLOR=#008080]call[/color] [COLOR=#008080]_scanf[/color]
  [COLOR=#008080]movl[/color] $[COLOR=#008080]LC2[/color], (%[COLOR=#008080]esp[/color])
  [COLOR=#008080]call[/color] [COLOR=#008080]_printf[/color]
  [COLOR=#008080]movl[/color] $[COLOR=#008080]_right_feeler[/color], [COLOR=#ff00ff]4[/color](%[COLOR=#008080]esp[/color])
  [COLOR=#008080]movl[/color] $[COLOR=#008080]LC3[/color], (%[COLOR=#008080]esp[/color])
  [COLOR=#008080]call[/color] [COLOR=#008080]_scanf[/color]
  [COLOR=#008080]cmpl[/color] $[COLOR=#ff00ff]0,[/color] [COLOR=#008080]_left_feeler[/color]
  [COLOR=#008080]jne[/color]  [COLOR=#008080]L4[/color]
  [COLOR=#008080]cmpl[/color] $[COLOR=#ff00ff]0,[/color] [COLOR=#008080]_right_feeler[/color]
  [COLOR=#008080]jne[/color]  [COLOR=#008080]L4[/color]
  [COLOR=#008080]movl[/color] $[COLOR=#ff00ff]10[/color], [COLOR=#008080]_left_speed[/color]
  [COLOR=#008080]movl[/color] $[COLOR=#ff00ff]10[/color], [COLOR=#008080]_right_speed[/color]
[COLOR=#008080]L4[/color]:
  [COLOR=#008080]cmpl[/color] $[COLOR=#ff00ff]1[/color], [COLOR=#008080]_left_feeler[/color]
  [COLOR=#008080]jne[/color]  [COLOR=#008080]L5[/color]
  [COLOR=#008080]cmpl[/color] $[COLOR=#ff00ff]0,[/color] [COLOR=#008080]_right_feeler[/color]
  [COLOR=#008080]jne[/color]  [COLOR=#008080]L5[/color]
  [COLOR=#008080]movl[/color] $[COLOR=#ff00ff]10[/color], [COLOR=#008080]_left_speed[/color]
  [COLOR=#008080]movl[/color] $[COLOR=#ff00ff]0,[/color] [COLOR=#008080]_right_speed[/color]
[COLOR=#008080]L5[/color]:
  [COLOR=#008080]cmpl[/color] $[COLOR=#ff00ff]0,[/color] [COLOR=#008080]_left_feeler[/color]
  [COLOR=#008080]jne[/color]  [COLOR=#008080]L6[/color]
  [COLOR=#008080]cmpl[/color] $[COLOR=#ff00ff]1[/color], [COLOR=#008080]_right_feeler[/color]
  [COLOR=#008080]jne[/color]  [COLOR=#008080]L6[/color]
  [COLOR=#008080]movl[/color] $[COLOR=#ff00ff]0,[/color] [COLOR=#008080]_left_speed[/color]
  [COLOR=#008080]movl[/color] $[COLOR=#ff00ff]10[/color], [COLOR=#008080]_right_speed[/color]
[COLOR=#008080]L6[/color]:
  [COLOR=#008080]cmpl[/color] $[COLOR=#ff00ff]1[/color], [COLOR=#008080]_left_feeler[/color]
  [COLOR=#008080]jne[/color]  [COLOR=#008080]L7[/color]
  [COLOR=#008080]cmpl[/color] $[COLOR=#ff00ff]1[/color], [COLOR=#008080]_right_feeler[/color]
  [COLOR=#008080]jne[/color]  [COLOR=#008080]L7[/color]
  [COLOR=#008080]movl[/color] $[COLOR=#ff00ff]10[/color], [COLOR=#008080]_left_speed[/color]
  [COLOR=#008080]movl[/color] $[COLOR=#ff00ff]0,[/color] [COLOR=#008080]_right_speed[/color]
[COLOR=#008080]L7[/color]:
  [COLOR=#008080]movl[/color] [COLOR=#008080]_right_speed[/color], %[COLOR=#008080]eax[/color]
  [COLOR=#008080]movl[/color] %[COLOR=#008080]eax[/color], [COLOR=#ff00ff]4[/color](%[COLOR=#008080]esp[/color])
  [COLOR=#008080]movl[/color] $[COLOR=#008080]LC4[/color], (%[COLOR=#008080]esp[/color])
  [COLOR=#008080]call[/color] [COLOR=#008080]_printf[/color]
  [COLOR=#008080]movl[/color] [COLOR=#008080]_left_speed[/color], %[COLOR=#008080]eax[/color]
  [COLOR=#008080]movl[/color] %[COLOR=#008080]eax[/color], [COLOR=#ff00ff]4[/color](%[COLOR=#008080]esp[/color])
  [COLOR=#008080]movl[/color] $[COLOR=#008080]LC5[/color], (%[COLOR=#008080]esp[/color])
  [COLOR=#008080]call[/color] [COLOR=#008080]_printf[/color]
  [COLOR=#008080]jmp[/color]  [COLOR=#008080]L2[/color]
  [COLOR=#804040][b].comm[/b][/color]  [COLOR=#008080]_left_feeler[/color], [COLOR=#ff00ff]16[/color]  # [COLOR=#ff00ff]4[/color]
  [COLOR=#804040][b].comm[/b][/color]  [COLOR=#008080]_right_feeler[/color], [COLOR=#ff00ff]16[/color]   # [COLOR=#ff00ff]4[/color]
  [COLOR=#804040][b].comm[/b][/color]  [COLOR=#008080]_left_speed[/color], [COLOR=#ff00ff]16[/color]   # [COLOR=#ff00ff]4[/color]
  [COLOR=#804040][b].comm[/b][/color]  [COLOR=#008080]_right_speed[/color], [COLOR=#ff00ff]16[/color]  # [COLOR=#ff00ff]4[/color]
  [COLOR=#804040][b].def[/b][/color] [COLOR=#008080]_scanf[/color];[COLOR=#0000ff] .scl  2;  .type 32; .endef[/color]
  [COLOR=#804040][b].def[/b][/color] [COLOR=#008080]_printf[/color];[COLOR=#0000ff]  .scl  2;  .type 32; .endef[/color]
 
Thank you mikrom.
With this compiled now how would I now produce an execution trace of all the possible combinations of inputs?
 
...how would I now produce an execution trace of all the possible combinations of inputs?
Sorry, I don't know, never done something like this...
 
Convert it to a function that accept two parameters and use a for loop.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top