ProDigit
Posts: 374
Joined: Tue Aug 30, 2011 1:24 am

Raspberry pi for coding on machine level?

Sat Jul 20, 2019 7:54 am

Would you recommend coding for the Raspberry Pi, on a machine code level?
I realize that machine code is very device specific.
And since the Pi has been created in the millions (probably sold more than any PC, phone, or even Mac), the highest chance to learn how to work with machine code, is to do it on a Pi (I'd say).

Thoughts?

jahboater
Posts: 4831
Joined: Wed Feb 04, 2015 6:38 pm

Re: Raspberry pi for coding on machine level?

Sat Jul 20, 2019 8:12 am

Assuming by "machine code level" you mean assembly language, then yes the Pi is great for that.

The official OS called Raspbian comes with an excellent assembler already installed and ready to use.

https://www.raspberrypi.org/downloads/raspbian/

Also included is a C/C++ compiler called GCC.

You may assemble and run small programs standalone (with "as" and "ld"), or use the C compiler which will do the assembly and link in one go. You can also run small pieces of assembler "inline" within C programs.
Here is a little example to get you started (ARM 32-bit assembler):

Code: Select all

@
@  Print Hello world
@
    .text
    .global _start

_start:
    mov  r7,#4        @ write system call
    mov  r0,#1        @ file (stdout)
    adr  r1,message
    mov  r2,#14       @ message length
    svc  #0

    mov  r7,#1        @ exit system call
    mov  r0,#0        @ return code
    svc  #0

message:
    .ascii "Hello, world!\n"

Code: Select all

[email protected]:~ $ as hello.s -o hello.o
[email protected]:~ $ ld hello.o -o hello
[email protected]:~ $ ./hello
Hello, world!
Last edited by jahboater on Sat Jul 20, 2019 8:27 am, edited 2 times in total.

ProDigit
Posts: 374
Joined: Tue Aug 30, 2011 1:24 am

Re: Raspberry pi for coding on machine level?

Sat Jul 20, 2019 8:26 am

Thanks for the quick and informative response!

Is there a sort of community where examples like these can be found, and where people can share their experiences (more for beginners; and specific to the Pi)?

User avatar
Michiel O.
Posts: 178
Joined: Mon Dec 12, 2016 12:06 pm

Re: Raspberry pi for coding on machine level?

Sat Jul 20, 2019 8:28 am

ProDigit wrote: Thoughts?
This is exactly the reason I bought a Raspberry Pi 3. I use it for other things now, too.

I have some debugging tips for you:

gdb hello
b _start
run
display/i $pc
si
...repeatedly press Enter to step through the program


Alternatively, you can use the graphical 'ddd' debugger to step through the program.

sudo apt-get install ddd
ddd hello


More details on the page I wrote about this.
"You can't actually make computers run faster, you can only make them do less." - RiderOfGiraffes

jahboater
Posts: 4831
Joined: Wed Feb 04, 2015 6:38 pm

Re: Raspberry pi for coding on machine level?

Sat Jul 20, 2019 8:30 am

ProDigit wrote:
Sat Jul 20, 2019 8:26 am
Thanks for the quick and informative response!

Is there a sort of community where examples like these can be found, and where people can share their experiences (more for beginners; and specific to the Pi)?
This forum is a great place for help and chat about Pi specific issues. Beginners are welcome.
There is a separate section for "Bare metal and assembler" topics.

Do feel free to ask any question however simple you might think it is.

jahboater
Posts: 4831
Joined: Wed Feb 04, 2015 6:38 pm

Re: Raspberry pi for coding on machine level?

Sat Jul 20, 2019 8:48 am

Michiel O. wrote:
Sat Jul 20, 2019 8:28 am
I have some debugging tips for you:
Try also "layout reg" (or just "la r" for short).

This splits the screen into three windows.
The top window contains the registers and their values with the last changed register highlighted.
The middle window contains the assembler with the next instruction highlighted.
The bottom windows is where you type commands (such as stepi (si))

This makes it very easy to step though a program watching the registers change as you go.
assemble with -g

gdb is included within Raspbain.
Last edited by jahboater on Sat Jul 20, 2019 8:53 am, edited 2 times in total.

ProDigit
Posts: 374
Joined: Tue Aug 30, 2011 1:24 am

Re: Raspberry pi for coding on machine level?

Sat Jul 20, 2019 8:52 am

As a side note,
Would you recommend learning to program on a Raspberry Pi, or on an old Windows laptop?
Are there many differences between coding on Python, or C++ in Linux (on a Pi) vs coding on an old Windows laptop?

The laptop I already own.
I can put Linux (Ubuntu or something) on it very easily.
A Pi, I'd have to order (and probably go with an older 3B model if I would order one.

jahboater
Posts: 4831
Joined: Wed Feb 04, 2015 6:38 pm

Re: Raspberry pi for coding on machine level?

Sat Jul 20, 2019 9:00 am

ProDigit wrote:
Sat Jul 20, 2019 8:52 am
As a side note,
Would you recommend learning to program on a Raspberry Pi, or on an old Windows laptop?
Are there many differences between coding on Python, or C++ in Linux (on a Pi) vs coding on an old Windows laptop?
My personal opinion is that its much easier on the Pi (compared to Windows). Linux on the laptop would be great too.

Python, C, and C++ are "native" languages on the Pi (Raspbian OS). Everything comes pre-installed and ready to use, including a class leading compiler supporting all the latest standards (C++17, C18 etc). Furthermore its all free!
There are a large number of support tools included as well (gdb being an obvious one, but there are countless others).
UNIX was designed by software engineers for software engineers - as a "software factory". Linux is the same.

If you need help on most things the "man" (short for manual) command will help directly.

So "man cos" will tell you all you need to know about the cosine function in C.
"man procfs" tells you about the /proc filesystem, "man stdio" etc etc.

Its all "just easy" on Linux.

Don't forget the raison d'etre of the Pi is to help people learn about these things.

The assembler is used by the compiler, so "as" is always present.
ProDigit wrote:
Sat Jul 20, 2019 8:52 am
A Pi, I'd have to order (and probably go with an older 3B model if I would order one.
For running small test programs to learn, any model of Pi will do.
For larger C++ programs, the latest model, the Pi4, will be much faster.
The 1GB version of the Pi4 is the same price as the older models.

hippy
Posts: 6230
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Raspberry pi for coding on machine level?

Sat Jul 20, 2019 12:30 pm

ProDigit wrote:
Sat Jul 20, 2019 8:52 am
Would you recommend learning to program on a Raspberry Pi, or on an old Windows laptop?
It depends on what type of programming you are seeking to learn, what experience you have of using either, and how familiar you are with each.

If you are looking at assembly language programming, low-level, system tool, I/O interfacing, or bare metal programming, you are almost certainly better off with a Pi. The Pi has everything you need included or easily installable. Getting the same set-up on Windows can be harder, often much harder.

For things like command line C or Python programs, either can suffice, but again it is probably easier to get modules you may need installed on the Pi.

For high-level programming you may find Windows better with tools such as Visual Studio, Eclipse, Android Studio, and the like easier to use on Windows.

Windows 10 has WSL, Windows Subsystem for Linux, which somewhat clouds the issue. That makes some things easier to do using Windows which otherwise might be done using Linux.

One can take advantage of the Windows system likely being faster, having more RAM and disk space, than most Pi computers. A modern Pi may however be just as good or better than an old laptop struggling under the weight of Windows.

The number one advantage of a Pi is that if you screw things up and render it unusable it's easy enough to start again. One tends not to want to take that risk with a Windows machine, especially one which may have other uses as well. And, if the worst happens, you destroy the Pi in your adventures, it's easy enough to replace and far less costly to do so.

User avatar
davidcoton
Posts: 4245
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: Raspberry pi for coding on machine level?

Sat Jul 20, 2019 1:14 pm

High level languages (Python, C++, C) are much the same on Windows or Linux -- even the library calls are largely the same, though there are some differences at the lowest levels.

OTOH Assembly code is completely different, although the principles are similar. Pis have ARM SOCs, while a Windows PC has (usually) an AMD64-based CPU. If you want to learn Assembler to use on one particular system, you may want to concentrate on that. As @hippy said, the Pi is a bit easier (and ultimately cheaper) to recover when you get things really wrong. If you're looking at a programming career, look at the job ads to see which skills are most in demand.
Signature retired

jahboater
Posts: 4831
Joined: Wed Feb 04, 2015 6:38 pm

Re: Raspberry pi for coding on machine level?

Sat Jul 20, 2019 1:29 pm

davidcoton wrote:
Sat Jul 20, 2019 1:14 pm
If you want to learn Assembler to use on one particular system, you may want to concentrate on that.
x86_64 is CISC, ARM and especially A64 (the Pi in 64-bit mode) are RISC. Quite different to learn.
But if you like assembler, and are interested in it, then it may be worth learning both. Its all good fun (and costs nothing apart from possibly a good book).
As @hippy said, the Pi is a bit easier (and ultimately cheaper) to recover when you get things really wrong
If you are writing simple programs to learn, I doubt very much if you will ever damage the Pi.
Its more likely to fail due to ill-advised tinkering with system config files. Or an overclock that's gone too far!

And then if the worst happens, re-flashing the SD card (a few minutes to 1/2 hour or so) will get you running again at no cost. You don't have to re-install lots of development software because its included within Raspbian.
Just make sure the programs you are currently working on, and any important config stuff, are kept in multiple places.

User avatar
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: Raspberry pi for coding on machine level?

Sat Jul 20, 2019 1:46 pm

ProDigit wrote:
Sat Jul 20, 2019 7:54 am
Would you recommend coding for the Raspberry Pi, on a machine code level?
I realize that machine code is very device specific.
And since the Pi has been created in the millions (probably sold more than any PC, phone, or even Mac), the highest chance to learn how to work with machine code, is to do it on a Pi (I'd say).

Thoughts?
It would be a lot easier than a PC to code for in machine code.

Though I would first recommend becoming very familiar with ARM Assembly language, before you attempt ARM Machine Language.

I am impressed at the interest. Not many want to use machine language anymore. There are still a lot of Assembly Language coders out there, though very few Machine Language programmers.
RPi = The best ARM based RISC OS computer around
More than 95% of posts made from RISC OS on RPi 1B/1B+ computers. Most of the rest from RISC OS on RPi 2B/3B/3B+ computers

jahboater
Posts: 4831
Joined: Wed Feb 04, 2015 6:38 pm

Re: Raspberry pi for coding on machine level?

Sat Jul 20, 2019 2:08 pm

DavidS wrote:
Sat Jul 20, 2019 1:46 pm
I am impressed at the interest. Not many want to use machine language anymore. There are still a lot of Assembly Language coders out there, though very few Machine Language programmers.
I "think" the OP meant assembler.
I have programmed Z80's over the years in hex, and wouldn't wish it on my worst enemy :(
The Z80 is very simple, it must be much harder on a modern CPU, quite a lot of research just to encode each instruction.

hippy
Posts: 6230
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Raspberry pi for coding on machine level?

Sat Jul 20, 2019 2:16 pm

DavidS wrote:
Sat Jul 20, 2019 1:46 pm
I am impressed at the interest. Not many want to use machine language anymore. There are still a lot of Assembly Language coders out there, though very few Machine Language programmers.
Are there any Machine Language Programmers doing that other than by being Assembly Language programmers ?

I would take it that most people, when they talk of Machine Language programming, they mean Assembly Language programming. Especially when asking about how to start programming.

Even though I have written compilers, assemblers, disassemblers, simulators, and the like, all of which require a detailed knowledge of the ISA, its bit-fields and the rest, I would never seriously consider Machine Language programming viable except for the most trivial test cases.

I would be a bit surprised if anyone faced with Machine Language programming, for whatever reason, did not make creating some kind of Assembler their first task.

ejolson
Posts: 3806
Joined: Tue Mar 18, 2014 11:47 am

Re: Raspberry pi for coding on machine level?

Sat Jul 20, 2019 2:58 pm

ProDigit wrote:
Sat Jul 20, 2019 7:54 am
Would you recommend coding for the Raspberry Pi, on a machine code level?
I realize that machine code is very device specific.
And since the Pi has been created in the millions (probably sold more than any PC, phone, or even Mac), the highest chance to learn how to work with machine code, is to do it on a Pi (I'd say).

Thoughts?
There is a free book on assembly-language programming written by a forum member here:

https://www.raspberrypi.org/forums/view ... 9&t=159409

That is where I would start, if I had an interest in ARM assembly.

W. H. Heydt
Posts: 11067
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Raspberry pi for coding on machine level?

Sat Jul 20, 2019 4:45 pm

ProDigit wrote:
Sat Jul 20, 2019 7:54 am
And since the Pi has been created in the millions (probably sold more than any PC, phone, or even Mac)
Since your actual question has been answered, this is a nitpick...

Total Pis sold to date: still under 30 million

PCs sold per year: roughly 100 million

Cell phones and tablets sold per year: roughly 1 billion

Musketeer
Posts: 152
Joined: Fri Feb 12, 2016 1:23 pm
Location: CZ

Re: Raspberry pi for coding on machine level?

Sat Jul 20, 2019 7:19 pm

I also thought about ASM but got persuaded on C and I am happy for that! ASM on modern ARM has above 1 thousand different instructions!

Android games engines are too in C++ (biggest market today).
Linux is like woman - both wants 180 % of your time...
You want speed Java 9.8x? Throw it out of some Window(s)!
My girlfriend is terribly unmature - she always sinks my boats in bathtub!

ProDigit
Posts: 374
Joined: Tue Aug 30, 2011 1:24 am

Re: Raspberry pi for coding on machine level?

Sun Jul 21, 2019 3:15 am

W. H. Heydt wrote:
Sat Jul 20, 2019 4:45 pm
ProDigit wrote:
Sat Jul 20, 2019 7:54 am
And since the Pi has been created in the millions (probably sold more than any PC, phone, or even Mac)
Since your actual question has been answered, this is a nitpick...

Total Pis sold to date: still under 30 million

PCs sold per year: roughly 100 million

Cell phones and tablets sold per year: roughly 1 billion
Is this of all the pis, or just the 3b?
Also, not a single PC, Mac or cellphone has sold as many units, as a pi. Cellphones in specific have thousands of different models from different brands, the most sold ones are the iPhone (5,6,7,8,9) and the Samsung Galaxy phones. Of each there are less sales than a raspberry pi 3

User avatar
rpdom
Posts: 15575
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Raspberry pi for coding on machine level?

Sun Jul 21, 2019 4:47 am

The "nearly 30 million Pi" figure quoted by W. H. Heydt is total sales for all Pi models.

To take just one example, over 60 million iphone6 were sold and used in the USA alone.

It would be fairer* to compare the Pi to other SBC sales where it has sold many times more units than all the others put together.

* Not fairer for the other SBCs ;)

willieaames
Posts: 18
Joined: Tue Aug 09, 2016 6:14 am

Re: Raspberry pi for coding on machine level?

Tue Jul 23, 2019 9:19 am

Thanks for the quick and informative response!

Is there a sort of community where machine learning applications like these can be found, and where people can share their experiences (more for beginners; and specific to the Pi)?
I believe there`s many, i`m following opensource and alex elli`s blog.

ProDigit
Posts: 374
Joined: Tue Aug 30, 2011 1:24 am

Re: Raspberry pi for coding on machine level?

Sat Jul 27, 2019 9:32 am

I think I more know what I want.
It's not 'machine code', but assembly I want to get to know better.
A very low level programming language.
Don't want to start binary or hex editing just yet! :lol:

User avatar
AdamStanislav
Posts: 147
Joined: Sun Mar 10, 2019 2:44 am
Location: Wisconsin
Contact: YouTube

Re: Raspberry pi for coding on machine level?

Sat Jul 27, 2019 8:06 pm

Well, there is a book Raspberry Pi Assembly Language RASPBIAN Beginners: Hands On Guide, by Bruce Smith. Maybe that could get you started.

timrowledge
Posts: 1289
Joined: Mon Oct 29, 2012 8:12 pm
Location: Vancouver Island
Contact: Website

Re: Raspberry pi for coding on machine level?

Sat Jul 27, 2019 9:05 pm

Unless you’re planning on writing a code generator there really isn’t much reason to worry about the very lowest level type of programming. I’ve done a fair bit whilst creating dynamic code translation systems for x86/68k/alpha/spark/arm/hp7000/etc and to be honest it makes for a fairly unpleasant time. Just say No.
Making Smalltalk on ARM since 1986; making your Scratch better since 2012

ProDigit
Posts: 374
Joined: Tue Aug 30, 2011 1:24 am

Re: Raspberry pi for coding on machine level?

Mon Jul 29, 2019 7:34 am

While assembler may take a long time to code, a lot of programmers prefer C.
I was wondering if C can generate (rather than the binary) a file that's readable in assembler.
I'm thinking, back in the days of web coding, we used MS Word, or other word editor, to make nice layouts of a web page, and then use a HTML or text editor, to simplify the code. Remove redundant code as well as classes (I hated working with classes), often times reducing file sizes by 10 to 90% depending on the project, at a cost of being 5-20% less appealing (80-95% identical in looks, but much faster load times).

If there was some way to edit higher level coding with lower level coding programs, like assembler, I'd like to know about this.

I read somewhere about D, but it appears to be a lot more difficult than c or c++, or even assembler, but combines high and low level coding.

markkuk
Posts: 137
Joined: Thu Mar 22, 2018 1:02 pm
Location: Finland

Re: Raspberry pi for coding on machine level?

Mon Jul 29, 2019 10:29 am

ProDigit wrote:
Mon Jul 29, 2019 7:34 am
I was wondering if C can generate (rather than the binary) a file that's readable in assembler.
Yes, use the -S option of the gcc compiler.

Return to “General discussion”