×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

#### Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
To Your Posts
• Keyword Search
• One-Click Access To Your
Favorite Forums
• Automated Signatures
On Your Posts
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

# Help with translating C code into assembler code2

 Forum Search FAQs Links MVPs

## Help with translating C code into assembler code

(OP)
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) ;

### RE: Help with translating C code into assembler code

For your robot.c (I corrected some syntax errors)

#### CODE

#include <stdio.h>
int left_feeler;
int right_feeler;
int left_speed;
int 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) ;
}
}
if i use gcc, then this command

$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 .file "robot.c" .def ___main; .scl 2; .type 32; .endef .section .rdata,"dr" LC0: .ascii " left feeler=\0" LC1: .ascii "%d/n\0" LC2: .ascii " right feeler=\0" LC3: .ascii "%d\0" LC4: .ascii "right speed = %d \12\0" LC5: .ascii "left speed = %d \12\0" .text .globl _main .def _main; .scl 2; .type 32; .endef _main: pushl %ebp movl %esp, %ebp subl$24, %esp
andl $-16, %esp movl$0, %eax
addl $15, %eax addl$15, %eax
shrl $4, %eax sall$4, %eax
movl %eax, -4(%ebp)
movl -4(%ebp), %eax
call __alloca
call ___main
L2:
movl $LC0, (%esp) call _printf movl$_left_feeler, 4(%esp)
movl $LC1, (%esp) call _scanf movl$LC2, (%esp)
call _printf
movl $_right_feeler, 4(%esp) movl$LC3, (%esp)
call _scanf
cmpl $0, _left_feeler jne L4 cmpl$0, _right_feeler
jne  L4
movl $10, _left_speed movl$10, _right_speed
L4:
cmpl $1, _left_feeler jne L5 cmpl$0, _right_feeler
jne  L5
movl $10, _left_speed movl$0, _right_speed
L5:
cmpl $0, _left_feeler jne L6 cmpl$1, _right_feeler
jne  L6
movl $0, _left_speed movl$10, _right_speed
L6:
cmpl $1, _left_feeler jne L7 cmpl$1, _right_feeler
jne  L7
movl $10, _left_speed movl$0, _right_speed
L7:
movl _right_speed, %eax
movl %eax, 4(%esp)
movl $LC4, (%esp) call _printf movl _left_speed, %eax movl %eax, 4(%esp) movl$LC5, (%esp)
call _printf
jmp  L2
.comm  _left_feeler, 16  # 4
.comm  _right_feeler, 16   # 4
.comm  _left_speed, 16   # 4
.comm  _right_speed, 16  # 4
.def _scanf; .scl  2;  .type 32; .endef
.def _printf;  .scl  2;  .type 32; .endef

### RE: Help with translating C code into assembler code

(OP)
Thank you mikrom.
With this compiled now how would I now produce an execution trace of all the possible combinations of inputs?

### RE: Help with translating C code into assembler code

#### Quote:

...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...

### RE: Help with translating C code into assembler code

Convert it to a function that accept two parameters and use a for loop.

Cheers,
Dian

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

### Reply To This Thread

#### Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!

Already a member? Close this window and log in.