As it seems you are new to assembly programming I would suggest to start with a couple of simple programs.

interestingly that you use an antique processor like the Mega128... school project? or self learning with school materials?

I have not used the Meag128 for a long time, but I do recall things might go horribly wrong when you set the fuse bites incorrect, so hope for school project were that is handled for you.

Perhaps some other freak can enlighten on that when you do start from scratch and also have to set the fuses.

first I would start with reading the datasheet, you seem to have copied an initialization sequence from somewhere (stack initialization)

I would suggest to first play a bit with the leds individually. So make a small program that turns the LEDs all off

then a second program that turns them all on, followed by one that has the LED blink with a certain interval.

this way you get some basic programming feeling and later on you have a debugging interface to your disposal (normally one starts with just 1 led, but hey you got 7 in one go so have 7 stages of information for debugging.

with the last step you can also see if the processor is running at the speed you think it is running at.

with uart failure that is the most common problem. You think the processor is running at frequency X but actually it is running at frequency Y.......

"If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

Do you have any experience with any other microcontrollers? Or with programming in general?

and i have to do a code

Why do you "have to"?

As already noted, is this a school project/assignment?

for transporting "something" from Visual Studio to AVR (show it on 8 Led 7 Segments display)

An important thing to understand is that these are really 2 entirely separate projects:

To Visual Studio, the AVR is irrelevant.
Your VS code will simply send to some standard PC port - it neither knows nor cares what might be attached to that port in the outside world.
Therefore your VS project can be defined as: "Send something to some port".

To the AVR, Visual Studio is irrelevant.
Your AVR code will simply receive from some AVR port - it neither knows nor cares what might be attached to that port in the outside world.Therefore your AVR project can be defined as: "Receive something from some port".

This is important to understand, as it allows you to break down the project into manageable chunks.

A common beginner's mistake is to try to do everything all at once, then just switch it all on and hope it all works.

But, when it doesn't work, you don't know if that's because the sender isn't sending properly, or the receiver isn't receiving properly, or your connections are faulty - or a combination of the lot!

ldi UCSR0B, 0b00011000;initialization what i have found that is necessary to do (i've tried to do it, but nor sure if it's right)
ldi UCSR0C, 0b00000011; -||-
ldi UCSRnA, 0b00000010; -||-

For anyone (and that includes you in a few weeks) to know what this is doing you would have to get out a copy of the datasheet and lookup what those two bits in UCSR0B are. Code like this is easier to read if you use something like:

That now is clearer about what bits are being set. HOWEVER this code is complete nonsense. You cannot use LDI like that. The first parameter of an LDI opcode can only be a register number between R0 and R31. You cannot LDI to a peripheral registers like UCSR0B in a single AVR opcode. This is a RISC processor and what can be achieved in a single opcode is quite limited so this code would be something more like:

which is "closer" but it is still wrong because there is no register called UCSRnA. When the datasheet talks about registers with lower case 'n' in the name it intends you replace 'n' with the number of the UART you are trying to program. So UCSR0A or UCSR1A. So the code would actually be:

ldi r16, (1 << U2X0); -||-
out UCSR0A, r16

If you had tried to build the code you presented you presumably already saw several errors such as this one and it saying "there is no such thing as UCSRnA"? Also you will have seen errors because you tried to us UCSR0B etc in LDI instructions.

Any way the C and then Asm I showed at the top of this is the very most basic stuff you need to make the UART work. Replace the 51 in it with the right number for baud rate and CPu clock frequency you plan to use.

Hi, thanks for all your responses. Yes it is school project, we have to use atmega128, we have to use AVR studio4 and Yes we have to do it with assembly language. (like we are also programming on 15-20 years old computers :/...). I will try to do something with your advices.

Well, I suppose asm can support comments (and ignore them) and macros, easier to read and to detect typos... .

What might be interesting would be that magical binary numbers be macros : you, your friend(s) and maybe your teacher would be happier, in the long term.

Another interesting feature would be that one knows how PORT B, say, is connected to a 7 segment display /digit and have some description of your hardware (say PORTB, 0 is connected to digit a....) (if this is part of your source, you might want to reuse it and adapt it: maybe your teacher, if -s-he reads code, would be appealed with the idea of making code easier to read/reuse/adapt). BTW : it is recommended, but not compulsory, comments are in American, in France (I do not know how they should be written in Czech Republic or Slovakia or...)...

Edited : is jmp an unconditional jump? Then, what is the use of comparing? (would be faster without comparing? a lot of lines would disappear with the same logic) I am very ignorant w/r assembly.

Thanks you Clawson (I was really puzzled : it looked like a switch ; maybe, if asm is array aware, he should have computed an index -with bound checking- and deduced R17, -R18 seems constant, if numbers are valid ones -or -s-he should have used c and activated the option --save-temps)

The way it is wrtitten, it can be optimised a lot, making it faster...

OTOH, if -s-he wants to use a conditional switch, there are 10 typos to fix... and it cannot be a mouse operation...

BTW : '0' -nula- is transported, if R7 does not vary, as 1110 0000 == 0xd0 0xe0; ASCII value of '0' is, according to python, ord('0') == 48 == 0x30 == 0b 0011 0000. I am afraid , if it is an error, it has been copied and pasted to every digit.

Does asm support quoted characters ('0'.... '9')? That would make things easier .

Perhaps C# should support quoted characters, too....

PC is not likely to find out a digit as code is written (or my optical nerve is in a very lousy state from following all the jumps)....

Edited : what would happen if serial port is not "com1" -say, one bought a new PC, with "only" USB plugs?

The assembly code would appear to be a horrible mashup of AVR and 8051.

Never was a code comment so true...

;this initialization was my friend's advice, but i think it's not right

"This forum helps those that help themselves."

"How have you proved that your chip is running at xxMHz?" - Me

"If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

Well, I suppose OP should change friends. I do not know whether teachers read code, and OP might add many other, subject related, comments.... (if it does not wotk, because of time constraints/unluck), comments can help fixing it...

The initailazition looks like sending data, but i need receiving data (i trust you), but are the data in r16 or r24? And the "2nd" part of code is just i am trying to make it easier - more sophisticated.

I am asking because i can't try it right now, i will try it tomorrow and it will be the last chance to try it on MCU then i have to give it to him, so could you tell me, if this will possibly work or how can i put the data from serial port to a register? Thank you

while ((UCSRA & (1 << RXC)) == 0) {}; // Do nothing until data have been received and is ready to be read from UDR
volatile byte ReceivedByte = UDR; // Fetch the received byte value into the variable "ReceivedByte"

Using Clawson's hint, you can get it converted it into asm, and adapted.