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

Re: Raspberry pi for coding on machine level?

Mon Jul 29, 2019 10:46 am

markkuk wrote:
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.
Yes.
If you use Raspbian Buster, it comes with a more recent version of GCC (8.3) that can include the original C source code within the assembler file. That makes it extremely easy to find the code emitted for a particular C source line.

Code: Select all

gcc -O3 -S -fverbose-asm hello.c -o hello.s
Here is an example you can try on the Pi:

Code: Select all

#include <stdio.h>

int
main( void )
{
  printf( "hello world!\n" );
}
produces this assembler:

Code: Select all

	.type	main, %function
main:
	@ args = 0, pretend = 0, frame = 0
	@ frame_needed = 0, uses_anonymous_args = 0
	push	{r4, lr}	
@ hello.c:7:   printf( "hello world!\n" );
	ldr	r0, .L4	
	bl	puts	
@ hello.c:8: }
	mov	r0, #0	
	pop	{r4, pc}	
.L5:
	.align	2
.L4:
	.word	.LC0
	.size	main, .-main
	.section	.rodata.str1.4,"aMS",%progbits,1
	.align	2
.LC0:
	.ascii	"hello world!\000"
You can easily see how its converted "printf( "hello world!\n" )" into "puts( "hello world!" )"

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

Re: Raspberry pi for coding on machine level?

Tue Jul 30, 2019 12:01 am

Thanks! This information is very interesting.
Yes, assembler is definitely not something you'd want to start coding on, but it's interesting to see how it allows for more flexibility and fine tuning of a program created in a higher level programming language!

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23891
Joined: Sat Jul 30, 2011 7:41 pm

Re: Raspberry pi for coding on machine level?

Tue Jul 30, 2019 11:10 am

ProDigit wrote:
Tue Jul 30, 2019 12:01 am
Thanks! This information is very interesting.
Yes, assembler is definitely not something you'd want to start coding on, but it's interesting to see how it allows for more flexibility and fine tuning of a program created in a higher level programming language!
It's always worth remembering that modern compilers are so extraordinarily good that it would take a really exceptional assembler programmer to be able to improve on the code they can generate. If it's even possible.

One place where programmers do have a slight advantage is NEON parallel coding, the compilers (I may be out of date), are not quite so good as sorting that stuff out optimally.

You will get more performance improvements by using better algorithms in most cases of optimisation.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“I think it’s wrong that only one company makes the game Monopoly.” – Steven Wright

User avatar
RaTTuS
Posts: 10491
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: Raspberry pi for coding on machine level?

Tue Jul 30, 2019 11:37 am

also compiling is hard ,
very hard -
compiling for speed or space can be conflicting and difficult - unrolling loops can help in some cases but not in others....
but in general a compiler is better than you .
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
jcyr
Posts: 443
Joined: Sun Apr 23, 2017 1:31 pm
Location: Atlanta

Re: Raspberry pi for coding on machine level?

Tue Jul 30, 2019 1:51 pm

jamesh wrote:
Tue Jul 30, 2019 11:10 am
It's always worth remembering that modern compilers are so extraordinarily good that it would take a really exceptional assembler programmer to be able to improve on the code they can generate. If it's even possible.
Agreed. If nothing else, learning assembler for at least one CPU architecture will give you a better appreciation of just how powerful optimizing compilers have become.
It's um...uh...well it's kinda like...and it's got a bit of...

MarkTF
Posts: 300
Joined: Tue Mar 03, 2015 4:59 pm

Re: Raspberry pi for coding on machine level?

Tue Jul 30, 2019 2:25 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?
If the objective is simply exposure to assembly level programming, my suggestion would be to start on something with a dirt simple machine architecture, like a PIC microcontroller. The level of support available will probably be better than on more complex architectures because it's a practical approach to using low end devices.

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

Re: Raspberry pi for coding on machine level?

Tue Jul 30, 2019 2:48 pm

MarkTF wrote:
Tue Jul 30, 2019 2:25 pm
If the objective is simply exposure to assembly level programming, my suggestion would be to start on something with a dirt simple machine architecture, like a PIC microcontroller. The level of support available will probably be better than on more complex architectures because it's a practical approach to using low end devices.
You have, included with Raspbian, an assembler, a debugger, and a choice of editors.
- and an excellent C compiler that you can use to find out how to do things when you get stuck :)

ARM assembler is popular and there are countless people who can help, many on this forum.

Finally, I find a 32-bit or 64-bit CPU with a decent instruction set much easier to program than a basic 8-bit CPU with a very limited choice of instructions. (That's just me though).

Return to “General discussion”