AVR Asm bad code at the internet

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

Today I was explaining to a AVR ASM novice about math asm routines, while he told me one unsigned mult 8x8 routine he copied from the net was failing all the time.

Reading his routine from the net, I noticed it was to fail completely.  Then I start to dig google for 8x8 routines and found the next, that also fails.

I can not understand how a "coder" can post a routine with quantity of cycles and instructions, etc, looking like as a nice tested routine, but doesn't work at all.

What should we do? contact the coder to fix it? or just ignore it, and leave the bait for other novices end up with headaches for not understanding why it fails?

 

One example:

 

http://elm-chan.org/docs/avrlib/...

 

mul08u:		sub	var11,var11	;initialize variables
		ldi	lc,9		; lc = 9;
		brcc	PC+2		;---- calcurating loop
		add	var11,var20	;
		ror	var11		;
		ror	var10		;
		dec	lc		;if (--lc > 0)
		brne	PC-5		; continue loop;
		ret

 

You don't even need to read all the routine to see it will fail.

Just see the two ROR instructions over the result right before it could end the loop by "lc" reaches zero.

It means that the result will always be half of the real result should be.

If you are multiplying 0x01 times 0x80, the result will be x040.

Although, I like the idea of a ROR following the ADD, since it also retrieves the lost carry bit into the shift.

 

 

 

Wagner Lipnharski
Orlando Florida USA

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

Bad code on the internet? Surely not!

 

surprise

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

Chan does pretty good stuff, so it is "interesting".  And indeed might be worth following up.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

wagnerlip wrote:

If you are multiplying 0x01 times 0x80, the result will be x040.

I poked the routine into a Studio app, and ran it in the debugger. After calling the routine with 0x01 x 0x80 the result is 0x80.

                     .DEF var10=R16
                     .DEF var11=R17
                     .DEF var20=R18
                     .DEF lc=R19

...

 

 

Lessee -- 5 x 25 is 125.  0x05 x 0x19 => 0x7d...

 

Results look OK to me.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

The Chan 8x8 code appears to work, fine...tried it with several values.  Appears the intermediate calcs are being done 1 position to the left...so the final shift puts into the correct position. 

When in the dark remember-the future looks brighter than ever.

Last Edited: Tue. Mar 13, 2018 - 09:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It seems to be the case.

I can think that as the shift is to the right, and the low value is added to the high result var, it really needs 8 shifts to escape the high result var and fall completely into the low result var.  So the last shift is necessary. 

Wagner Lipnharski
Orlando Florida USA

Last Edited: Tue. Mar 13, 2018 - 09:53 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AVR200 is the same size (9 words), same register requirements (4, 1 high), but 6 cycles faster (8 passes of the loop instead of 9, but one extra shift before entering the loop)

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

The Internet is typed by monkeys who do not know Shakespeare.

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

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

Is there a way to clear the internet's cache & start fresh?  Perhaps a once a year cleanup routine & archive anything not being used.

When in the dark remember-the future looks brighter than ever.