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

Thread Starter

Xenon02

Joined Feb 24, 2021
500
Hello !

I've been learning more about microcontrollers and microprocessor with 16 and 32 bits. So far I've been using MCS-51 the 8 bit microcontroller and I couldn't understand something when I was learning 32 bit STM32.

I would like to also confirm if I am right about some facts :
- is it true that the code for MCS-51 or for STM32 written in C tracked by program counter and send the whole code from top to bottom into ROM memory and the whole code is being transfered into opcode and sent from ROM into control system and the control system says what ALU has to do whether it is Adding two adresses or changing the value of one if his registers etc.
- The program counter restarts and starts again from top to bottom all the time until the program stops ? I've seen that the program counter has a connection from control system and it increments the program counter

About my problem understanding some things in STM32 which is 32 bits :
- When I have UART that transmits or receive for example 8 bit (I know it can be 7-9 bits I choose 8 bits). Where does it store the information in ALU ?
I mentioned about the storage because MCS-51 I usually had to store information in RAM 128 Byts in which 16 Byts I can choose any bit and change it's value and okey I receive 8 bit of infomation from UART, where could it be stored in the 8 bit microprocessor or microcontroller ? In the 128 Byts of Ram or in 128 Byts of SFR ? Also for 32 bit STM32, where he storages the information in which part of the ram from UART, because he needs 4 Byts to make an adress usable for processor because he need 32 bit adress and cannot take 8 bit or 16 bit adress.

1696445287361.png

- Why the opcodes have different length sometimes it can be 8 bit sometimes 16 bit. For MCS-51 even thoughit is 8 bit the opcode can be 16 bit for one operation.

1696445438715.png

I thought that SUBB will have 8 bits, A will have 8 bits and R0 also 8 bits, all 3 of them instead of giving 24 bits they give 8 bits. Does it also works the same with STM32 ?

- I think this is the last question. When I write mov 40h, C then I use the bit (like it is shown in red arrow) and when I write mov 40h, A then I use the Byte from the general purpose ram and not the 40h bit from 28 byte ?

I am grasping the knowladge of microcontrolers and microprocessor, it is just the bits/bytes lengths that changes and I lose the track at some point ... I think this isn't that important of information to create a decent program, but I wanted to know how it works because the bit/byte length that changes from 32 bit into 8 bit input from UART that is stored somewhere, or the opcode length, or other bit/byte related things. Also I was struggeling with where it is stored and how much it stores because I was confused with how much space I have and which part of the ram is free so I can direct it somewhere. Sorry for my confusion, because while writing this post I was trying to show my side of confusion with my sloppy grammar ;>

PS.
I was also confused with UART that was transmited from STM32. Like transmite from STM to PC a sentence "Hello World I am STM32 !" How much space will it take in STM32 where it stores do I have to worry whether the space will run out ?

It is hard to say what confuses me I was so used to the MCS-51 where I somehow knew how it worked because when I wanted to control 8 diods so I used one byte to do it with specific value liek 10111001 and I knew which diode worked. But with UART and where is stores the info or other interfaces/devices. I just used UART as an example.
 
Last edited:

Ian0

Joined Aug 7, 2020
10,293
On a true ARM processor all the instructions are 32-bits long. The Thumb instruction set which is used on the cortex M series of ARMs abbreviates some instructions to 16-bit, so the result is a mixture of 32-bit and 16-bit instructions.
You can choose when you write to memory whether you write 8, 16 or 32 bits, by writing STRB, STRH or STR.

Whilst the core might have 32-bit registers throughout, the peripherals don't. Some peripheral registers (such as the receive and transmit registers for a UART) may only be 8-bits wide. When writing STR R0,[R1] with R1 pointing to a 8-bit register, the top 24-bits disappear.
 

Thread Starter

Xenon02

Joined Feb 24, 2021
500
On a true ARM processor all the instructions are 32-bits long. The Thumb instruction set which is used on the cortex M series of ARMs abbreviates some instructions to 16-bit, so the result is a mixture of 32-bit and 16-bit instructions.
You can choose when you write to memory whether you write 8, 16 or 32 bits, by writing STRB, STRH or STR.

Whilst the core might have 32-bit registers throughout, the peripherals don't. Some peripheral registers (such as the receive and transmit registers for a UART) may only be 8-bits wide. When writing STR R0,[R1] with R1 pointing to a 8-bit register, the top 24-bits disappear.
That pretty confusing when the bit lenghts can change. I didn't really understand your second paragraph.
And I would be really greatfull if you could point which question you are answering :) Because I've asked couple of them.

Going back you what you've said. What is STR :) ? What does it mean that top 24 bits dissapear. I know that in MCS-51 have 8 bit register size so then R0 has 8 bits size.Then in STM32 and his R0 have 32 bits ?

So the peripherals data is saved in registers such as R0 ? Same goes with other peripherals deviced not only UART ? Like LCD screen and it's information is stored also somewhere in registers ?
Going back to 8 bits so it is stored in R0 and first it get 8 bits and get next 8 bits till we get full 32 bits from UART and then it is send from R0 straight to ALU or changed into an opcode ?

Also about first paragraph the instructions are always 32 bits when I had MCS-51 I had one instruction but it was 2 byte so 16 bits :

1696450713511.png
sorry it was supposed to be byte.
Also I might have mistaken that opcode = instruction. So perhaps opcode can be very long like 3 byte but the instruction is always 8 byte.

Here is 1 byte
1696450868076.png

Thank you and sorry for this block of text.
My mind is twisted with those bit length and how it is changable and where itis stored aaaaaaaa :D

I got lost a bit.
 

Ian0

Joined Aug 7, 2020
10,293
As you mentioned STM32, I thought you might be familiar with the ARM instruction set.
STR is Store in Memory. STRB is Store Byte in Memory, STRH = Store Half-Word in Memory.

It's easiest to think of it as storing 1, 2 or 4 bytes at a time. Some 8-bit micros have 16-bit registers, which can be saved to two consecutive bytes in memory. It's no different.

A UART is a peripheral that may have 8- or 16-bit registers. They may be mapped contiguously, so that RXD is at one address, and TXD at the next address (Renesas does it that way), or they may be mapped so that they are all at addresses divisible by 4 (NXP does it that way) Not sure about ST.

When you write STRB R0,[R1] it stores just the lowest 8-bits of R0. Nothing happens to the top 24 bits. They just stay in R0.
If you use STR R0,[R1] the top 24 bits will be stored in [R1]+1, [R1]+2 and [R1]+3 if they exist. In the case of NXP peripherals, they don't, so the top 24 bits of R0 don't go anywhere.
 

Thread Starter

Xenon02

Joined Feb 24, 2021
500
As you mentioned STM32, I thought you might be familiar with the ARM instruction set.
I was starting, and got confused pretty fast. The only thing I was pretty familiar was only MCS-51.


When you write STRB R0,[R1] it stores just the lowest 8-bits of R0. Nothing happens to the top 24 bits. They just stay in R0.
Wasn't it before STR that just stores 8-bits of R0 ? And STRB is 2 bites so 8 bytes in R1 and 8 bites in R1 + 1
Ok comming back. So I understand that R0 isn't the STR32 register but this UART register and the R1 is the STM32 register. And how does the ALU know where the next byte is alocated in R1 how those he know what R1 + 1 means ?

But I slowly get it. And where the code goes ? From UARF it goes straight as an instruction to STM32 or rather as a part of program that when it detects data from UARF stored somewhere it will do something.

And last thing. Like I asked at the beggining. When I want to send "Hello STM32" from PC to STM using UART every single letter is 16 bite or 32 bite wide. Where is stored the whole sentence ? It works the same when the STM wants to send the same message to PC it has to storage the whole sentence and it has many bites. How it doesn't overflow because R0 isn't enough. It alocates it self somehow ?

Ahhhh I again overdone asking many things ... It is confusing. I got lost in bits and bytes. Because everytime I see something that isn't 32 bit as a problem because I don't know how it works or where it is stored and if I should be worried about it because I don't know how much space I have and where ;>
 

MrChips

Joined Oct 2, 2009
31,214
Try not to overthink this.

An 8-bit processor works with 8-bit registers and single byte data. Some registers might be 16 bits in length. In that case, the 16-bit register is accessed as two bytes, LO-byte and HI-byte.

A 32-bit MCU might still access data in single bytes. It might access 1, 2, or 4 bytes with a single instruction. Thus, if the hardware peripheral register is 8-bits long, it can access the data as a single byte. As a programmer, you can virtually ignore how the processor does it.
 

Thread Starter

Xenon02

Joined Feb 24, 2021
500
Try not to overthink this.

An 8-bit processor works with 8-bit registers and single byte data. Some registers might be 16 bits in length. In that case, the 16-bit register is accessed as two bytes, LO-byte and HI-byte.

A 32-bit MCU might still access data in single bytes. It might access 1, 2, or 4 bytes with a single instruction. Thus, if the hardware peripheral register is 8-bits long, it can access the data as a single byte. As a programmer, you can virtually ignore how the processor does it.

Hmm I think I slowly get it. MCU are like a mixture. I've heard that STM32 i think Discovery gave access to write 16 bit instruction and was converted into 32 bit instruction. But 16 bit instruction was easier to write and it didn't work everytime.

Okey so 8 bit from UARF goes to ALU not as an instruction because it is not an instruction I guess ? It is data like input, A = 5.
And the UARF has it's own register and it transfers into STM register right ? I have to set where UARF will save it's content ?


As a programmer, you can virtually ignore how the processor does it.
I think it came from programming 8051F06x. I had to focus when programing the LEDs on every single bit, using P7 and P2 to control them etc.
And I got very confused looking at how STM worked with 32 bites and then switches to 16 bits etc.
I belive you can understand my confusion haha ;D
But still I wanted to understand it because I couldn't accept not knowing it ;>


“Hello World!” is still store as one byte per character, not 4 bytes.
Oh so how many space it takes ? Or is it like that he shows one character "H", clears the register and shows another character ? Or rather it takes R0,R1,R2 because there are like 10 bytes.
Just asking because I am worried how to manage the memory, what is left to use freely and which registers are still free.

There are also more questions that I wanted in the first post and I would like to clear up any doubts.

Sorry for any bother
 

MrChips

Joined Oct 2, 2009
31,214
“Hello World!” contains 12 characters. These get stored in 12 bytes plus a NUL byte.

Assuming that the program is written in C, you can ignore how the MCU will access this string. The code written in C for MCS51 will be the exact same code as for STM32.
 

Thread Starter

Xenon02

Joined Feb 24, 2021
500
“Hello World!” contains 12 characters. These get stored in 12 bytes plus a NUL byte.

Assuming that the program is written in C, you can ignore how the MCU will access this string. The code written in C for MCS51 will be the exact same code as for STM32.
But where they are stored ? in MCS51 R0 has 1 byte, and in 1 bank there is from R0-R7 not enough. And if it was a bigger text. Same goes with STM32.

1696454867640.png

Something like that.

Also do you have any advice with bytes and bits ? Like in MCS 51 I could use to storage things in Bit Reg or in General Reg or in Bank the SFR was for special functions. So when I don't know when data goes to Bank or to general register or to bit adress how am I supposed to know which memory slot is free, and same goes with STM32 when i don't know where it goes and how much it takes from my usable memory and do I have enough memory ?

I am overthinking this but it confuses me.
 

MrChips

Joined Oct 2, 2009
31,214
No data gets stored in ALU register.
R/W variables will occupy space in RAM.
Static data will be stored in flash memory. The model of the MCU will tell you how much RAM and ROM space you have.

The STM32F407VE that I am using has 512KB flash and 192KB of RAM. I would have a difficult time using up all of that.
 

MrChips

Joined Oct 2, 2009
31,214
You mentioned controlling single bits in a port, such as P2 and P7.
Again, you don’t worry about how many bits there are in a port.
You can set, clear or toggle a single port bit regardless of the model of the MCU, 8, 16, 32, or 64-bit model.
 

Thread Starter

Xenon02

Joined Feb 24, 2021
500
No data gets stored in ALU register.
R/W variables will occupy space in RAM.
Static data will be stored in flash memory. The model of the MCU will tell you how much RAM and ROM space you have.
In MCS-51 it would be General registers (part of the ram) and not Banks? Because the whole ram is in ALU so banks are the registers and General registers are also registers and part of RAM as well. I was also confused because before Ian was using R0 and R1 like it was from a bank and they are registers so I thought that the data is randomly stored.
In STM32 like you said it stores in flash interesting. And the processor somehow finds this data. So I have to be carefull where the data from external devices are stored, it is somewhere written how it stores to not mistakenly for example take one byte and save some information but I didn't know how much it would take. I mean you know, with MCS-51 I didn't know which space was used so I had to be carefull.


You mentioned controlling single bits in a port, such as P2 and P7.
Again, you don’t worry about how many bits there are in a port.
You can set, clear or toggle a single port bit regardless of the model of the MCU, 8, 16, 32, or 64-bit model.
I am just very confused because everything is mixing Once I have to care about bits and once not and I don't know anymore ;D
I think they are single bits ? I saw it on youtube. The ports maybe aren't the problem rather the received and transmitting data adresses.

I think I learned something from this post which I am very gratefull which is :
- UARF isn't going into ALU as instruction but acts like input.
- The instructions for 32 bit can have 16 bit instruction but I think the 16 bit is translated later into 32 bit
- The character have 1 byte (I thought it was ASCII with 32 bits ;>)

I am still confused with bytes and bits because whenever I see a tutorial it changes it's size from 32 to 16 or anything else and is stored somewhere, and if I want to use ram to like mov 35h, #45h (I know in STM32 it would be different but you get the point), and it happens that there was data I didn't know because I don't know from where the stored data starts and where it ends. I just know it came from UARF and that's all it is stored somewhere in RAM.
 

MrChips

Joined Oct 2, 2009
31,214
I don’t know what is UARF.
If you mean UART, hardware peripherals have their own data storage. If the size is 8-bits, then it is read as a single byte.

There is no conversion of 16 or 32-bit instructions. So I don’t know where you got that concept.

If the hardware register is 8 bits long, it is read in as 8 bits into the lowest 8 bits of the ALU register. There is no conversion happening.
 

Thread Starter

Xenon02

Joined Feb 24, 2021
500
I don’t know what is UARF.
If you mean UART, hardware peripherals have their own data storage. If the size is 8-bits, then it is read as a single byte.
Yes sorry you are right. I did a misspell.
So when I have 12 bytes of characters then it works like that
- it stores in UART own data storage, and it storages the first byte then send it into RAM lowest bit which clears UART own register and repeats the process
- first lowest 8 bites are 0x30 to 0x37 it is the general ram (Because it is not stored in bank or in bit addres register I guess am I right ? ) and I think this is the ram it is occupying. Then the second byte would be I think 0x3A to 0x3F etc.

So usually the external data like UART or LCD screen or anything is stored in the lowest bit of ALU register ? Good to know.


There is no conversion of 16 or 32-bit instructions. So I don’t know where you got that concept.
I am not sure where I heard it. I just saw that there were two equivelant instruction but one was in 32 bit and one in 16 bit.


If the hardware register is 8 bits long, it is read in as 8 bits into the lowest 8 bits of the ALU register. There is no conversion happening.
O okey so the 12 bytes that are stored in RAM are read from the lowest to the highest and the program interprets this data as ASCII. That's how it pop out the info what the STM/MSC-51 received.
 

MrChips

Joined Oct 2, 2009
31,214
You have a lot of misconceptions.

Eight bits are eight bits, nothing more, nothing less.
It doesn’t matter in what order the characters in “Hello” are stored.
Just accept that the program will accept one character at a time. One character is 8 bits wide. This is binary. ASCII is binary.

For your understanding, stop using the term ASCII. You don’t need it.
You are just fetching 8 bits from memory somewhere and moving it to some place else.
 

Thread Starter

Xenon02

Joined Feb 24, 2021
500
You have a lot of misconceptions.

Eight bits are eight bits, nothing more, nothing less.
It doesn’t matter in what order the characters in “Hello” are stored.
Just accept that the program will accept one character at a time. One character is 8 bits wide. This is binary. ASCII is binary.

For your understanding, stop using the term ASCII. You don’t need it.
You are just fetching 8 bits from memory somewhere and moving it to some place else.
So was I fully incorrect ?
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.

Order maybe doesn't matter but rather where they are stored mattered to not overwrite any specific byte with new data.
For example let's say I have 20 characters. But before loading these characters into ALU RAM, there was already an occupied byte and it was 0x19h, it was occupied with data on which diodes to turn on, but the string had 20 characters. Will it overwrite the 0x19h byte, because it starts from 0x00 to 0x19(because 20 characters)? If it skips the 0x19h and takes 0x20h how the program knows that 0x19h is not the character and 0x20h is ?

I also get it that the UART accepts only 1 character, so UART storage passes it into first lowest byte in ALU RAM, clears the UART storage and takes another character and it repeats.

Also is Bank/R0/R1 can be also treated as first free byte ?

Sorry again for my misconceptions I am learning to understand how bigger bit processors and controlers works, and I mentioned alot that my previous practice was something like :

- store byte for LED turn on as mov R0, #51h
- use DPTR knowing where it was stored for moving light LED clockwise and counter clockwise
- store in byte 8-segment display like mov 32h, #12h (one character)
- I tried to turn on the small numeric keyboard

In everything I needed to manage where it was saved in RAM, so why here in 32 bit it is neglected without the thought that I might accidently ovewrite ?

PS.

I also want to ask if anything I said is correct at some point. I want to try to correct myself and get approved that yup I got it ;>.
the only thing I know is only MCS-51 and how small it was and that I had to know where it is stored (that's how it was having only 128 B of RAM so 12 characters was pretty a lot).And when there is chaos in STM32 then why was I learning how to manage the ram in MCS-51 that created my misconception of this all byte/bit stuff

Or rather what is weird with the question where it is stored ? What if I overwrite something accidently like in my example.I know I am comparing it to 8 bit controler with small RAM but with bigger RAM with kB I wonder how it was managed without overwriting because I don't believe it is somewhere stored without knowing where. So many questions and I don't know where to find answers ;>.

Thank you for your patience !
If something is not understandable or if you don't know what I am trying to say, give me a sign so I'll edit it.
 
Last edited:

MrChips

Joined Oct 2, 2009
31,214
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.
 

MrChips

Joined Oct 2, 2009
31,214
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.
 
Top