Quick basic questions about microcontrolers/microprocessor 8/16/32 bits

Thread Starter

Xenon02

Joined Feb 24, 2021
504
In general, you do not overwrite data because the data is stored in its own place.

When you declare two variables, X and Y, they are allocated separate space in RAM.

One area of concern is in array storage, such as strings.
If you declare an array with 10 characters and you attempt to write 11 characters then, of course, you are asking for trouble.
Can you tell me an example or something like that ? I need to somehow visualise it. Like variable X is in I don't know in first free byte it happens to be 0x20h and Y in 0x21h
Okey so they do not overwrite.
My question wasn't of adding new character to already existing. The example was :

- There is a existing data in byte 0x19h and it contained which diodes must turn on we have 8 LEDs so you know 8 bits 8 LEDs.
- I have array with 20 characters so if it is stored in the first free byte then it would be 0x00h right ? It is free only 0x19h is occupied. So 20 character then is it 0x00 (first character) 0x01 (second etc...), 0x02 .... it skips 0x19h and it is stored then in 0x20h ?
- If yes then how the program know what the array end in 0x20h byte and that 0x19h is not a character ? Can something like that happen ?

MCS-51 is a very old model and you need to move away from that model.
More recent models (though many years old too) are MSP430 and Atmel AVR.

Have a look at this.
https://www.nxp.com/docs/en/reference-manual/M68HC05TB.pdf

Then everything I know is useless ? I cannot compare MCS with STM32 with managing the stored data ?

Why does it work that I don't have to worry about it ? That let's say I have some stored data I don't know where because you said it is not important and I use one byte for something and Accidently it was occupied but I didn't know because I didn't put attention to it.


I tried to show an example before like : the lowest bit of a ram registor was (lowest free byte) 0x30h and it was first byte, then the UART clears it's 8 bit storage and takes another character and stores it in for example 0x31h (another lowest free byte because 0x30h is occupied) because it is now the lowest free byte.
Also is Bank/R0/R1 can be also treated as first free byte ?

These quotes are incorrect ?

Sorry for taking the time, soon I will gather what I understood write an big answer and close it because I probably this post is to long for me having a hard time ;>


You have asked many questions all at once and I have lost track of what is your question.

Ask one question and let’s see if we can give you the appropriate answer.
Sorry, my bad trying to understand everything at once and accidently create new questions.
 

MrChips

Joined Oct 2, 2009
34,817
unless it is stored in unicode, which is 2 bytes per character.
True, but let’s keep it simple for TS.

TS is making silly mistakes such as unused memory is being cleared to 0x00. And 0x20 follows 0x19.

TS needs to get his act together.
 

MrChips

Joined Oct 2, 2009
34,817
@Xenon02 you need to fix your hexadecimal notation.
There are three ways to represent hexadecimal notation in printed text.

$12AB
0x0F3C
0C56h

You skipped 6 values going from 0x19 to 0x20.

I cannot answer multiple questions at once. Please ask one question at a time.
 

MrChips

Joined Oct 2, 2009
34,817
To answer your last question, the MCU processor does not know if the byte at address $1234 is being used or not, only the human programmer who wrote the code knows.
 

Ian0

Joined Aug 7, 2020
13,132
To answer your last question, the MCU processor does not know if the byte at address $1234 is being used or not, only the human programmer who wrote the code knows.
Some microcontrollers do, but only when it’s too late and the attempted write gets the hardware in a mess.
Worse still are processors with incomplete decoding of the address bus which means that the attempted write ends up somewhere else.
 

MrChips

Joined Oct 2, 2009
34,817
To clarify things for TS, if variable XYX is declared at RAM location $0020 and has the value $00 stored there, the value 0 has as much significance as the value 255. No one can tell if the location is occupied or not.

Declaring character strings is different.
In Pascal, the first byte gives the length of the string. A zero byte means that the length is zero, regardless of what the following locations contain. The maximum length of the string is 255 characters.

In C, the character $00 is known as the NUL character. This is used to terminate the string. Hence a string can have as many characters as space available. If the first byte is $00 then the length of the string is zero, regardless of what follows. You don’t have to clear subsequent locations to $00.
 

Thread Starter

Xenon02

Joined Feb 24, 2021
504
To clarify things for TS, if variable XYX is declared at RAM location $0020 and has the value $00 stored there, the value 0 has as much significance as the value 255. No one can tell if the location is occupied or not.

Declaring character strings is different.
In Pascal, the first byte gives the length of the string. A zero byte means that the length is zero, regardless of what the following locations contain. The maximum length of the string is 255 characters.

In C, the character $00 is known as the NUL character. This is used to terminate the string. Hence a string can have as many characters as space available. If the first byte is $00 then the length of the string is zero, regardless of what follows. You don’t have to clear subsequent locations to $00.
So when the processor doesn't know which byte is occupied and which note and the string can be somewhere stored in the RAM, how do I know that the string doesn't overwrite the byte I've choosed ? When the whole sentence is stored randomly.


You skipped 6 values going from 0x19 to 0x20.
Yup it was supposed to be19h and 1Ah etc.


There are three ways to represent hexadecimal notation in printed text.

$12AB
0x0F3C
0C56h
A yes also again sorry


TS needs to get his act together.
I've rechecked my previous codes I've used last year to refresh what I was doing in MCS-51 8051F06x
And I had a Asembler code example and C code example. In assembler code I had to focus on where I saved data, which I guess is normal. Then I checked the C code example, and yea I had a variable called DataBusValue and I didn't think then where it was stored but with Asembler I think I had to declare there the DataBusValue has to store it's data in specific byte.
I think I understand what you've tried to tell me at the beggining that where it stored doesn't matter because while coding in C the variable gets assigned space after compyling.

Like here :

C code :
1696513551056.png
Assembler :
1696513705291.png

And in Assembler I had to store the data in Accumulator (A) so the storage had to be defined. While in C coding I just set the value and it storages by itself, I don't know how but it does and picks random place to store it I guess. That's what I came up with while watching at codes.
 

MrChips

Joined Oct 2, 2009
34,817
No. In ASM and in C, you don’t need to be concerned where the variables are stored, so long as there are no memory location violations.

In ASM, your variable was given a name P7. The assembler would assign it to a location which you don’t need to know.

A is the working register in the code

mov P7, A

A does not count as a location.
 

Thread Starter

Xenon02

Joined Feb 24, 2021
504
In ASM, your variable was given a name P7. The assembler would assign it to a location which you don’t need to know.

A is the working register in the code

mov P7, A

A does not count as a location.
P7 here is a DATABUS_PORT.
So ok then the value stored in P7 is located somewhere in the red circle or in green circle or somewhere possibly in both ?

1696518470800.png


Then why do I have a possibility to use specific byte from General Purpose RAM or specific bit from the green circle area ? Is there any purpose to use for example mov 51h, #60h ? When I don't know where is the data stored from the P7 I can mistakenly overwrite P7 data. So what's the point in using something like mov 51h, #60h (saving data in specific byte written by user) when I can overwrite the data now knowing it.
 

Ian0

Joined Aug 7, 2020
13,132
You’ll find it in the Special function registers between 0x80 and 0xFF, but I thought that type of device only had three 8-bit gpio ports.
 

Thread Starter

Xenon02

Joined Feb 24, 2021
504
I don’t do MCS-51.
I would scrap it and move on.
So I understand that in different MCU 8 bit ones or bigger ones doesn't have the option like in MSC-51 where I can just write a data individually in General Ram.
How does it look like in other devices ? Does it have something like gerenal ram or is it saved as well in SFR like in MCS-51


You’ll find it in the Special function registers between 0x80 and 0xFF, but I thought that type of device only had three 8-bit gpio ports.
Wait how :D ?
I thought that in SFR there are special functions like turning on Timer 1,2,3, setting the Timers and many more. I know there are many SFR pages so how is it possible or rather where it is written that there is a page for many variables ?
And why then I have an opption to use General RAM when any variable is saved in SFR, hence the data form UARF also is saved then in SFR because it's data which is a sentence is saved in variable as well, am I right ?
 

Ian0

Joined Aug 7, 2020
13,132
Wait how :D ?
I thought that in SFR there are special functions like turning on Timer 1,2,3, setting the Timers and many more. I know there are many SFR pages so how is it possible or rather where it is written that there is a page for many variables ?
And why then I have an opption to use General RAM when any variable is saved in SFR, hence the data form UARF also is saved then in SFR because it's data which is a sentence is saved in variable as well, am I right ?
a port register is a ”special function” because it controls the output pins.
 

Ian0

Joined Aug 7, 2020
13,132
So I understand that in different MCU 8 bit ones or bigger ones doesn't have the option like in MSC-51 where I can just write a data individually in General Ram.
How does it look like in other devices ? Does it have something like gerenal ram or is it saved as well in SFR like in MCS-51
87AEA562-6A95-4D25-9E01-332925FD49CA.png
This is what it looks like in the processor I use.
If you attempt to the grey areas it is detected and it runs the “bus error” interrupt.
The rest consists of flash ROM (where the program goes), RAM and peripheral registers.
All RAM and flash locations are 32 bit (I.e. it reads four bytes at a time)
The peripheral registers can be 8, 16 or 32 bit, and by no means all possible locations are used.
 

Thread Starter

Xenon02

Joined Feb 24, 2021
504
a port register is a ”special function” because it controls the output pins.
Oooo so it is one byte register for this port I guess.
Then where it saves the variables ? And why I have the possibility tu same something in General RAM like mov 51h, #60h ?
Also do other microprocessors such as STM32 has General RAM and option like in the example " mov 51h, #60h" which is saving specific data in specific byte ? And lastly where it stores bigger data like from the UARF or from LCD screen of from speaker, also in SFR ?


This is what it looks like in the processor I use.
If you attempt to the grey areas it is detected and it runs the “bus error” interrupt.
The rest consists of flash ROM (where the program goes), RAM and peripheral registers.
All RAM and flash locations are 32 bit (I.e. it reads four bytes at a time)
The peripheral registers can be 8, 16 or 32 bit, and by no means all possible locations are used.
Holy moly so there is a special space in your processor for peripheral devices. So like data from LCD, from PC any information like "Hello World" is stored in this perpheral register I understand right ?
So what is normal RAM for ? When variables aren't I guess saved in general RAM and saving single byte data individually isn't prefered nad rather the user only uses variables or perhaps I am wrong.
And I slowly am getting how things works.
 

Ian0

Joined Aug 7, 2020
13,132
RAM is where your variables go.
If you receive "Hello World" via the UART, then the "H" will be stored in a peripheral register (the UART RXD register). You will detect that it has arrived*, take it out of the RXD register and start assembling it into a string variable in RAM. Then the "e" will arrive - repeat until you receive a terminator character which indicates it is the last character in the string.

*You will read a UART STATUS register (which is in the peripheral region) which will say whether or not new data is available. Alternatively, you might set the UART to interrupt when a new character appears.

Shall I mention FIFOs?
 

Thread Starter

Xenon02

Joined Feb 24, 2021
504
RAM is where your variables go.
Ok Variables are stored in RAM. How do I know where it is stored specifically ?
Let's say I wanted to use 52h byte and write in Assembler "mov 52h, #10h".And I know where it is stored now lets say I write


If you receive "Hello World" via the UART, then the "H" will be stored in a peripheral register (the UART RXD register). You will detect that it has arrived*, take it out of the RXD register and start assembling it into a string variable in RAM. Then the "e" will arrive - repeat until you receive a terminator character which indicates it is the last character in the string.
So from peripheral register it goes to RAM DataBusValue = 0x06 (that's how it is wroten in the program as I see). So DataBusValue is a variable will it not overwrite my data in byte nr.52 ? If not then why, how the program knows which byte to give this certain variable.

Do I also have to give this UART a specific amount of RAM and the range of bytes like : from ... to ... so that the data won't exceed the "Hello World!" ? Or is it all with setting the stuff like show the message, clear the RAM of this message and receive new messege from UART ?

Shall I mention FIFOs?
First in First out yes ? So first if "H" so it also is first to get out ;>
 

Ian0

Joined Aug 7, 2020
13,132
Ok Variables are stored in RAM. How do I know where it is stored specifically ?
In assembler - you tell it where to put it, and you have to remember not to put anything else there, and it is also you responsibility to make sure that there is RAM at that address.
In C, you don't care - you give the variable a name and the compiler decides where to put it because it knows where there is RAM and where there isn't, and it makes sure that other variables with different names don't use that location.
So from peripheral register it goes to RAM DataBusValue = 0x06 (that's how it is wroten in the program as I see). So DataBusValue is a variable will it not overwrite my data in byte nr.52 ? If not then why, how the program knows which byte to give this certain variable.
I wouldn't say that "DataBusValue" was not the best choice of name for the data you receive from the UART. Will you remember that was what you called it?
Anyway, the C compiler will allocate storage to it - it is first used inside the function, so it will put in temporary storage, in an area where it normally puts temporary variables. It won't overwrite any permanent variables.


Do I also have to give this UART a specific amount of RAM and the range of bytes like : from ... to ... so that the data won't exceed the "Hello World!" ? Or is it all with setting the stuff like show the message, clear the RAM of this message and receive new messege from UART ?
The UART won't do anything for you, except tell you when it has received a character. You have to take the character and store it and assemble it into a string, and you have make sure you haven't exceeded the amount of storage that you have allocated for it.
 

Thread Starter

Xenon02

Joined Feb 24, 2021
504
In assembler - you tell it where to put it, and you have to remember not to put anything else there, and it is also you responsibility to make sure that there is RAM at that address.
In C, you don't care - you give the variable a name and the compiler decides where to put it because it knows where there is RAM and where there isn't, and it makes sure that other variables with different names don't use that location.
Understandable


I wouldn't say that "DataBusValue" was not the best choice of name for the data you receive from the UART. Will you remember that was what you called it?
Anyway, the C compiler will allocate storage to it - it is first used inside the function, so it will put in temporary storage, in an area where it normally puts temporary variables. It won't overwrite any permanent variables.
I have mixed what I tried to ask when I read it again but I understood it reading the first Paragraph.

But I'll try to ask something else, will the C also remember which bytes are the character bytes ? Like lets say 12h, 13h and 15h are the bytes that contain H, i, !
Why 15h ? Because in 14h is variable "a" which is equal let's say 5. I understand that C will know that 12h, 13h and 15h stored in RAM are the characters and with take 14h as a character.



The UART won't do anything for you, except tell you when it has received a character. You have to take the character and store it and assemble it into a string, and you have make sure you haven't exceeded the amount of storage that you have allocated for it.
A okey so like I create an array that is 10 character wide. Any more information that the UART want to send to this array will be discarded ? Ignored ? Untill this array is reseted and ready to read new info from UART ?
 
Top