Page 2 of 2

Re: Raspberry pi for coding on machine level?

Posted: Mon Jul 29, 2019 10:46 am
by jahboater
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!" )"

Re: Raspberry pi for coding on machine level?

Posted: Tue Jul 30, 2019 12:01 am
by ProDigit
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!

Re: Raspberry pi for coding on machine level?

Posted: Tue Jul 30, 2019 11:10 am
by jamesh
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.

Re: Raspberry pi for coding on machine level?

Posted: Tue Jul 30, 2019 11:37 am
by RaTTuS
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 .

Re: Raspberry pi for coding on machine level?

Posted: Tue Jul 30, 2019 2:25 pm
by MarkTF
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.

Re: Raspberry pi for coding on machine level?

Posted: Tue Jul 30, 2019 2:48 pm
by jahboater
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).