Avr assembly arrays?

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

I have use the search but I can not find any tutorial or any example with step by step instructions how to build arrays in assembly.
Actually I want a 2 dimension array in eeprom
the x should have 90 cells and y should have 20 cells and then with a pointer i have to choose the correct cell and read the value.
Any documents with a how to?
Thank you

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

Ptr = base address + x* sizeof x + y

In your instance sizeof x is 90

So you need to do a multiply and some adds. Welcome to assembly programming! Using C makes all of this much easier.

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

Thank you for answer me
Probably with c all the mathematics and arrays is easy but with c is more difficult for me to use commands like sbi cbi etc so I decide to keep in assembly.
I hope someone have to give me a good example in assembly arrays.

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

You don't need to use sbi and cbi in C, the compiler does it for you.
PORTC |= (1<<2) in C will generate a sbi instruction.

If you want to be proficient in asm, you need to know how to add and multiply. If you use a mega, it has a multiply instruction. Here are the steps
1. Multiply your x value by 90
2. Add your y value to the result
3. Add your base address to the result
4. Do eeprom read/write usinf the result as the address
Job done

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

Thank you a lot
Ok with pointer and it is easy because I use mega1284 so I have multiply.
For tables now until now I use something like this
.ESEG
.org $0000
.db 1,2,3,4,5,65,6,7

So I have one dimension table and simple add the value to x and take the correct value from my lookuptable.
For 2 dimension how it is look like?

For example the one dimension named
rpm:
.db 5,10,15,20

and the other dimension
volt:
.db 1,3,4,5

Looks like that?
I

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

For a 5x by 2y table:
.db 1,2. ;x[0]
.db 3,4. ;x[1]
And so on

Actually i made a mistake earlier. For step 1, multiply x by sizeof y in your case 20

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

Looks like you are wanting to look up ignition advances for rpm and pressure sensor readings??? Assuming it is...(adjust the words below if not)

So you have 1800 cells arranged as 90x20...and you want to look up the value in the cell that most closely represents the rpm and sensor readings?

So in addition to the 1800 cells you have a list of 90 rpm values and 20 pressure values. You need to walk through the lists for your current readings to see that, for example the 27th rpm value and the 15th pressure value is needed - so element (15*90)+27 is needed. (may be different if you consider your axes start at 0 or 1, and how you count into them)

To be more clever, you could find the 4 array elements that surround the measured values and do a 2D linear interpolation...

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

Actually yes the first dimension is the rpm (x) from engine and the other is tps (y).
In my program I read first rpm and after that tps then I have to read the correct cell in my array and read or write the value

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

If you can afford to lose some memory space, or to use the space between unused cells for something else and you start your array from zero, you could just concatenate the x and y address bits.

 PORTC |= (1<<2)

looks more complicated than

 SBI PortC, 2
or
 PortC.2 = 1

George.

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

petros047 wrote:
Actually yes the first dimension is the rpm (x) from engine and the other is tps (y).
In my program I read first rpm and after that tps then I have to read the correct cell in my array and read or write the value
...in that case I have given you hints on exactly what you need to do :D

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

Quote:
I use mega1284

What makes you program it in asm? This chip runs at 20MHz, has 16kB SRAM, 128kB of flash and you are writing in asm? You will not be able to fill a single flash page with opcodes!

Quote:
you could find the 4 array elements that surround the measured values and do a 2D linear interpolation...

Of course keep away from floats.

My suggestion is rather radical, but I guarantee you will not find a faster soltuion: just make a 64kB LUT and no interpolation is ever needed.. :)

No RSTDISBL, no fun!

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

My program simple read rpm,tps,wide band o2 sensor and read or write a cell of total 3 arrays of 2 dimension (1 array is what μc read,2 array is the data from user who upload it with rs232 to μc ,and the 3 array is the afr.
I dont know very well c language actually i know only the basics,so I have two options or to read and learn c and make it on c or to learn better the asm and do it in asm.
Also if someone can post a sample of this type of array it will help me a lot.
I understand about the pointer but I am still missing how to build the array.

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

Quote:
I understand about the pointer but I am still missing how to build the array.
What?
Kartman showed you in his third post!

--greg
Still learning, don't shout at me, educate me.
Starting the fire is easy; the hardest part is learning how to keep the flame!

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

What do you mean by 'build the array'? You only need to allocate enough space in which to store it. Did I not show this??

Learning C is far easier than assembler.

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

I have NEVER thought about ARRAYS in ASM, they really DO NOT EXIST even in C, it's a figment of one's immagination. :)

All values are arranged one after another in a linear fashion. So if you have a 2x10 "array" say located at 0 then the first "element" is from 0-9, the 2nd "element" is located from 10-19.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Kartman wrote:
Ptr = base address + x* sizeof x + y

Sure you don't mean Ptr = base address + y * sizeof x + x?

Quote:
Welcome to assembly programming! Using C makes all of this much easier.

Amen to that.

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

I have read the post for the example array but I didnt understand.
The first .db is for example the x dimension and every second line is the y dimension?

js wrote:
I have NEVER thought about ARRAYS in ASM, they really DO NOT EXIST even in C, it's a figment of one's immagination. :)

All values are arranged one after another in a linear fashion. So if you have a 2x10 "array" say located at 0 then the first "element" is from 0-9, the 2nd "element" is located from 10-19.

I agree.

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

Quote:
they really DO NOT EXIST even in C

And the most funny thing is literally all semiconductor memories are physically organized as 2D :)

Quote:
The first .db is for example the x dimension and every second line is the y dimension?

Do you think it really matters if x is a row and y is a column or opposite? .db macro just puts them one after the other so:

Quote:
.db 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15

gives the same effect as
Quote:
.db 1,2,3,4,5
.db 6,7,8,9,10
.db 11,12,13,14,15

And it still is the same as:
Quote:
.db 1
.db 2
.db 3
.db 4
.db 5
.db 6
.db 7
.db 8
.db 9
.db 10
.db 11
.db 12
.db 13
.db 14
.db 15

All three take same space and same sequence.
So if you want to access the one from third row and fourth column (I guess it is the one in bold),
then it is (row-1)*5+column=(3-1)*5+4=?.

No RSTDISBL, no fun!

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

Brutte wrote:
Quote:
The first .db is for example the x dimension and every second line is the y dimension?

Do you think it really matters if x is a row and y is a column or opposite? .db macro just puts them one after the other so:

Quote:
.db 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15

gives the same effect as
Quote:
.db 1,2,3,4,5
.db 6,7,8,9,10
.db 11,12,13,14,15

And it still is the same as:
Quote:
.db 1
.db 2
.db 3
.db 4
.db 5
.db 6
.db 7
.db 8
.db 9
.db 10
.db 11
.db 12
.db 13
.db 14
.db 15

All three take same space and same sequence.
So if you want to access the one from third row and fourth column (I guess it is the one in bold),
then it is (row-1)*5+column=(3-1)*5+4=?.

But certainly for readability and maintainability one would choose a source format that matched the array dimensions, if possible.

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

Quote:
one would choose a source format that matched the array dimensions, if possible.

Up to 2D.

Nice feature of C is you do not need to give first dimension in table declaration. All other dimensions are mandatory.
For lower triangular it gives:
row*(row-1)/2+column.
Here none of the sizes is needed to get the data :)
C does not support triangular ones.

.db 1
.db 2,3
.db 4,5,6
.db 7,8,9,10

No RSTDISBL, no fun!

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

Ok now I have clear it in my mind.
Thanks for help soon I will make a sample program and test it.
I will post it how I make it.

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

Quote:
C does not support triangular ones.

Perhaps, not, 'out of the box'. But you can model any data structure in 'C' that you'd like to. Consider, a two dimensional array is still just a sequential segment of memory (hopefully), as is any other dimensioned array. you simply (or not so simply) need to design the algorithm to access any particular element.
I suppose asm is exactly the same.

--greg
Still learning, don't shout at me, educate me.
Starting the fire is easy; the hardest part is learning how to keep the flame!