Author Topic: Newbie, What is the right way to learn programming?  (Read 6946 times)

0 Members and 1 Guest are viewing this topic.

Offline King123Topic starter

  • Contributor
  • Posts: 18
  • Country: in
Newbie, What is the right way to learn programming?
« on: July 17, 2022, 09:48:01 am »
Hello

This is my first post and I don't know C programming. I want to learn  C programming. I hope the experts here will give me the best advice to learn Embedded C programming.

I have installed code block in my computer. I have also started reading books for C language. I am still finding it very difficult to learn programming. I don't understand how to make learning programming easier.

Do you have any advice to make learning programming easier for newbie?
« Last Edit: July 17, 2022, 09:50:04 am by King123 »
 

Offline Picuino

  • Frequent Contributor
  • **
  • Posts: 730
  • Country: 00
    • Picuino web
Re: Newbie, What is the right way to learn programming?
« Reply #1 on: July 17, 2022, 09:56:02 am »
You can start with an Arduino board to learn embedded programming.
There are plenty of examples and you can get help from many people online in many forums.

You can program Arduino in C or C++. You will see some small difference with these languages because Arduino uses a template to translate the main() function by two other functions: start() and loop().
But the compiler underneath the Arduino software is a GNU C++ compiler.
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 8179
  • Country: fi
Re: Newbie, What is the right way to learn programming?
« Reply #2 on: July 17, 2022, 10:01:40 am »
Try to find some old tutorials for programming PIC or AVR microcontrollers in C, before the time of Arduino, bloat libraries and frameworks and whatnot.

In other words, make Google return only results pre-2007 or so.

Then start some simple project with said simple 8-bit microcontrollers. You can totally use the Arduino boards and still write code like Arduino did not exist yet.

This way you gain good fundamental understanding of how microcontrollers work and how to program them in C. This all applies to more fancy microcontrollers.

For better idea of C, look at source codes of linux kernel, or some classic linux tools.

I mean, if you use AVRDUDE to program the AVR microcontrollers, why not look at AVRDUDE source code: https://github.com/avrdudes/avrdude/blob/main/src/ just for fun / learning experience. It's written in C, but is more complex and runs on an operating system, making it a different kind of learning experience than the "bare metal" C code you write on AVR/PIC.

Also whenever in doubt with C, either look directly at the C standard, or just google about your problem, instead of assuming. You start building understanding piece by piece.

Getting some good old classic C book is not a bad idea, either!
 

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 3711
  • Country: nl
Re: Newbie, What is the right way to learn programming?
« Reply #3 on: July 17, 2022, 11:01:26 am »
It depends on what your knowledge level on the subject is.

But I would say, start with studying how a microprocessor works. Gaining insight in the deterministic nature will help you understanding how C works too.

See https://www.tutorialspoint.com/microprocessor/microprocessor_quick_guide.htm  or for instance something like this about the 6502 https://codeburst.io/an-introduction-to-6502-assembly-and-low-level-programming-7c11fa6b9cb9 or the Z80 https://www.assemblytutorial.com/z80/

But even the AVR based microcontrollers are fine to learn the basics of it.

Knowing about assembler can help in understanding quite a bit when learning how to program in C.

A book like this one is also useful https://archive.org/details/isbn_9780070145900

Offline Picuino

  • Frequent Contributor
  • **
  • Posts: 730
  • Country: 00
    • Picuino web
Re: Newbie, What is the right way to learn programming?
« Reply #4 on: July 17, 2022, 11:19:39 am »
"The C Programming Language" by  Dennis Ritchie, Brian Kernighan.
It is the c language book written by the creators of the c language. Still one of the best manuals on the language.
If you search a little you can download a digital version of the book.
 
The following users thanked this post: cfbsoftware

Offline abquke

  • Regular Contributor
  • *
  • Posts: 128
  • Country: us
Re: Newbie, What is the right way to learn programming?
« Reply #5 on: July 17, 2022, 12:33:32 pm »
It might be worthwhile to spend some time getting to know C, compilers, and debuggers on a regular computer for a while. This will let you learn things that will be the same as when you're developing for a microcontroller, which will make learning the differences a little more clear.
 

Online xrunner

  • Super Contributor
  • ***
  • Posts: 7519
  • Country: us
  • hp>Agilent>Keysight>???
Re: Newbie, What is the right way to learn programming?
« Reply #6 on: July 17, 2022, 01:01:51 pm »
Hello

This is my first post and I don't know C programming. I want to learn  C programming. I hope the experts here will give me the best advice to learn Embedded C programming.

I have installed code block in my computer. I have also started reading books for C language. I am still finding it very difficult to learn programming. I don't understand how to make learning programming easier.

Do you have any advice to make learning programming easier for newbie?

Not clear if you want to learn programming or you want to learn C programming.

A long, long time ago I learned programming with a very basic computer that had BASIC on it. No arduino or anything else. I just learned how to program - how to get the statements in the language to do what I wanted.

If you want to learn how to program, you can start with C for sure. But I wouldn't worry about Arduinos or anything like that. I would do basic sorts of things like printing and manipulating simple equations and looking at the answers - things like that.

The Book "The C Programming Language" by Kernighan & Ritchie, which I'm looking at right now, has lots of simple examples to start with. Get those examples to work on your computer by looking at screen results and others and you will learn programming.  :-+
I told my friends I could teach them to be funny, but they all just laughed at me.
 

Offline rooppoorali

  • Regular Contributor
  • *
  • Posts: 100
  • Country: bd
Re: Newbie, What is the right way to learn programming?
« Reply #7 on: July 17, 2022, 01:46:25 pm »
I think you're going to the right direction.
 

Offline Christe4nM

  • Supporter
  • ****
  • Posts: 252
  • Country: nl
Re: Newbie, What is the right way to learn programming?
« Reply #8 on: July 17, 2022, 01:47:18 pm »
There are good answers already from people way more experienced than me. I don’t know that many resources myself apart from one I’ll link to below. But I’d like to add something about what you’re about to learn, so you may have a better idea of how you want to go about it. I am going to assume you are starting without any prior knowledge.

I’d say learning embedded programming has 3 main elements to it.

1) Learning the concepts of programming and the mindset of a programmer
2) Learning a programming language, in your case; learning C
3) Learning how microcontrollers work and how this all applies to programming them

Let’s have a look at those in a little more detail.

1) Learning the concepts of programming and the mindset of a programmer
This is learning about such things as variables and variable types, loops, conditional statements, functions and splitting up your program in smaller subsections. About how to test your code. Best practices. Algorithms perhaps, and ways to design and structure your program; i.e. it’s architecture. And so on. You’ll get a feel for *how* to think, as well as the concepts involved in programming. While you learn them at first tied to your first programming language, many ideas are applicable to any language, even if they look very different. Which leads me to the next element.

2) Learning a programming language
This is learning the actual language, C in your case. Compare it with languages like English, or Spanish or French, etc. If 1) is about the ideas of nouns and verbs and how to build a sentence, and such, 2) is the actual language, the words, phrases, saying and idiom. You’ll need to get familiar with the way C does things. What you can and can’t do. And how to “say” what you want to achieve in C. And as with English, Spanish and French etc., There are things that you can easily do in C that another language doesn’t have naturally and you’ll need a workaround. And there are things that are easily done in another language, that C needs a lot of “words” for.

3) Learning how microcontrollers work and how this all applies to programming them
Embedded programming comes with its own set of ideas, and concepts. Now you are applying the above to a specific environment: a microcontroller (MCU). (Embedded programming is broader than only microcontrollers, but I’ll stay with what you mentioned in your first post). You’ll need to learn what a MCU really is, like on the inside. What things like the core, clocks, peripherals, registers, and many more parts are. You’ll learn about GPIO, timers, ADCs and about communication between the MCU and other chips with say UART (Serial), I2C, SPI, for a start. You’ll learn elements of electronics design, so you can understand how to power the chip, and connect it to other things outside it. You’ll learn how to read a data sheet or reference manual and find the information you’re looking for. In summary: you need to come to understand a microcontroller and it’s environment.
When you’re an absolute beginner 1 and 2 will go hand in hand. When you’re more experienced and ever want to try another language, you’ll find that many ideas and concepts in programming are similar, except different languages implement it in a different way. (This is too generic, I know that, and there’s more to the differences in languages. But for now that’s a starting point).

How to get started
So now to your question as to how to get started with this. There are multiple ways to go about it. Here is one:
You could separate 1) and 2) from 3) at first so you don’t get too overwhelmed with all these new ideas. That’s how I leant, by first learning C, and then starting to apply it to a microcontroller. The upside of this is that you are able to focus more on understanding the language and the concepts of programming first. And when you get to learn how the apply to a microcontroller you already know the basics of a language. The downside is, that you may learn things from C that work well for a PC programmer, but are best avoided when using C in an embedded environment. Specifically because the embedded environment puts constraints on what you can and cannot do, and if you only learn C for PC programming at first, you may need to unlearn things when you apply it to a microcontroller.

I don’t think that risk is too high when you’re only just starting out. If you go this route, start by getting familiar with the language C and concepts from variables, types and loops and if/else statements and structs. Pointers too perhaps, but by the time you get there, you’ll likely understand the basics of programming and the language C. That is a good time to switch what you just learned and apply it to a microcontroller. You'll need to decide on a microcontroller type you want to start to learn on. And with that comes a computer program in which you can write the code, compile it and download it into your controller. That's usually refered to by people as the IDE, for Intergrated Development Environment.

I’m not too familiar with (free online) courses or tutorials that I would be able to recommend anything for where you are at. I do highly recommend that when you feel familiar with C and have some experience with a microcontroller that you look up this course on YouTube: Modern Embedded Systems Programming Course. I wouldn’t recommend you start with it. Get to know C a bit first. But when you do, and have some first experience with a microcontroller, it’s a really good one to get in depth insight in how it works on a modern ARM microcontroller. Even if you end up using a different ARM MCU and different programming environment (IDE)

To Arduino or not Arduino
There is an ongoing debate about whether Arduino is a good starting point for learning to program. On one hand, when you are an absolute beginner it may be the quickest way to get you started with an actual microcontroller. However, Arduino does many things for you, specifically to make it so easy for you to just make something. I’d say that is one of its strengths, to provide something for people who just want to make something. The flipside to that, in my opinion, is that you run the risk that you don’t actually learn to program (well). And may even need to unlearn a few things. Depending on where you want to go with programming and microcontrollers, my personal suggestion would be to skip Arduino. This is just my opinion, but without it you’ll more likely to learn to program well. And you’ll more likely to get to understand how a microcontroller works. From personal experience it is very satisfying when you make the microcontroller do exactly what you want, after finding out which registers to set in a certain way. I found I learnt a lot faster that way and I had to really understand what I was doing, rather than typing one line of code that someone else made and does everything for you like a magic black box. It still feels like a reward to me each time.

An option could be to start with using an Arduino Uno as your hardware, with the Arduino PC program to write your code at first. But then actually not use anything Arduino gives as code at all. Just use the board and the software, but learn how to use the actual microcontroller by yourself. Like Siwastaja said above:
Try to find some old tutorials for programming PIC or AVR microcontrollers in C, before the time of Arduino, bloat libraries and frameworks and whatnot.
The Arduino Uno uses a Microchip (Atmel) ATmega328P. If you search for AVR programming you can find many examples from people who learned to program these before Arduino existed. Eventually though, you will want to use something else than the Arduino software with more capabilities and more control for you. So perhaps skipping it altogether is still the better option.

(edit: some typos)
« Last Edit: July 17, 2022, 01:51:53 pm by Christe4nM »
 
The following users thanked this post: Picuino, DiTBho

Offline abquke

  • Regular Contributor
  • *
  • Posts: 128
  • Country: us
Re: Newbie, What is the right way to learn programming?
« Reply #9 on: July 17, 2022, 09:15:05 pm »
I've been reading this book:
https://nostarch.com/bare-metal-c
and the first half has been a reasonably practical introduction to C. The overall motivation is towards microcontroller programming, so that's a leg up on most programming texts.

The K&R C book is good for reference, but not good for learning the first time out in my opinion.
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: Newbie, What is the right way to learn programming?
« Reply #10 on: July 21, 2022, 02:40:05 pm »
The K&R C book is good for reference, but not good for learning the first time out in my opinion.
The K&R book is loaded with conversion functions like itoa() and such.  These can be replicated in a uC project where it is not desirable to bring the entire printf() function and all the related string functions into the project.  I use the conversion functions all the time!

The easiest way I know of to learn C is under Linux.  I would probably install gedit for the editor and follow the instructions for gcc:

https://linuxize.com/post/how-to-install-gcc-compiler-on-ubuntu-18-04/

This will create a nice development environment.  Later on, you need to make sure that 'make' is installed and up to date.  This will simplify projects spread over multiple files and multiple directories.  Later...

If I were thinking in terms of Raspberry Pi, I would get one of the starter kits from Amazon.  There are many incantations but the code included with the Freenove is known to work on a Pi

https://www.amazon.com/Freenove-Complete-Raspberry-708-Page-Tutorials/dp/B09ZXNL2WH

You can use either a Pi 4 or Pi 400.  Think about the Pi 400, all you need to add is a monitor and you have a decent Linux workstation and a place to play with C projects.  It can be used as discussed above to just learn C or you can do the experiments in the starter kit for which code is provided.  There's a lot of education in this kit!

Buy the 3 item package deal because you will need a capable wall wart and having a power switch is handy.  Any monitor with HDMI input should be adequate.

https://www.amazon.com/Raspberry-400-Computer-Kit-RPI400-US/dp/B08MYVQW1S

The Raspberry Pi 4 will work just as well but you need a spare keyboard and mouse.
« Last Edit: July 21, 2022, 02:53:56 pm by rstofer »
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: Newbie, What is the right way to learn programming?
« Reply #11 on: July 21, 2022, 03:03:59 pm »
Depending on a lot of stuff, you might be able to take a C programming course at a local college.

Simply looking at someone else's code isn't really a learning experience unless you are already a better coder than they are.  You need to spend time behind a keyboard.

I just noticed you are in India, I have no idea what is available.  I would still go with the Linux recommendation simply because it is an easier place to play.  You can also do C projects using Microsoft Visual Studio (although they certainly hide the C capability).  Check out:



There are literally thousands of books on C programming and you can follow along by typing the projects.  I find that I learn more when my fingers and eyes are linked.

The subject of 'programming' is large regardless of the language.  Pay attention to how projects are structured.

And, yes, the Arduino is also a good way to go.  The underlying software is written in C++ but you can write the user code in regular C (mostly).
« Last Edit: July 21, 2022, 03:28:04 pm by rstofer »
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
« Last Edit: July 21, 2022, 03:13:37 pm by rstofer »
 

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 3711
  • Country: nl
Re: Newbie, What is the right way to learn programming?
« Reply #13 on: July 21, 2022, 03:58:03 pm »
If I were thinking in terms of Raspberry Pi, I would get one of the starter kits from Amazon.  There are many incantations but the code included with the Freenove is known to work on a Pi

https://www.amazon.com/Freenove-Complete-Raspberry-708-Page-Tutorials/dp/B09ZXNL2WH

Looks like a nice kit, but together with the Raspberry PI it is quite a bit of money, and there is no telling if the original poster can afford it.

There are cheaper options like an Arduino Uno clone with a cheapish kit from Aliexpress.

I'm not one to advise the Arduino IDE and framework for proper learning to program, but it is an easy way to get started.

For learning pure C a computer running linux certainly is a good place to start. The simple "hello world" projects and start building from there to get more things done, but the original poster did state he/she wants to learn "embedded" programming. Then it is debatable if Raspberry PI with linux classifies as embedded programming.

A Raspberry Pico springs to mind as an interesting platform to play with. Also very cheap.

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: Newbie, What is the right way to learn programming?
« Reply #14 on: July 21, 2022, 07:16:01 pm »
I just bought 2 of the Raspberry Pi Pico W (wireless) boards from Digikey at $6 each.  I'm assuming I can use the Micropython tools I was using for the non-W version.  I just got the boards, I haven't tried them.

Micropython is pretty interesting.  I use the Thonny toolchain

https://thonny.org/

I have to go looking for wireless examples.  I found an HTML Server project but I would be more interested in a Berkeley Sockets demo.

 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3147
  • Country: ca
Re: Newbie, What is the right way to learn programming?
« Reply #15 on: July 22, 2022, 04:18:56 pm »
I have installed code block in my computer. I have also started reading books for C language. I am still finding it very difficult to learn programming. I don't understand how to make learning programming easier.

Learning is difficult. It takes time and effort. Nothing you can do about this.

There are many shortcuts, but they typically work by avoiding learning rather than by making learning easier. Be careful not to fall in such a trap.
 
The following users thanked this post: newbrain, pcprogrammer

Offline eugene

  • Frequent Contributor
  • **
  • Posts: 494
  • Country: us
Re: Newbie, What is the right way to learn programming?
« Reply #16 on: July 22, 2022, 04:46:51 pm »
Learning to swim was easy: I jumped in a pool. I didn't jump into the deep end because it was over my head, so I started in the shallow end. I probably started by doggy paddling, but soon learned a few different ways to propel myself through the water. Eventually, I was comfortable in the deep end. The entire process, from beginning to end was relatively undramatic.

I started programming in C as a student, developing routines that I needed to either generate data (simulations) or process data. It was all pretty much command line driven, shuffling text files around in the great Unix tradition. Not a lot of drama there either.

When I first started programming MCU's it was in C using a 16 bit PIC. Writing code in C was natural for me, even things like pointers and arrays of pointers to functions were simple to understand. But setting up a PIC to read and write GPIO started out as a bigger step than I felt comfortable with. There was a longer, steeper learning curve than I was used to. Using timers to generate PWM that could be adjusted on the fly was another big hurdle. Today, this stuff is not difficult because I understand the concepts, but at the time it was a lot of work for a relatively small reward.

The problem was that there was no shallow end when it came to embedded programming. It was necessary to jump directly into the deep and and struggle to stay afloat without drowning. The Arduino ecosystem and IDE, etc provide a shallow end. So, when asked by a newbie about embedded coding, I always recommend starting with an Arduino or something compatible with the ecosystem. (I'm fond of Teensies, but Adafruit Feathers have advantages of their own.) They're easy just like the shallow end of the pool.

I never recommend that a beginner start by studying IO and timer registers before attempting to make a working program. To me, it makes far more sense to get something working on an Arduino-like board. Later, you can replace digitalWrite() with direct register manipulation. After that, registers will make a little more sense and you can start setting up and using timers through direct register manipulation. It's learning in bite size pieces, and it's way more manageable than diving straight into the deep end.

Point being that just because one starts with digitalWrite() and analogWrite() and delay() doesn't mean they're stuck there forever. It's just a first step. Many hobbyists don't need to go any farther than straight Arduino libraries and remain ignorant of how the MCU really works. That's fine because their applications don't require any more than that. Some gradually learn about AVR or ARM or whatever and exploit it. Some move on to VS Code and Platformio. Some eventually start designing their own boards and code bare metal. Good for them. Whatever works. And starting in the shallow end is proven to work.

For what it's worth, mostly I do consulting these days and what I'm best known for is developing custom instrumentation that gets used either in R&D labs or on production floors. In other words, not high volume commercial products. At most, a few of a particular instrument will ever get built. There's almost always a custom PCB required, and I could easily design it with an MCU of my choice. But, believe it or not, I continue to use a socketed Teensy 4.0 or 4.1 in most applications. Not only that, but even though I'm familiar with VS Code and Platformio, I still write code that compiles with the standard Arduino IDE. Why? Because that makes it way easier for other engineers to maintain. Simple as that. And it works well enough. I'm not afraid to use hardware specific features, so my code is never intended to be portable, but it should be easy for any EE or programmer to understand and maintain without any special tools that are long gone.

So, Arduinos are appropriate in many situations. Beginning hobbyists that just want to light up a string of LEDs is one. Beginning or experienced programmers that want to learn about microcontrollers is another. And even instruments that will be counted on to function reliably in a professional environment is yet another.
90% of quoted statistics are fictional
 
The following users thanked this post: Electro Fan, Picuino

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: Newbie, What is the right way to learn programming?
« Reply #17 on: July 22, 2022, 05:29:16 pm »
I think we need to separate 'coding' from 'programming' if 'programming' includes design.  To that end, I learned to program in Fortran back in '70.  It got to the point that I would draw a rough flowchart of the program's intent before I wrote a single line of code.  I still do that from time to time when the program logic gets very deep.  Google 'flow charts'.

FWIW, the big kids in the COBOL business logic community drew EXTENSIVE flowcharts in a very formal manner.  These were considered a major part of the documentation.  IBM used to provide flowcharts with their distributed libraries.  It was a really big deal.

You draw a flowchart on graph paper, do various walk-throughs to see if the logic is correct and then translate it into the language of choice.
 

Offline Picuino

  • Frequent Contributor
  • **
  • Posts: 730
  • Country: 00
    • Picuino web
Re: Newbie, What is the right way to learn programming?
« Reply #18 on: July 22, 2022, 05:38:47 pm »
Point being that just because one starts with digitalWrite() and analogWrite() and delay() doesn't mean they're stuck there forever. It's just a first step. Many hobbyists don't need to go any farther than straight Arduino libraries and remain ignorant of how the MCU really works. That's fine because their applications don't require any more than that. Some gradually learn about AVR or ARM or whatever and exploit it. Some move on to VS Code and Platformio. Some eventually start designing their own boards and code bare metal. Good for them. Whatever works. And starting in the shallow end is proven to work.

And that is what everyone, who is not sick, does with 32-bit microcontrollers and their more complex peripherals.
One thing is to directly control the registers of an 8-bit microcontroller and quite another to do it with a 32bit one. That's a big deal. Most of them use libraries just like in the Arduino environment.
 
The following users thanked this post: eugene

Offline eugene

  • Frequent Contributor
  • **
  • Posts: 494
  • Country: us
Re: Newbie, What is the right way to learn programming?
« Reply #19 on: July 22, 2022, 05:54:52 pm »
Point being that just because one starts with digitalWrite() and analogWrite() and delay() doesn't mean they're stuck there forever. It's just a first step. Many hobbyists don't need to go any farther than straight Arduino libraries and remain ignorant of how the MCU really works. That's fine because their applications don't require any more than that. Some gradually learn about AVR or ARM or whatever and exploit it. Some move on to VS Code and Platformio. Some eventually start designing their own boards and code bare metal. Good for them. Whatever works. And starting in the shallow end is proven to work.

And that is what everyone, who is not sick, does with 32-bit microcontrollers and their more complex peripherals.
One thing is to directly control the registers of an 8-bit microcontroller and quite another to do it with a 32bit one. That's a big deal. Most of them use libraries just like in the Arduino environment.

I hear that. I have poured over the 3500 page reference manual for the Cortex-M7 in the Teensy 4.0 in an effort to exploit the amazing timers that are available. I eventually ended up using a hardware specific library...

OTOH, not too long ago I needed to output a stream of well-timed pulses with a Raspberry Pi. Well-timed and RPi are mutually exclusive, so I looked for a cheap IC to do the job. What I found was a little 8 bit AVR. Reading the datasheet, setting up the timers and bit-banging SPI-like IO bare metal was so easy. I'd forgotten that programming could be fun.
90% of quoted statistics are fictional
 

Offline andy3055

  • Super Contributor
  • ***
  • Posts: 1111
  • Country: us
Re: Newbie, What is the right way to learn programming?
« Reply #20 on: July 22, 2022, 06:36:13 pm »
A lot of great advice so far. I take a simple approach in learning things. I would start with BASIC and learn the concepts that will apply to any language. Learn how to make flow charts as they are very useful and important in structuring what you want to accomplish.  After that, moving to C will be easy. Just my take on the subject. Good luck.
 

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 3711
  • Country: nl
Re: Newbie, What is the right way to learn programming?
« Reply #21 on: July 22, 2022, 06:49:33 pm »
And that is what everyone, who is not sick, does with 32-bit microcontrollers and their more complex peripherals.
One thing is to directly control the registers of an 8-bit microcontroller and quite another to do it with a 32bit one. That's a big deal. Most of them use libraries just like in the Arduino environment.

I would then like to hear your definition of sick.

For me I don't see a big difference between the common peripherals in an 8 bitter and a 32 bitter. Sure there are more complex peripherals and you need to study a bit harder to make use of them, but does that make you "sick" if that is what excites you?

I would not recommend it to a beginner, but with step by step learning it can come in reach. And I agree with starting in the shallow end of the pool, and for me that is first learn what you are playing with, and that is the microprocessor itself. Not learning a programming language directly but first the basics of how a computer works.

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: Newbie, What is the right way to learn programming?
« Reply #22 on: July 22, 2022, 09:06:29 pm »
Traditionally, I think there were two ways to learn "programming."

The Top-Down approach.  Go to school and take Computer Science classes for about 4 years, along with the math, science, and EE classes needed to understand the underpinnings, and some social science and humanities classes so that you're aware of some of the human factors, or at least are somewhat able to communicate. Learn a language or two, and stuff about algorithms, data structures, architectures, operating systems, logic, numeric methods, compiler design, and etc.  Graduate with "experience" that varies wildly depending on how many extracurricular or "above and beyond" projects you did.  Struggle with job interviews where technical people conclude that you seem pretty smart, but don't have the experience to tackle the problem that they want solved RIGHT NOW.  :-(

The Bottom-Up approach.  Leap in and start copying code for those Arduinos.  Type in ancient BASIC games for the Commodore-64 emulator you can run on your Raspberry Pi.  Figure out how that stuff works, and start modifying that copied code, improving it, writing your own code, expanding what you know and how much you know about it.  Read a bunch of stuff online and become well acquainted with the perceived strengths and weaknesses of half-a-dozen languages and half-a-dozen CPUs.   Get really good at optimizing C code, so you can talk knowledgibly about how awful digitalWrite() in Arduino's RP2040/MBed core is.  Analyze other peoples' code, and let them ruthlessly analyze yours.  Figure out which advice is good, and which is bad.  Hopefully, read up on (or take online classes in) some of those topics that the CS majors are learning in their classes.  Struggle with getting your resume past HR screening that tags it "no relevant degree." :-(

Nominally, those used to be called "Analyst" and "Coder."  An Analyst would take a problem and create an outline of an algorithm to solve it (perhaps down to "flowchart" level), and then a "Coder" would convert that to code, taking into account the details of languages and how to actually compile, prettying up the output, and etc.  This was supposedly especially prevalent in Business Programming with COBOL - there were "vocational" "programming" classes that turned out "programmers" in not-much-time, without much CS (or other tech) background.

I'd sort-of like to add a THIRD methodology, which I think has become more prevalent as programming has "spread", and perhaps is more applicable to embedded software.   Here, you replace some of the generalized knowledge-gathering with material applicable to a particular application area.  So instead of learning all about queuing algorithms, or all about C on ARM64, you learn "network", "game design", "robotics", or "HVAC technology."  That way, instead of understanding the tools you MIGHT use, you understand the PROBLEMS you might face (beyond getting the cursed program to do what you wanted it to do.)  I vaguely suspect that this is something that is only possible because the first two ways have made some things easy, that used to be difficult.


Christe4nM's post was really good...

 
The following users thanked this post: nctnico, DiTBho, tinfever

Offline LM21

  • Regular Contributor
  • *
  • Posts: 95
  • Country: fi
Re: Newbie, What is the right way to learn programming?
« Reply #23 on: July 25, 2022, 08:15:44 pm »
One good way to learn is really with Arduino boards, but an other easy way is to download and install an IDE for Windows programming, if you are using a Windows PC, that is. That is a start, when you can print something on the screen.
 

Offline DiTBho

  • Super Contributor
  • ***
  • Posts: 3915
  • Country: gb
Re: Newbie, What is the right way to learn programming?
« Reply #24 on: July 25, 2022, 08:32:05 pm »
Learn *what* to do *what*? First, define your purpose.
Answers like the above by westfw are the best answer.
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline cvriv

  • Frequent Contributor
  • **
  • Posts: 275
  • Country: us
Re: Newbie, What is the right way to learn programming?
« Reply #25 on: July 26, 2022, 03:18:10 pm »
I don't think you should learn how to program microcontrollers using C right now.

I'd recommend that you learn how to program microcontrollers using assembly first. That's going to be a lot right there, if you don't know anything about electronics and digital systems.

I started with a PIC16F883 controller. It's cheap, has many internal peripherals for you to mess around with, and has an easy to follow datasheet, as well as a manual that will explain things in even further detail. EVERYTHING is in the datasheet and manual for you. There will probably be some things you won't understand. There's a book called Digital Systems, 12th edition. It will teach you all about logic levels, logic gates, truth tables, Karnaugh maps, Boolean algebra etc. It's a great book and will help you out.

Once you learn how to do all that, and get good at it, you can start learning C++. Once you learn C++ and get good at it... you can then bridge the two together and start programming controllers using C.

Learning to program micro controllers with C immediately is craziness. You can probably do some stuff... but you really won't understand what your really doing at all.     

 
 

Offline abquke

  • Regular Contributor
  • *
  • Posts: 128
  • Country: us
Re: Newbie, What is the right way to learn programming?
« Reply #26 on: July 26, 2022, 09:25:50 pm »
It's more straightforward to use assembly when one's learning how microcontrollers work since there's less in between hardware documentation and a project.

The "stuff" in between editing an assembly file and having a binary to put on a chip is a little more transparent too. Most of the difficulty in writing C for a microcontroller is getting the toolchain to work.

I started with doing assembly with TI MSP430s which had the benefit of being ARM-esque. When getting started the chip you have in front of you that works is the best chip to use. Once you can see similarities and differences between chips to make a decision, then you can make a decision.
 

Offline tellurium

  • Regular Contributor
  • *
  • Posts: 231
  • Country: ua
Re: Newbie, What is the right way to learn programming?
« Reply #27 on: July 27, 2022, 06:05:47 pm »
Once you learn how to do all that, and get good at it, you can start learning C++. Once you learn C++ and get good at it... you can then bridge the two together and start programming controllers using C.

Learning to program micro controllers with C immediately is craziness. You can probably do some stuff... but you really won't understand what your really doing at all.     

Advising for Assembly -> C++ -> C is .. questionable. C++ is a way more complex and non-obvious language than C.

And I don't see anything crazy or wrong in learning C straight away. In the end, C is a cross-platform assembly, that's what it is.
Open source embedded network library https://mongoose.ws
TCP/IP stack + TLS1.3 + HTTP/WebSocket/MQTT in a single file
 
The following users thanked this post: Siwastaja

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: Newbie, What is the right way to learn programming?
« Reply #28 on: July 27, 2022, 06:46:30 pm »
There is no way on earth I would recommend learning assembly language for an ARM device as a first go at programming.

I also would caution against assembly language for mid-range PICs.  They don't have very many instructions and extremely limited addressing formats along with an extremely limited stack and no PUSH/POP op codes, bottom line:  they are UGLY to program.  Keeping track of paging and banking is a PITA!  And yes, I have spent a good deal of time with the 16F877(A) and 16F827.  But that was 20 years ago and I'm not going to use them again.

The AVR ATmega128 is a reasonable architecture and assembly programming isn't overly difficult but why bother?  C works very well with these ATmegas.

For certain incantations of ARM processors, the startup code must be written in assembly language.  crt.s (or crt.S) is the usual file for this code.  You don't want to write a lot of code in this file.  Just branch to the C code and call it good.  This is how it's usually done anyway.  Only the most brief startup functions (set up .data and clear .bss) and certain interrupt vectors are contained here.  The less the better!

IBM used to guesstimate 100 lines of debugged code per day.  Those 100 lines can do a little (assembly) or a lot (FORTRAN), take your pick.
« Last Edit: July 27, 2022, 06:51:03 pm by rstofer »
 
The following users thanked this post: nctnico, newbrain

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 3711
  • Country: nl
Re: Newbie, What is the right way to learn programming?
« Reply #29 on: July 27, 2022, 07:06:05 pm »
For a basic understanding of how a microprocessor works learning a simple assembler is certainly useful in learning how to program, but I second that going for ARM assembly is not good advice.

And indeed assembler -> C++ -> C is not the way to go.

In one of the first posts in this thread I advised to first study how a microprocessor work since the original poster wants to learn embedded programming, and to my opinion knowing how a microprocessor with its peripherals work will give you an advantage over just starting with programming in C.

Learning how assembly works is part of the process, also to my opinion. You don't have to become a master in assembly programming first, but at least know about the basics.

Then move on to studying C. And with that I mean the basics of C, like variables, if/else statements, for and while loops etc. Do not dive into library based functions just yet, like printf and atoi and what more you have, that are additions based on basic C.

See if you can write some simple program on a micro controller where you output something to a uart with just plain C.

That is how I learned to program. And take small steps just as someone else here advised.

Edit: I wonder if the original poster is taking in any of this? Being that his original post was his first and only one, and he was last active on the 23th of this month.
« Last Edit: July 27, 2022, 07:09:42 pm by pcprogrammer »
 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 26907
  • Country: nl
    • NCT Developments
Re: Newbie, What is the right way to learn programming?
« Reply #30 on: July 27, 2022, 07:18:46 pm »
There is no way on earth I would recommend learning assembly language for an ARM device as a first go at programming.
Actually, starting on an embedded platform to learn programming is a recipy for a dissaster. Better start to learn programming on a PC first. Command line tools are very close to embedded software as there is no graphical user interface. Also, a lot of embedded software is developed & tested on a PC because there will be much more debugging and testing facilities.

I agree with the people stating that programming consists of two things you'll need to learn: 1) how to translate a problem into a structured flow (using flowcharts / UML / whatever) and knowing the programming language inside out.
« Last Edit: July 27, 2022, 07:20:44 pm by nctnico »
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 
The following users thanked this post: newbrain

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14488
  • Country: fr
Re: Newbie, What is the right way to learn programming?
« Reply #31 on: July 27, 2022, 07:39:14 pm »
Agreed. While the question itself was a bit too wide, and even seasoned CS teachers may have a hard time giving a fully satisfying answer in just a few lines, learning "programming" directly on microcontrollers sounds like a bad idea and usually leads to poor software developers with bad habits.

But to even answer the question, we'd need to know what the OP's goal is to begin with. Maybe they don't care about becoming a poor software developer and they just want to make some LEDs blink, or something.
 

Offline tellurium

  • Regular Contributor
  • *
  • Posts: 231
  • Country: ua
Re: Newbie, What is the right way to learn programming?
« Reply #32 on: July 27, 2022, 07:47:27 pm »
That's one of the advises on the topic, which I bumped at in 90s, which I follow to date.

A piece of documentation to the Merc MUD, written in summer 1993, https://github.com/alexmchale/merc-mud/blob/master/doc/hacker.txt
Open source embedded network library https://mongoose.ws
TCP/IP stack + TLS1.3 + HTTP/WebSocket/MQTT in a single file
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: Newbie, What is the right way to learn programming?
« Reply #33 on: July 28, 2022, 04:10:38 pm »
Agreed. While the question itself was a bit too wide, and even seasoned CS teachers may have a hard time giving a fully satisfying answer in just a few lines, learning "programming" directly on microcontrollers sounds like a bad idea and usually leads to poor software developers with bad habits.

But to even answer the question, we'd need to know what the OP's goal is to begin with. Maybe they don't care about becoming a poor software developer and they just want to make some LEDs blink, or something.

In some ways, I think the Raspberry Pi is perfect for learning both application programming and embedded programming (such as it is on the Pi).

Software development is much easier on a Linux workstation and the Pi provides that.  Given one of the Starter Kits, embedded programs are also workable.  Certainly not bare metal and, yes, there are some layers in the path but none of that matters in the vast majority of applications.  I would go for the Pi 400 but the Pi 4 is certainly workable.

If you simply must use Windows, SSH is enabled on the Pi by default.  Use Putty on Windows to make a SSH connection to your Pi (although getting the IP address may be a problem the first time around).  Later, use VNC to get a graphics windows on Windows of the PI desktop.



This kit is particularly useful as all the code is given in C, Python, Java and Scratch.  There are many interesting and useful projects to build:

https://www.amazon.com/Freenove-Complete-Raspberry-708-Page-Tutorials/dp/B09ZXNL2WH

That still leaves a large gap that I'll call 'design'.  That comes with experience, both bad and good.
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14488
  • Country: fr
Re: Newbie, What is the right way to learn programming?
« Reply #34 on: July 28, 2022, 06:50:38 pm »
In some ways, I think the Raspberry Pi is perfect for learning both application programming and embedded programming.

RPI is not embedded programming, it's a GNU/Linux computer running a GLIBC-based rootfs.
Sure, you can use GPIO, spi, i2c, etc, but this doesn't make it an embedded device.

Embedded is a vague term. Yes, the RPi can be considered an "embedded" target, but no, it is about nothing like a microcontroller, for instance.
There are ways to program it bare metal, but this is way, way beyond any beginner's capabilities (and even those of many experienced developers!)

I for one find some answers rather strange here, but probably coming from the fact the question was posted in a "Microcontrollers" section.
The easiest, most comfortable and more flexible way of learning "programming" is to simply use a PC. Almost everyone has one these days, and tools (even free ones) are abundant. Free resources are abundant as well. You can basically start with zero cost and actually learn something. Go "embedded" if that's a goal of yours only after you have at least mastered the basics.
 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 26907
  • Country: nl
    • NCT Developments
Re: Newbie, What is the right way to learn programming?
« Reply #35 on: July 28, 2022, 07:52:24 pm »
All utter nonsense. Only people that like being in a world of pain would adhere to such restrictions (and use OpenWRT nowadays). You have to be aware that crutches like ucLibc where invented to deal with limited storage which was an issue >15 years ago. Nowadays such limitations no longer apply and hence the need for crutches is no longer there.

Any computing system inside a device that isn't a desktop PC or server, is embedded.
« Last Edit: July 28, 2022, 07:55:06 pm by nctnico »
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 
The following users thanked this post: Siwastaja

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: Newbie, What is the right way to learn programming?
« Reply #36 on: July 29, 2022, 02:28:43 pm »
Any computing system inside a device that isn't a desktop PC or server, is embedded.

Your definition becomes all the more correct if we include systems such as the Jetbot Nano which, while running a full version of Ubuntu, is aimed at some variant of AI where the CUDA cores are working at a parallel solution to some gigantic matrix problem such as computer vision route following.  Independent of the OS, the Nano can achieve 472 GFLOPS (16 bit FP) with 128 CUDA cores.

Many years ago, it wasn't uncommon to see laptops dedicated to small(ish) robots.  I would argue that the primary purpose of the laptop was control and that would make it 'embedded'.  By my defintion...

It isn't easy to differentiate or classify embedded versus non-embedded.  Large AI projects may indeed use a server for the number crunching and some variant of networking for communication.  The application is still 'embedded' if the purpose of the system is something like facial recognition.


 

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 3711
  • Country: nl
Re: Newbie, What is the right way to learn programming?
« Reply #37 on: July 29, 2022, 03:21:29 pm »
Any computing system inside a device that isn't a desktop PC or server, is embedded.

Wikipedia agrees with this: https://en.wikipedia.org/wiki/Embedded_system

The first sentence of the Dutch version of it might be interpreted as such that it includes your every day computer too. https://nl.wikipedia.org/wiki/Embedded_system

Quote
Een embedded system (ook wel ingebed systeem of geïntegreerd systeem) is een elektronisch systeem (hardware én software) dat is geïntegreerd in gebruiksartikelen of apparaten, met de bedoeling deze een vorm van intelligent gedrag te bezorgen.

Quote
An embedded system (also integrated system) is an electronic system (hardware and software) that is integrated into consumables or appliances, with the intent to give them a form of intelligent behavior.

Some might say that this is what a computer does.

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5913
  • Country: es
Re: Newbie, What is the right way to learn programming?
« Reply #38 on: July 29, 2022, 03:22:28 pm »
Good old pic16f84 books, that's where I started!
No magic libraries, no "do_everything();"  :D
« Last Edit: July 29, 2022, 03:24:00 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 8179
  • Country: fi
Re: Newbie, What is the right way to learn programming?
« Reply #39 on: July 29, 2022, 03:33:31 pm »
Raspberry Pi becomes embedded when you build a WiFi connected coffeemaker out of it. If you connect a monitor and keyboard (or through SSH) and use it as a programming learning environment, then it's not embedded, except that if your aim is to make the coffeemaker after all, then it maybe is embedded. Go figure.

Embedded is one of those relatively useless and potentially confusing terms. This is why I say microcontroller when I mean microcontroller, and say single-board computer when I mean a general purpose computer which runs a general purpose OS.

These terms are much more well-defined (not perfectly of course) and have handy TLAs, namely MCU and SBC.
 

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 3711
  • Country: nl
Re: Newbie, What is the right way to learn programming?
« Reply #40 on: July 29, 2022, 03:49:27 pm »
Raspberry Pi becomes embedded when you build a WiFi connected coffeemaker out of it. If you connect a monitor and keyboard (or through SSH) and use it as a Embedded is one of those relatively useless and potentially confusing terms. This is why I say microcontroller when I mean microcontroller, and say single-board computer when I mean a general purpose computer which runs a general purpose OS.

But what do you call a board with a micro-controller? And when does a board become a computer?

Technically speaking the chip on a raspberry pi is a micro-controller. It has a CPU and peripherals, which somewhat is the definition of a micro-controller.

And with the rather powerful micro-controllers like on a raspberry pico it is possible to drive a vga screen and turn it into a computer. Not a good one to run linux on, but might be possible.

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14488
  • Country: fr
Re: Newbie, What is the right way to learn programming?
« Reply #41 on: July 29, 2022, 07:02:37 pm »
Not sure debating about what "embedded" really entails is that fruitful, but hey. Here goes.

Words have a meaning, and common sense supplements them.
What is commonly called "embedded" is the *embedded* CPU or more generally "computing resources" in any device *the purpose of which is not computing per se*. (There is some computing resource embedded in something else, that's the whole idea.)

While many people assume that "embedded" is basically something around either a MCU, or a simple SoC, and no OS or some kind of dedicated low-level RTOS, this is just a narrow view. For instance, some scopes of the 2000's were based on a mini-ITX motherboard, a x86 CPU and Windows 2000 or XP. Would you not call that "embedded"? Examples abound. Many avionics devices look more like full-fledged computer-like motherboards than small boards based on some MCU, yet they perfectly qualify as "embedded". They also often run some OSs that are way more complex than say, FreeRTOS.

 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4040
  • Country: nz
Re: Newbie, What is the right way to learn programming?
« Reply #42 on: July 29, 2022, 11:36:29 pm »
There is no way on earth I would recommend learning assembly language for an ARM device as a first go at programming.

The basics of the ARM assembly languages are among the better ones. If you can find something with an ARM7TDMI then that's fairly simple to learn on. Hard to know whether A32 mode or T16 mode is better to start with, as they both have strong and weak points. But either is massively better than the complexity of ARMv7 / T32.

It's usually the hardware setup stuff that kills you on modern ARM SoCs.

Cortex M0, and the Pi Pico, are probably a good modern, easily obtained, place to start.

Quote
I also would caution against assembly language for mid-range PICs.  They don't have very many instructions and extremely limited addressing formats along with an extremely limited stack and no PUSH/POP op codes, bottom line:  they are UGLY to program.  Keeping track of paging and banking is a PITA!

A restricted ISA isn't a bad thing in itself. A beginner should mostly use their time to learn how to use the available tools, not learning what the tools are. They shouldn't be wondering if they've missed something, or if there is some cool construct/instruction that does exactly what they want but they just haven't found it yet. They shouldn't be wondering what is legal and what is illegal.

But PIC goes too far, and in a very awkward direction that doesn't fit modern programming practices.

A week or two ago I did an exercise where I reduced the already pretty slim (37 instructions) RV32I instruction set down to just 10 (ten) instructions that are still sufficient to write absolutely any program, or to compile C to. Except for SB/SH (which need more) each missing instruction can be replaced by 2-4 of the remaining instructions.

I hand-altered some compiled C code (e.g. my count primes benchmark) to use only the 10 instructions and found the code size increase was less than 30% and the execution time increase much less than that (as most of the extra code was preloading constants outside a loop, for use inside the loop).

The instructions:

LW, SW Rd, imm(Rb): load/store with register base and 12 bit signed offset
ADDI Rd, Rs, imm: add 12 bit signed immediate constant
ADD, NAND, SLL, SRA: Rd = Rs1 op Rs2 register to register ALU operations
JAL Rd,.+imm, JALR Rd,imm(Rb): unconditional branches
BLT Rs1,Rs2,.+imm: conditional branch

That's a really small instruction set to be both universal and to offer code size and speed close to real world instruction sets.

(NAND of course isn't a standard RV32I instruction, but is replacing AND, OR, XOR. I originally included BLTU until I realised you can emulate it by simply adding 0x80000000 to both values and then use BLT)

Working 10-instruction subset assembly language source for my Primes benchmark is here: https://hoult.org/primes.S (I didn't convert main(), just countPrimes())

The original C source is here: https://hoult.org/primes.txt

Quote
The AVR ATmega128 is a reasonable architecture and assembly programming isn't overly difficult but why bother?  C works very well with these ATmegas.

Yes, a good choice for first adventures in assembly language programming, up there with RISC-V and ARM and MSP430.

I think C working well with an ISA is a basic prerequisite for bothering with the ISA at all, these days. If a compiler can use it easily then probably so can a human.

The one thing about learning programming, whether C or asm, on a microcontroller is the difficulty of observing execution. If you can attach a debugger and single-step then that's good, otherwise it can be best to learn using an emulator with instruction-by-instruction trace output.


Quote
IBM used to guesstimate 100 lines of debugged code per day.  Those 100 lines can do a little (assembly) or a lot (FORTRAN), take your pick.

The correct figure is ten

See "The Mythical Man Month" from 1975, by the manager of the software when the IBM 360 was developed.
 
The following users thanked this post: tellurium

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: Newbie, What is the right way to learn programming?
« Reply #43 on: July 30, 2022, 12:09:45 am »
Quote
The basics of the ARM assembly languages are among the better ones.
Perhaps the original ARM32 instruction set.
Thumb (eg all the Cotex-M microcontrollers) is less good.  Obfuscated instruction encodings.
The Cortex-M0 subset of Thumb is pretty unpleasant.  Lack of orthogonality and severe limits on operand ranges :-(
(Also, there tends not to be very much ASM example code for ARM (or any of the modern RISC architectures.  Part of the whole RISC thing is that humans aren't supposed to need to deal with ASM.)

The MSP430 is pretty nice, but it's an older CISC-like architecture.
AVR isn't bad for a RISC-like architecture, but it's 8bits.  Lots of existing material, though.
MIPS seems to be used by a lot of "computer architecture" classes, which might be a better learning path than simply "assembly language programming."  But it's sort-of dying out (being replaced by RISC-V?)
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4040
  • Country: nz
Re: Newbie, What is the right way to learn programming?
« Reply #44 on: July 30, 2022, 01:19:19 am »
Quote
The basics of the ARM assembly languages are among the better ones.
Perhaps the original ARM32 instruction set.
Thumb (eg all the Cotex-M microcontrollers) is less good.  Obfuscated instruction encodings.
The Cortex-M0 subset of Thumb is pretty unpleasant.  Lack of orthogonality and severe limits on operand ranges :-(

As I said "they both have strong and weak points".

Original ARM32 has a relatively small number of different instruction formats, and is quite orthogonal. If you think you should be able to do something then you can. But it's got the complexity of conditional execution in every instruction and the "free" shift/rotate in a lot of them.

Thumb1 has as you say a lack of orthogonality -- there are something like 20 different instruction formats, each with annoying restrictions -- and dealing with things such as hi vs lo registers is annoying.

Quote
Part of the whole RISC thing is that humans aren't supposed to need to deal with ASM.)

I've never agreed with that.

Some early RISC CPUs had programmer-unfriendly features such as branch delay slots and more generally no pipeline interlocks, so you had to count the number of instructions and clock cycles between an instruction that generated a result and the first instruction that used that result, and if you didn't leave enough gap then you just silently got garbage. THAT is the origin of the "humans aren't expected to program this by hand" meme, but it was actually just a terrible design in general because 1) compilers too often had to bloat the program size by putting in NOPs because there was no useful work that could be done while waiting, and more importantly 2) when you made a 2nd or 3rd generation machine with the same instruction set it had a different pipeline design and the rules imposed by the first design were simply not the right rules any more.

Once you added pipeline interlocks so the machine would just stall for a couple of cycles if needed, so you could actually build multiple generations of hardware that ran the same code, the programmer-unfriendliness went away too.

The remaining "programmer unfriendly" aspect of RISC is just the lack of memory-to-memory operations and complex addressing modes. Most of that went away once you got enough registers that all your function arguments and local variables can be in registers not in the stack frame. 32 registers is almost always more than enough for that, but 16 usually is too. VAX and M68k probably should have used a register-based ABI (as ARM32 and x86_64 both do with 16 registers).

If you want to code a = b + c on a machine with stack-based ABI then you want to have instructions like add b(SP), c(SP), a(SP). On a RISC machine with the same stack-based ABI you need load t1,b(SP); load t2,c(SP); add t1,t1,t2; store t1,a(SP). Which is admittedly pretty awful. But once you have a register-based ABI it's just add a,b,c, which is fine, and in fact I'd say *easier* than the CISC way.
 

Offline DiTBho

  • Super Contributor
  • ***
  • Posts: 3915
  • Country: gb
Re: Newbie, What is the right way to learn programming?
« Reply #45 on: July 30, 2022, 07:42:51 am »
try programing in assembly a MIPS4 machine like the R18200  :D
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline tellurium

  • Regular Contributor
  • *
  • Posts: 231
  • Country: ua
Re: Newbie, What is the right way to learn programming?
« Reply #46 on: July 30, 2022, 07:48:23 am »
A week or two ago I did an exercise where I reduced the already pretty slim (37 instructions) RV32I instruction set down to just 10 (ten) instructions that are still sufficient to write absolutely any program, or to compile C to. Except for SB/SH (which need more) each missing instruction can be replaced by 2-4 of the remaining instructions.

I hand-altered some compiled C code (e.g. my count primes benchmark) to use only the 10 instructions and found the code size increase was less than 30% and the execution time increase much less than that (as most of the extra code was preloading constants outside a loop, for use inside the loop).

The instructions:

LW, SW Rd, imm(Rb): load/store with register base and 12 bit signed offset
ADDI Rd, Rs, imm: add 12 bit signed immediate constant
ADD, NAND, SLL, SRA: Rd = Rs1 op Rs2 register to register ALU operations
JAL Rd,.+imm, JALR Rd,imm(Rb): unconditional branches
BLT Rs1,Rs2,.+imm: conditional branch

That's remarkable!
Is there a reason for that exercise other than curiosity? Like, to build a minimal RV core on an FPGA?
Open source embedded network library https://mongoose.ws
TCP/IP stack + TLS1.3 + HTTP/WebSocket/MQTT in a single file
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4040
  • Country: nz
Re: Newbie, What is the right way to learn programming?
« Reply #47 on: July 30, 2022, 09:09:16 am »
A week or two ago I did an exercise where I reduced the already pretty slim (37 instructions) RV32I instruction set down to just 10 (ten) instructions that are still sufficient to write absolutely any program, or to compile C to. Except for SB/SH (which need more) each missing instruction can be replaced by 2-4 of the remaining instructions.

I hand-altered some compiled C code (e.g. my count primes benchmark) to use only the 10 instructions and found the code size increase was less than 30% and the execution time increase much less than that (as most of the extra code was preloading constants outside a loop, for use inside the loop).

The instructions:

LW, SW Rd, imm(Rb): load/store with register base and 12 bit signed offset
ADDI Rd, Rs, imm: add 12 bit signed immediate constant
ADD, NAND, SLL, SRA: Rd = Rs1 op Rs2 register to register ALU operations
JAL Rd,.+imm, JALR Rd,imm(Rb): unconditional branches
BLT Rs1,Rs2,.+imm: conditional branch

That's remarkable!
Is there a reason for that exercise other than curiosity? Like, to build a minimal RV core on an FPGA?

Mostly just interest in how minimal you can go without suffering too much. Maybe with an idea to build something in 7400 logic sometime?

We know we can build a one-instruction computer using, for example, SUBLEQ. But it's awfully inconvenient to use and you lose more than an order of magnitude of speed compared to normal ISAs. How low can you go while staying reasonably fast, and fully supporting C++?


Logic gates isn't the only resource I tried to minimise. Instruction opcode space is also valuable. The cost to have AND, OR, XOR ... and SUBTRACT ... is pretty minimal in gates.  And whatever ALU operations you have, providing immediate operand versions as well as register operand versions uses an extremely minimal number of gates. But it uses a lot of opcode space. With 5 bit register fields and 12 bit immediate operand fields, every ANDI, ORI, XORI uses the same amount of opcode space as 128 AND, OR, XOR register-to-register instructions.  And yet ORI and XORI are almost never used.  On the other hand ADDI gets a huge amount of use: incrementing and decrementing counters and pointers, allocating and deallocating stack frames, loading constants, copying one variable to another (add #0)

With only 10 instructions, you're *almost* down to only needing a 3 bit opcode field. Could these instructions be encoded in a smaller way than RV32I's 32 bit instructions?
 
The following users thanked this post: tellurium

Offline DiTBho

  • Super Contributor
  • ***
  • Posts: 3915
  • Country: gb
Re: Newbie, What is the right way to learn programming?
« Reply #48 on: July 30, 2022, 09:22:31 am »
Which are the minimalist instructions ever? Like ijvm?
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4040
  • Country: nz
Re: Newbie, What is the right way to learn programming?
« Reply #49 on: July 30, 2022, 09:43:57 am »
Which are the minimalist instructions ever? Like ijvm?

According to the wikipedia page, that's got 28 instructions! And without any kind of shift or rotate -- my 10 instruction ISA at least has 2 instructions dedicated to shifts.

I don't have HALT or IN/OUT but I can do everything else. I/O in modern machines is done with memory-mapped I/O registers not special instructions. For HALT you can probably just substitute an infinite loop.

 

Offline DiTBho

  • Super Contributor
  • ***
  • Posts: 3915
  • Country: gb
Re: Newbie, What is the right way to learn programming?
« Reply #50 on: July 30, 2022, 10:50:05 am »
my 10 instruction ISA

can you list instructions?
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4040
  • Country: nz
Re: Newbie, What is the right way to learn programming?
« Reply #51 on: July 30, 2022, 11:00:46 am »
my 10 instruction ISA

can you list instructions?

I did. And showed a real program written using them.

https://www.eevblog.com/forum/microcontrollers/what-is-the-right-way-to-learn-programming/msg4328953/#msg4328953

Except for NAND, they are all identical to the corresponding existing instructions in RV32I.
« Last Edit: July 30, 2022, 11:03:31 am by brucehoult »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf