Problem with assembly in attiny85

Go To Last Post
37 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,
I am working on a project for a university exam where I have to use the attiny85 to manage the ultrasonic sensor hc-sr04 and to switch on and off two leds based on the distance measured by the hc-sr04.
The problem is that in the C language I have to include some lines of code in assembly and I am at the very beginning and I understand very little about assembly. I use arduinoIDE as a compiler and can someone tell me why I am getting these error messages?

 

C:\Users\Xeno\AppData\Local\Temp\ccI2TlXn.s: Assembler messages:
C:\Users\Xeno\AppData\Local\Temp\ccI2TlXn.s:350: Error: constant value required
C:\Users\Xeno\AppData\Local\Temp\ccI2TlXn.s:373: Error: constant value required
C:\Users\Xeno\AppData\Local\Temp\ccI2TlXn.s:396: Error: constant value required
C:\Users\Xeno\AppData\Local\Temp\ccI2TlXn.s:496: Error: constant value required
C:\Users\Xeno\AppData\Local\Temp\ccI2TlXn.s:497: Error: constant value required
C:\Users\Xeno\AppData\Local\Temp\ccI2TlXn.s:530: Error: constant value required
C:\Users\Xeno\AppData\Local\Temp\ccI2TlXn.s:531: Error: constant value required
C:\Users\Xeno\AppData\Local\Temp\ccI2TlXn.s:552: Error: constant value required
C:\Users\Xeno\AppData\Local\Temp\ccI2TlXn.s:553: Error: constant value required
lto-wrapper.exe: fatal error: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc returned 1 exit status
compilation terminated.
c:/program files/windowsapps/arduinollc.arduinoide_1.8.49.0_x86__mdqgnx93n4wtt/hardware/tools/avr/bin/../lib/gcc/avr/7.3.0/../../../../avr/bin/ld.exe: error: lto-wrapper failed
collect2.exe: error: ld returned 1 exit status
exit status 1
Errore durante la compilazione per la scheda Attiny 85.

 

And this is the code

#define TEMPERATURA_PIN 2
const int trigger=3; 
const int echo=4; 
const int LED= 0;
const int LED1= 1;
float dist;
void setup(){ 
  pinMode(trigger,OUTPUT);
  pinMode(echo,INPUT);
}
void loop(){
  int input = analogRead(TEMPERATURA_PIN);
  float temperatura = input / 2.046;
    asm("cbi PORTB, PORTB3 \n");    //digitalWrite(trigger,LOW);
  delayMicroseconds(5);        
      asm("sbi PORTB, PORTB3 \n");  //digitalWrite(trigger,HIGH);
  delayMicroseconds(10);     
      asm("cbi PORTB, PORTB3 \n");  //digitalWrite(trigger,LOW);
  dist=pulseIn(echo,HIGH);      
  dist = dist/58;               // conversione in centimetri
  float velocita = 0.03314 + 0.000062 * temperatura;
  float distanza = velocita * dist / 2; // Si quisieramos convertirlo a pulgadas, dividimos entre 148.
   if(dist < 10){
        asm(
          "sbi PORTB, PORTB0 \n"  //digitalWrite(LED, HIGH); 
          "cbi PORTB, PORTB1 \n"  //digitalWrite(LED1, LOW);
          ); 
  }  
  if(dist >=  10 && dist < 100){
          asm(
            "cbi PORTB, PORTB0 \n"   //digitalWrite(LED, LOW);
            "sbi PORTB, PORTB1 \n"   //digitalWrite(LED1, HIGH);
            ); 
  }
  
  if(dist >= 100 ){
       asm(
        "sbi PORTB, PORTB0 \n"   //digitalWrite(LED, HIGH); 
        "sbi PORTB, PORTB1  \n"  //digitalWrite(LED1, HIGH);
        ); 
  }             
}     

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Take a look at the manual...

 

https://www.nongnu.org/avr-libc/...

 

but what do you see as the advantage of using Asm here anyway? I you have:

        asm(
          "sbi PORTB, PORTB0 \n"  //digitalWrite(LED, HIGH); 
          "cbi PORTB, PORTB1 \n"  //digitalWrite(LED1, LOW);
          ); 

you can achieve the same in C using:

PORTB |= (1 << PORTB0);
PORTB &= ~(1 << PORTB1);

that will generate the single SBI and CBI you are looking for anyway without resorting to asm().

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

 

"but what do you see as the advantage of using Asm here anyway?

i know there are no advantages but my teacher wants us to use assembly language wherever possible.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Xeno_0101 wrote:
my teacher wants us to use assembly language wherever possible

So why are you writing in C, and kludging your assembler as "inline assembler" ?

 

Surely, that makes no sense?

 

This means you not only have to deal with assembler, but also all the arcane rules for inline assembler in GCC.

 

Why not just write directly in assembler?

 

https://www.avrfreaks.net/commen...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Xeno_0101 wrote:
i know there are no advantages but my teacher wants us to use assembly language wherever possible.
The maybe consider doing the whole code in Asm in a .S source file? It is far easier than trying to do battle with the archaic asm() interface.

 

The issue you have is that (simplistically) the bit you put in quotes in the asm() statement is just passed "as is" to the assembler. Things like "PORTB" and "PORTB0" are C pre-processor symbols but they are not expanded within the "" of asm().

 

Now I did say it was simplistic to say it is passed directly. In fact the asm("...") has it's own kind of parsed syntax and beyond the "..." you can pass some other things that can be fed into the string parsing but this is a new syntax you have to learn and to get PORTB and PORTB0 into the text of the asm string will involve reading that manual I linked to previously.

 

Really, .S is a much better way to approach this - don't forget to do the "__SFR_OFFSET 0"  trickk if you do as it makes things even easier ! Also .S (unlike .s) files are passed through the pre-processor so it can handle things like #include <avr/io.h> and the use of symbols like PORTB and PORTB0.

 

EDIT: Oh I see - you have a mix of Asm and float. It would be wise to keep the float stuff in C (it's much trickier to interface to direct from Asm!) but maybe either call to C routines from the Asm or to Asm routines (in separate .S) from the C.

 

Oh and if the Asm thing is about making things fats/efficient that doesn't make a lot of sense when float is also involved. The best optimization for this might be to try and do the float calculations in integer maths!

Last Edited: Wed. Sep 8, 2021 - 09:54 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

 

Why not just write directly in assembler?

 

 

I don't know why, I just know that my teacher taught all students to write like this ... also because for us writing everything in assembly would be very complicated since it is a topic that we touch for the first time

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Xeno_0101 wrote:
my teacher taught all students to write like this

seriously?!

 

surprise

 

 for us writing everything in assembly would be very complicated 

Far less complicated than messing about with inline assembler!

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

Far less complicated than messing about with inline assembler!

 

so far I have programmed only in C and Java, for me the assembly is an "alien" language, in fact from the links you send I am not understanding anything

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I strongly believe that teachers should not be so foolish.

 

Surely it is best for students to learn how to use C and C++ proficiently.

 

This will give you good knowledge and experience for further studies or future career.

 

Yes,  at the end of your curriculum she might show some examples where ASM would have been faster or more efficient.

 

The most important lesson is that the "ASM snippet" is seldom appropriate in most applications.   But it does have occasional uses.

 

You would take this "general knowledge" with you.   In much the same way that a music student learns about composing music but has an "awareness" of Mozart, Bach, ... and that they are possibly in a different league.

 

David.

Last Edited: Wed. Sep 8, 2021 - 10:15 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Xeno_0101 wrote:
for me the assembly is an "alien" language

so don't further alienate it with all the added complications & restrictions of inlining it in C.

 

Your teacher's job is to teach you this stuff - so, if you're stuck, you really should be asking her/him for help.

 

You might point her/him to this thread - (s)he really is making things unnecessarily difficult.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


By the way the inline asm "cookboook":

 

https://web.stanford.edu/class/e...

 

has this at the very end:

 

 

which is exactly the problem you are suffering from in this thread. In their example they have hard coded the 0x07 to select which bit is to be set. But you could pass in PORTB3 or whatever as a %1 parameter perhaps?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

he really is making things unnecessarily difficult.

I know but what can I do? the guidelines for the exam are these and some students have already done so. Unfortunately, besides asking you for help, there is nothing I can do

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

he really is making things unnecessarily difficult.

I know but what can I do? the guidelines for the exam are these and some students have already done so. Unfortunately, besides asking you for help, there is nothing I can do

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

By the way the inline asm "cookboook":

 

https://web.stanford.edu/class/e...

thank you very much, I will definitely take a look at it.
Anyway I wanted to ask one thing, would you know how to translate:

delayMicroseconds(5); 

into assembly inline?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You may also want to go through this one Atmel AT1886: Mixing Assembly and C with AVRGCC

 

Xeno_0101 wrote:

Anyway I wanted to ask one thing, would you know how to translate:

delayMicroseconds(5); 

into assembly inline?

Google AVR delay loop calculator. That is a quick way to do it for one off thing.

 

But if you want to learn then read the AVR instruction set and try to make one for yourself.

“Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?” - Brian W. Kernighan

Last Edited: Wed. Sep 8, 2021 - 01:58 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

 

Xeno_0101 wrote:
into assembly inline?
Well normally you would ensure the ELF file was built with a -g option (embed debug info) and then you would use avr-objdump -S on the ELF file to have it produced a mixed C+Asm listing then you can see exactly what the compiler generated for that delay but as this is Arduino (a) accessing the ELF is tricky, (b) I'm not sure it's built with -g, (c) even if it were I think delays may not be soft loops but using a timer interrupt counter (though that may only be for ms not us??)

 

But anyway here's an interesting web site:

 

http://darcy.rsgc.on.ca/ACES/TEI...

 

You can put in the parameters for a timer (for CPU speed I think you can assume 16000000 if this is an Arduino) and it will auto-generate an Asm sequence to delay that long.

 

But when you think about it 5us is not very long on a 16MHz micro. At 16Mhz one machine cycle is 1/16000000s which is 62.5 nanoseconds. So to delay for 5us you need to delay for 80 cycles. One sledgehammer to crack a nut way to do that would be to code 80 NOPs as one NOP takes 1 cycle so 80 of them would take 80 cycles which would be 5us when a cycle is 62.5ns. More usually you would encode some kind of loop.

 

The website I linked to above said:

but now you are going ask how to do that L1: branch stuff in inline asm - good luck with that!!

 

EDIT: oh wait a minute I just found out what the "avr-gcc" button on that page does:

 

 

so it even generates inline Asm !!

 

(be careful about what register is used in this - the compiler needs to know and that is what "clobber lists" are used for.

 

(again who ever thought using inline Asm was a good idea ?!?)

Last Edited: Wed. Sep 8, 2021 - 02:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
(again who ever thought using inline Asm was a good idea ?!?)

Also I think if anyone really has the need for speed, calling a subroutine from C code is much better/elegant, writing assembly in .asm file and including that into C program.

“Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?” - Brian W. Kernighan

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello, 

   I believe that your teacher means that assembler should be used when absolutely necessary.  And, that assembly language can be inserted into Arduino code using the inline-assembler techniques as shown,  when it is necessary.

 

  Assembler is necessary when the code must move data with very precise and exact timing.  The only time that I have ever used inline-assembler is when programming WD1228 addressable LEDs (NeoPixels).  The data for this chip must be presented in exact microsecond timings.  A data '0' is 0.4 us high, followed by 0.8 us low.  A data '1' is 0.8 us high, followed by 0.4 us low.   Assembler is the only way to do this on an AVR.  See the Adafruit library for NeoPixels for examples of how this is done using inline-assembler for Arduino.

 

   The code that you have presented should work OK.  But you should use all the digitalWrite() and digitalRead() statements that you have commented out.

 

  I use the HC-SR04 as an input device.   The ultrasonic pulses can bounce off the palm of your hand so you can use your hand to set the distance value.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Xeno_0101 wrote:

delayMicroseconds(5);

 

That may be not enough, put 10 or 20us.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

 

But anyway here's an interesting web site:

 

http://darcy.rsgc.on.ca/ACES/TEI...

 

I do not know how to thank you..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Simonetta wrote:

Hello, 

   I believe that your teacher means that assembler should be used when absolutely necessary.  And, that assembly language can be inserted into Arduino code using the inline-assembler techniques as shown,  when it is necessary.

in your opinion when is it necessary to use assembly in my code?
my teacher told us we have to write at least 10 lines of assembly code..so i don't know how to do it

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Xeno_0101 wrote:
in your opinion when is it necessary to use assembly in my code?

It was in the post:

Simonetta wrote:
  Assembler is necessary when the code must move data with very precise and exact timing. 

Also when you have to achieve absolute minimum timing and/or code size - without any of the higher-level language overheads.

 

There are a few other cases where you need to do things which are just far simpler in assembler, or even impossible in a HLL

 

my teacher told us we have to write at least 10 lines of assembly code..so i don't know how to do it

If your teacher told you that, then  (s)he thinks you must have had  sufficient information & preparation from your classes, set reading, etc ...

 

If that's not the case, then you really need to go back to your teacher to get her/him to explain more/better

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Xeno_0101 wrote:
my teacher told us we have to write at least 10 lines of assembly code..

What an arbitrary & pointless requirement.

 

frown

 

I'm sure you could use up your 10 lines in making a delay function - then just do everything else in C ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here are some reasons not to use inline-assembly:https://gcc.gnu.org/wiki/DontUse...

 

Here is long and involved guide for using inline-assembly in general: https://web.stanford.edu/class/e...

 

Here is an example (about half-way through the code listing) of how inline-assembler fits in an Arduino sketch: https://github.com/adafruit/Adaf...

 

I believe that your assignment is to demonstrate that you can mix AVR assembler with Arduino C++ by putting a "micro-demo" program into an Arduino sketch.   This "micro-demo" needs to be at least 10 AVR instructions long. 

 

  You ....could....maybe.... get at least a 'pass' grade on the assignment by putting in ten NOP statements into the sketch using inline asm format.

 

At worst, just cut-and-paste this code below into your  Arduino sketch and hope for the best:

void delay(uint8_t ms) {
uint16_t cnt;
asm volatile(
"\n"
"L_dl1%=:" "\n\t"
"mov %A0, %A2" "\n\t"
"mov %B0, %B2" "\n"
"L_dl2%=:" "\n\t"
"sbiw %A0, 1" "\n\t"
"brne L_dl2%=" "\n\t"
"dec %1" "\n\t"
"brne L_dl1%=" "\n\t"
: "=&w" (cnt)
: "r" (ms), "r" (delay_count)
);
}

 

 

Last Edited: Wed. Sep 8, 2021 - 04:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
I'm sure you could use up your 10 lines in making a delay function - then just do everything else in C ...

Simonetta wrote:
At worst, just cut-and-paste this code below into your  Arduino sketch and hope for the best:

void delay(uint8_t ms) {
   uint16_t cnt;
   asm volatile(
      "\n"
      "L_dl1%=:" "\n\t"
      "mov %A0, %A2" "\n\t"
      "mov %B0, %B2" "\n"
      "L_dl2%=:" "\n\t"
      "sbiw %A0, 1" "\n\t"
      "brne L_dl2%=" "\n\t"
      "dec %1" "\n\t"
      "brne L_dl1%=" "\n\t"
      : "=&w" (cnt)
      : "r" (ms), "r" (delay_count)
   );
}

yes - a delay function ! laugh

 

however, that seems to be only 8 actual assembler lines - the last 2 (highlighted) are just compiler directives ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Go on.   It is criminal to introduce Gobbledygook.   Learning a few regular ASM opcodes is pointless enough.

 

At least the official ASM opcodes and directives are supported by the microcontroller manufacturer.

The world does not revolve around Gnu.

 

Your teacher should have shown some ASM examples.   If she didn't it is a bit of a clue that ASM is seldom appropriate in User applications.

 

Yes,  she could have pointed to several C Library functions that are implemented in ASM.   Straight ASM e.g. .S files.

But as a User you are not concerned with how low level library functions are made.   You just need to know how to use them.

 

Or she could have pointed to some specific timing sensitive hardware.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0



https://web.stanford.edu/class/e... says:

asm("in %0, %1" : "=r" (value) : "I" (PORTD) : );

And

 

Are both WRONG, aren't they?   PORTB would be evaluated by C before doing the asm, passing in the current value of PORTB instead of the address of PORTB as needed by the in or sbi instruction?  (maybe asm() behaves or did behave differently?)   Even then, there's the whole "memory addresses vs io addresses" issue that is apparently being ignored.   In any case, neither example will even compile with a recent avr-gcc.

 

https://www.nongnu.org/avr-libc/... has what I guess is more recent info, and suggests:

asm("in %0, %1" : "=r" (value) : "I" (_SFR_IO_ADDR(PORTD)) );

 

That means that to get the desired:

sbi PORTB, PORTB0

Would require C code that looks like:

asm("cbi %0, %1 \n" :: "I"(_SFR_IO_ADDR(PORTB)), "I"(PORTB0));

 

That appears to be accepted and produce correct code:

  asm("cbi %0, %1 \n" :: "I"(_SFR_IO_ADDR(PORTB)), "I"(PORTB3));    //digitalWrite(trigger,LOW);
   0:	c3 98       	cbi	0x18, 3	; 24

That, obviously, is quite obscure and verbose.  It bears very little resemblance to actual avr asm code, and is more an exercise in struggling with the compiler than in learning anything about avr.  (OTOH "struggling with the compiler" is an important real-world skill...)

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why not just do it in asm?  With all the registers avail (0-31), it is especially easy.  Often doing so really forces you to think thoroughly through the details of what really needs to happen.  With that detailed insight, it is also easier to later appreciate what equivalents can be more easily done in a higher level language.   We are talking about a short asm program here, not pages and pages of code, so saying asm vs C will take too long is rather moot.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:
Why not just do it in asm? 

indeed - see #4, #5, #7, #10, #16, #17, ...

 

But OP seems insistent  

 

frown

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Xeno_0101 wrote:
some students have already done so

So why aren't you talking to them, then?

 

Xeno_0101 wrote:
besides asking you for help, there is nothing I can do

Not at all: You could ask your teacher, and you could ask your classmates; both are better placed to answer, as they know the course you're on, the exam guidelines, etc - we known none of that.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Why not just do it in asm? 

  float velocita = 0.03314 + 0.000062 * temperatura;
  float distanza = velocita * dist / 2; // Si quisieramos convertirlo a pulgadas, dividimos entre 148.

The use of floating point would stop ME from doing it in ASM.

 

Of course, whether one needs floating point or not is another question.

 

The whole mix of "Arduino=isms" with "use some asm" is quite strange.

 

 

Last Edited: Thu. Sep 9, 2021 - 07:46 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

westfw wrote:
The use of floating point would stop ME from doing it in ASM.

As already noted, you'd do the FP stuff in C (if you really needed it at all) and keep the ASM stuff in proper ASM .S source files - and link them together.

 

Of course, whether one needs floating point or not is another question.

Indeed.

 

The whole mix of "Arduino=isms" with "use some asm" is quite strange.

quite bizarre!

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

westfw wrote:

sbi PORTB, PORTB0

Would require C code that looks like:

asm("cbi %0, %1 \n" :: "I"(_SFR_IO_ADDR(PORTB)), "I"(PORTB0));

why do you go from "sbi" to "cbi"? Or were you just wrong?

However thanks to this I got far fewer errors

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Xeno_0101 wrote:
why do you go from "sbi" to "cbi"? Or were you just wrong?
Just wrong. SBI if you want to set the bit to 1, CBI if you want to clear it to 0.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

Just wrong. SBI if you want to set the bit to 1, CBI if you want to clear it to 0.

yes I know this .. what does the "_SFR_IO_ADDR" instruction mean?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Have a read of the user manual...

 

https://www.nongnu.org/avr-libc/...

 

also worth knowing about:

 

https://www.nongnu.org/avr-libc/...

 

(which the first page links to).

 

If you decide to go for a standalone .S Asm file solution I highly recommend the __SFR_OFSET = 0 thing they talk about in the second page even if it does say:

 

but it's a hack, so it is recommended to change your source

 

!!!