How to convert c to asm

,i am trying to solve this already a copal days with no luck 'can someone help me ?
i need to convert the code from c to asm

`

# define F_CPU 16000000UL

#include <avr/io.h>
#include <util/delay.h>

asm volatile(".equ DDRD, 0x0A;\n"
             ".equ PORTD, 0x0B;\n"
             ".equ DDRB, 0x04;\n"
             ".equ PORTB, 0x05;\n");

int main()
{
  // Reset direction of PORTD:
  //(assembler command: CLR)
  DDRD = 0;
  
  // Reset PORTD (disable Tx & Rx LEDs)
  //(assembler command: CLR)
  PORTD = 0;
  
  // Reset PORTB
  // (assembler command: CLR)
  PORTB = 0;

  // Set pin 1 of PORTD for output (LED):
  // (assembler command: SBR)
  //DDRD |= 0x01;
  asm("LDI R16, 0x01\n");
  asm("OUT DDRD, R16\n");
  
  while(1)
  {
    // Don't convert this line!!!:
    while(PINB & (0x01));

    //Turn LED on/off:
    PORTD ^= 0x01;

    // Delay controls the blinking frequency: 
    _delay_ms(500);

  }
}

`

Compile it. Converting from C to ASM is what a compiler does.

1 Like

Is it possible to see that intermediate ASM Codes in the Arduino IDE Environment?

Yes it is.

In which path/folder it is saved?

I am completely fresh at this subject ,can you help to doing that ? or even to send me print screen of what should i need to do ?

Enable the full compiler output, and look there for the clues.

1 Like

Then your assignment is a waste of time.

we work at tinkercad

You can download and run the IDE on a PC. Compared with the several days you say you've been at this, it isn't going to take much time.

This is a school assignment, right? Did you complete any similar assignments before this one?

That's what compilers are built for.

If this is for school, I told you "how to cheat". A school assignment would expect you to understand the code. So don't be too sheepish, do what I said and either

  1. Take my advice and cheat, don't ever become a programmer
  2. Follow the lesson and learn
2 Likes

That task should be pretty easy for the posted example, if you have studied the instruction set and mnemonics.

Post your best attempt and describe where you are having the difficulties.

I provide few samples of C codes and the corresponding ASM Codes (hand assembled using the attached Instruction Set Manual ). Check if you can follow them:
Atmel-0856-AVR-Instruction-Set-Manual.pdf (2.4 MB)

1. Data Movement Instruction
C++ Code:
byte y = 0x35;

Assembly Code:

ldi r16, 0x35
lds 0x0120, r16         ; an arbitrary RAM location (see Fig-1 below)

or

ldi r27, 0x01
ldi r26, 0x20 ; <r27, r26> register pair now works as X-pointer register
ldi r16, 0x35
st x, r16 ; content of r16 reg goes to a location pointed by x

(2) Arithmetic Instructions
C++ Code:

byte x1 = 0x12, x2 = 0x45;
byte sum = x1 + x2;

Assembly Code:

ldi r16, 0x12
ldi r17, 0x45
add r16, r17

(3) Logical Instructions
C++ Code:

byte x1 = x65;
byte x2 = 0x73;
byte y = x1 | x2;

Assembly Code:

ldi r16, 0x65
ldi r17, 0x73
or r16, r17

(3) Branch Instructions
C++ Code:

byte x1 = 0x65;
byte x2 = 0x45;
if( x1 != x2) //Skip (do not execute) next instruction if they are equal
{
    L1: //do this tasks
}
else
{
   L2: //do this task
}

Assembly Code:

ldi r16, 0x65
ldi r17, 0x45
cpse r16, r17 ; compare two regs and skip  next instruction if regs are equal
L1: rjpm L3
L2: rjmp L4
L3: --------------
-------------------------
L4: --------------------

RAMSpace
ram
Figure-1:

2 Likes
ldi r16, 0x35
sts 0x0120, r16   ;; corrected
1 Like

Your fun could begin here

1 Like

Better just code it yourself. You will not be pleased with the asm from a C compiler. Personal experience.

1 Like

Compilers have options to spit out assembly code.
Check which compiler you use and set the option in the compiler arguments, e.g. -S for gcc:
GCC assembly code
I think, it stops after assembly code generation, it does not generate executable code.

Why do you want to do it?
(do you want to check if your compiler works properly? - tough to judge, better trust your compiler)

Also, bear in mind: if you set optimization level (e.g. -O3) - you might see very hard to understand code.

I add sometimes assembly code to my project (e.g. this startup.s code, or I add very tiny functions, e.g. for "fast_memcpy"). But this needs more to understand the ARM instruction set, how the a(GCC) assembler works in terms of keywords and syntax to use.
But I have never use assembly code listing just to check if my compiler did it right.
When it comes to debugging: sometimes I had to step through assembly code, but just important to understand the ARM instructions.

C language was invented to avoid to program in assembly code. It is efficient in a similar way (C code is "machine coding" on a bit higher abstraction layer). Who would program in assembly?
I do sometimes, but just for very small piece of code and just to increase performance of my system (C code is already very like assembly code, even in terms of speed).

BTW:
When using ARM compiler (GCC, ARM CC) - they follow often the "ABI reference":
they use specific registers as input parameters (R0..R3) and return parameter (R0) and some other registers are reserved for "interlink registers" (to connect two functions calls or code).
You have to understand also this ABI convention when mixing C with assembly code.

Any compiler (any language, any CPU) has to have some specified ABI for its functions to follow. Otherwise you could never write assembly language subroutines to implement things that were impossible to implement in the language.

Examples (using Instruction Set Manual):
Atmel-0856-AVR-Instruction-Set-Manual.pdf (2.4 MB)

1. Addition Instruction without Carry