User avatar
__----__----__
Posts: 16
Joined: Tue May 15, 2012 9:33 am

Re: Assembly Code

Mon May 21, 2012 9:36 am

rurwin wrote: Wouldn't it be easier to teach them C with embedded assembler?
IMO, no.
C can be very off putting to a beginner and over complex, both in the
syntax and the compiler directives.
It's much easier (and nooby friendly) to ease them into programming using
BASIC like macros and when they have built up enough confidence then
expose them do the assembler behind the macros.
Once they feel comfortable with that, then move on to C as an alternative
and more complex language.
GCC does accept in-line assembler but it's definitely not for beginners.

User avatar
DexOS
Posts: 876
Joined: Wed May 16, 2012 6:32 pm
Contact: Website

Re: Assembly Code

Mon May 21, 2012 12:07 pm

rurwin wrote:
DexOS wrote: And yes you start them off with the basic like language and them start introducing them to asm code as they learn more.
Wouldn't it be easier to teach them C with embedded assembler?

Code: Select all

static bool GetSema(int ms, long* pSema)
{
	double	StartTime	= Time;

WaitForSema:
	__asm	push		eax
	__asm	push		edi
	__asm	mov		eax,1
	__asm	mov		edi,pSema		// lock			
	__asm	xchg		0[edi],eax
	__asm	or		eax,eax
	__asm	pop		edi
	__asm	pop		eax
	__asm	jz		UseIt			// got semaphore
	
	sleep(1);
	if ( (Time - StartTime)*1000 > (double)ms )
	{
		return false;
	}
	__asm	jmp		WaitForSema

UseIt:
	return true;	// success
}
(Yes that's x86. I don't have my Pi yet nor any code for it.)
I agree with __----__----__ », C is just has un-read able to a beginner has ASM.
By using macros, i could for example do a python like language, also its portable.
The same code would run on anything that the assembler has been coded for, in this case x86, arm, if the right includes were written.
But remember the code is very small compared to a C app, they can see the asm code and learn from it and add to it.
The only advantage of C is that theres lot of examples.
The problem with HLL, is that there only advantage is that they are more like human language (which i do not think C is ), so are easier to understand by a beginner.
But thats also there down fall, has when something higher comes along, coders move to the new higher language.
Example linux, it was a C world, C was king, but now a lot of linux stuff is done with python (yes i know it need C).
Windows was V this and V that.
But from win8 its more html5 and javascript.
Has we get higher and higher languages, it will be called programing when you move a window across the screen.
I think basic and asm are a good mix for beginners, most of today top programmer learn't on this combo .
Batteries not included, Some assembly required.

User avatar
Dave_G_2
Posts: 196
Joined: Sat Apr 14, 2012 7:04 pm

Re: Assembly Code

Mon May 21, 2012 2:40 pm

I am also in agreement, Basic helps ease beginners into programming and assembler gives
anyone a very good solid background to programming.
As DexOS wrote, as computer languages go higher and higher in level, not only do the compilers
do too many things behind your back (unwanted optimization and reorganizing things) but the
programmer tends to be shielded more and more from the fundamentals of pure programming and
with that there is an ever diminishing pool of people that have any concept of CPU, RAM and other
capabilities/limits which invariably ends up in worse and worse quality of code and pure bloatware.

As a case in point, just look at the Windows drivers for just about any printer or 3G stick, it's hundreds
and hundreds of megabytes in size, why? for a driver?

I'm certainly not advocating writing a whole web browser or office type app in pure assembler but
if only more programmers understood more of the basics of the machine and OS they are coding
in/for, then it can only benefit all of us as both coders and users.
To use an analogy, how can you possibly expect a solid building if the foundations are not up to scratch.

User avatar
cheery
Posts: 219
Joined: Wed Jan 25, 2012 9:39 pm

Re: Assembly Code

Mon May 21, 2012 4:07 pm

You shall not mock layers of abstractions. As they do not make programs slow and big. Premature optimization, stupidity, lack of optimizations - in that order - make the programs slow and big.

Why learn assembly? It's fun exercise. You need it only in extreme cases and it's sort of a requirement for full-fledged programmers.. But I don't think many ppl. know or use assembly for any real purpose these days.

Assembly level optimizations are one of the last things you do when you want more performance from the program. And they are highly platform dependent although that's not a problem with many programs.

User avatar
Dave_G_2
Posts: 196
Joined: Sat Apr 14, 2012 7:04 pm

Re: Assembly Code

Mon May 21, 2012 4:25 pm

cheery wrote:You shall not mock layers of abstractions. As they do not make programs slow and big. Premature optimization, stupidity, lack of optimizations - in that order - make the programs slow and big.
You are kidding, right?
What's the size of the .net framework and java and even VB5/6 for that matter?
They are huge and use J.I.T. which is not exactly fast compared to a stand alone binary.
They are fine with the latest 3GHz CPU and oodles of RAM, but when it comes to a less powerful
system just like the Pi, that is when we notice the difference.
VB3/4/5/6 also does not compile to pure bytecode but rather to Pcode which is then run
and translated to proper bytecode at run time, that is why one needs VBRUN600.dll
Another problem with many high level compilers, like I wrote before, is that the optimizations
are "automatic" and not configurable which like you say adds to the problem.

Linux has a very handy command called strace which when combined with the -t switch will
trace a pgm and give you a time stamp after every line/instruction.
Try it with some apps written in a high level language with layers and layers of abstraction
then see the time taken.
cheery wrote: Why learn assembly? It's fun exercise. You need it only in extreme cases and it's sort of a requirement for full-fledged programmers.. But I don't think many ppl. know or use assembly for any real purpose these days.
Used a driver lately?

Sure, assembler is not an absolute must for all facets of programming but it sure does help in
better understanding the fundamentals of programming.

User avatar
cheery
Posts: 219
Joined: Wed Jan 25, 2012 9:39 pm

Re: Assembly Code

Mon May 21, 2012 5:43 pm

Dave_G_2 wrote:You are kidding, right?
What's the size of the .net framework and java and even VB5/6 for that matter? VB3/4/5/6 also does not compile to pure bytecode but rather to Pcode which is then run and translated to proper bytecode at run time, that is why one needs VBRUN600.dll Another problem with many high level compilers, like I wrote before, is that the optimizations are "automatic" and not configurable which like you say adds to the problem.
Now the kind of bulky frameworks you mentioned make it harder to write good quality code. Javascript framework exchanges some additional binary-level portability to performance. NET framework exchanges performance for "me too want a virtual machine and java popularity!".

Virtual machines can be slim and beautiful. Good abstraction layers are there just to make programming faster, exchanging some computing time to more expensive programmer time. Couple megabytes tend to be usually a small price to pay from faster development speed. And even if on top of abstractions, you can still optimize.
Sure, assembler is not an absolute must for all facets of programming but it sure does help in
better understanding the fundamentals of programming.
Maybe I should've been clear that I also agree on that. For most people though, assembly is one of those things you learn and have fun with it and then go on your own ways.

gadgetoid
Posts: 152
Joined: Wed Mar 07, 2012 9:58 pm

Re: Assembly Code

Mon May 21, 2012 5:48 pm

Moving swiftly away from the gentlemans flame-war over the virtues of assembly... does anyone know how one would go about manipulating the GPIO from assembly?

It would be an interesting exercise to write an LED control program entirely in assembly, if it's possible!

tufty
Posts: 1456
Joined: Sun Sep 11, 2011 2:32 pm

Re: Assembly Code

Mon May 21, 2012 5:57 pm

gadgetoid wrote:Moving swiftly away from the gentlemans flame-war over the virtues of assembly... does anyone know how one would go about manipulating the GPIO from assembly?

It would be an interesting exercise to write an LED control program entirely in assembly, if it's possible!
Should be a case of toggling bits.

User avatar
johnbeetem
Posts: 945
Joined: Mon Oct 17, 2011 11:18 pm
Location: The Mountains
Contact: Website

Re: Assembly Code

Mon May 21, 2012 5:58 pm

gadgetoid wrote:Moving swiftly away from the gentlemans flame-war over the virtues of assembly... does anyone know how one would go about manipulating the GPIO from assembly?

It would be an interesting exercise to write an LED control program entirely in assembly, if it's possible!
Start with the C version from the RasPi Wiki: http://elinux.org/RPi_Low-level_periphe ... le_.28C.29

The tricky part is mmap, which you need to map virtual addresses into the physical addresses of the GPIO registers. Once you've done that, you can talk to the GPIO registers with loads and stores.

You can always cheat by saving the C compiler's ASM output file and then editing it.

User avatar
Dave_G_2
Posts: 196
Joined: Sat Apr 14, 2012 7:04 pm

Re: Assembly Code

Mon May 21, 2012 6:03 pm

cheery wrote: Now the kind of bulky frameworks you mentioned make it harder to write good quality code. Javascript framework exchanges some additional binary-level portability to performance. NET framework exchanges performance for "me too want a virtual machine and java popularity!".
Agreed, unfortunately too many people don't understand that.
cheery wrote: Maybe I should've been clear that I also agree on that. For most people though, assembly is one of those things you learn and have fun with it and then go on your own ways.
What you describe is unfortunately the way it is in most cases, which is a great pity.

User avatar
Dave_G_2
Posts: 196
Joined: Sat Apr 14, 2012 7:04 pm

Re: Assembly Code

Mon May 21, 2012 6:10 pm

gadgetoid wrote:Moving swiftly away from the gentlemans flame-war over the virtues of assembly...

Fair enough
gadgetoid wrote: does anyone know how one would go about manipulating the GPIO from assembly?
It would be an interesting exercise to write an LED control program entirely in assembly, if it's possible!
Very possible, look on pages 5,6,7 & 90 ,91 of BCM2835-ARM-Peripherals.pdf where it shows you the
address of all the GPIO registers.

Possible example (FasmARM syntax):
(Don't execute, as it's only an example, first check the addresses and data to put in the regs
at these addresses to make the GPIO do what you want them to do)

Code: Select all

format ELF executable
        entry start

        segment readable executable

start:  mov     r0,0x01000  ; the data we want to put in a GPIO reg
         str        r0,[0x7E200000]  ; the address of the reg
          ; repeat the mov and str for other regs as required.
           mov     r0,6
           swi     0x900001  ; exit
        
         segment writeable

Last edited by Dave_G_2 on Mon May 21, 2012 6:39 pm, edited 3 times in total.

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

Re: Assembly Code

Mon May 21, 2012 6:25 pm

Dave_G_2 wrote:To use an analogy, how can you possibly expect a solid building if the foundations are not up to scratch.
Do people have to know how to build the instrument to be good musicians, or know how to manufacture paint or understand its chemical composition to be great artists ?

Having coded in assembler commercially for 15 years, I don't agree with this notion that assembler is essential or makes anyone a better HLL programmer. It's nice to have a wide knowledge of the field but not essential. How many people have written perfectly acceptable Java code without understanding the Java Bytecode definition ? Would knowing it really help them write better programs ?

User avatar
Dave_G_2
Posts: 196
Joined: Sat Apr 14, 2012 7:04 pm

Re: Assembly Code

Mon May 21, 2012 6:46 pm

hippy wrote:
Dave_G_2 wrote:To use an analogy, how can you possibly expect a solid building if the foundations are not up to scratch.
Do people have to know how to build the instrument to be good musicians, or know how to manufacture paint or understand its chemical composition to be great artists ?

Having coded in assembler commercially for 15 years, I don't agree with this notion that assembler is essential or makes anyone a better HLL programmer. It's nice to have a wide knowledge of the field but not essential. How many people have written perfectly acceptable Java code without understanding the Java Bytecode definition ? Would knowing it really help them write better programs ?
Yes agreed but the point I'm trying to make is that it does help in making better decisions as to what
language is used for a particular task.

User avatar
johnbeetem
Posts: 945
Joined: Mon Oct 17, 2011 11:18 pm
Location: The Mountains
Contact: Website

Re: Assembly Code

Mon May 21, 2012 6:49 pm

hippy wrote:Do people have to know how to manufacture paint or understand its chemical composition to be great artists?
Actually, yes. Color pigments are chemicals, and chemicals may react with each other. From what I've read, if you put certain colors together, they react over time and the colors degrade over the decades. (This may have improved since I first read about it.)

User avatar
cheery
Posts: 219
Joined: Wed Jan 25, 2012 9:39 pm

Re: Assembly Code

Mon May 21, 2012 6:53 pm

hippy wrote:Do people have to know how to build the instrument to be good musicians?
Sure they have to KNOW their instrument. Bad analogy have we? I do think assembly is useful to know but not really essential, a bit like you say.

I don't say it's particularly useful to know specifics. But it sure is useful if you have an idea how your program will turn into assembly instructions, or how the virtual machine runs your code. At least when there will be bugs.

User avatar
Dave_G_2
Posts: 196
Joined: Sat Apr 14, 2012 7:04 pm

Re: Assembly Code

Mon May 21, 2012 7:05 pm

Image
johnbeetem wrote: Actually, yes. Color pigments are chemicals, and chemicals may react with each other. From what I've read, if you put certain colors together, they react over time and the colors degrade over the decades. (This may have improved since I first read about it.)
cheery wrote: Sure they have to KNOW their instrument. Bad analogy have we? I do think assembly is useful to know but not really essential, a bit like you say.

I don't say it's particularly useful to know specifics. But it sure is useful if you have an idea how your program will turn into assembly instructions, or how the virtual machine runs your code. At least when there will be bugs.

tufty
Posts: 1456
Joined: Sun Sep 11, 2011 2:32 pm

Re: Assembly Code

Mon May 21, 2012 8:26 pm

hippy wrote:Having coded in assembler commercially for 15 years, I don't agree with this notion that assembler is essential or makes anyone a better HLL programmer.
It doesn't.

Assembler is all about the mechanics of a particular processor (or the mechanics of processors in general). It (assembly in general,as opposed to any particular assembly language or, indeed, syntax subset) is an awful language for teaching basic concepts. Teaching programming is all about teaching computational thinking, not about micromanaging register usage (something which, oddly enough, compilers are usually better at than you are).

Assembly is extremely useful when you need it. But the times when you actually, really, need it are rare indeed. And for Dave_G2, who is probably jumping about shouting "I know! Drivers!" at the moment, drivers don't need to be written in assembler - for example, the drivers in NeXTStep were written in Objective-C (and operated in userland ), and those for the MIT Lisp machines were written in - well - Lisp. Both of these managed to be blinding fast (in NextStep's case, running on something slightly more powerful than an Atari ST)

There are those who, being around my age, learned to program using BASIC, then moved on to assembler (or, in my case, came straight in with assembler because I didn't have the BASIC ROM for my first machine). They look back, misty eyed, on the hours they spent poring over listings, and say to themselves "That's the way kids should learn! After all, I learned that way, and look at the 37337 programmer-dude I have become!". Unfortunately, they are missing the fact that they probably would have managed to learn to program even if it meant flipping binary patterns in at the front panel; they learned to program despite being restricted to BASIC and assembler, not because of them.

Abstraction is good. As cheery said:
You shall not mock layers of abstractions. As they do not make programs slow and big. Premature optimization, stupidity, lack of optimizations - in that order - make the programs slow and big.
Simon

User avatar
Dave_G_2
Posts: 196
Joined: Sat Apr 14, 2012 7:04 pm

Re: Assembly Code

Mon May 21, 2012 9:03 pm

@tufty

Actually "manipulating registers" as you put it is very much in line with computational thinking.
So much so that you almost have to think like the CPU.

The fact that you may think that assembler is awful for teaching is fine, it don't necessarily mean it's correct though.
Different strokes and all that.

Yes drivers can be coded with other languages, but in general they are done in assembler and just think
if you hadn't learnt assembler you wouldn't be partaking in this discussion. :-)

User avatar
johnbeetem
Posts: 945
Joined: Mon Oct 17, 2011 11:18 pm
Location: The Mountains
Contact: Website

Re: Assembly Code

Mon May 21, 2012 9:35 pm

Dave_G_2 wrote:Different strokes and all that.
I wonder how much the varying opinions on this subject are related to the specific machine language(s) learned. When I say I love programming in ASM (which I don't actually do that much these days) it's because I wrote most of my ASM code for the PDP-11. The PDP-11 is a very elegant, highly orthogonal architecture. At the time I learned it, it was a whole lot easier to write data structure and string processing code in PDP-11 ASM than in available high-level languages such as Fortran and Algol. Pascal and C showed up after I had mastered PDP-11 ASM. It was an easy and intuitive step to use C as a portable assembly language, and Pascal is a great language for data structure programming.

I've done a lot of 8080 ASM. While not as much fun or productive as PDP-11, it was very enjoyable because it was the first computer I owned (Heathkit H-8). I've done a lot of 680X0 ASM, which I like because it's so much like the PDP-11. I'm looking forward to playing with TI MSP430 one of these days. It's also similar to PDP-11 except for costing $1 instead of $10K.

I've done very little 80x86 ASM. I have a tendency to wince when I look at 80x86 code. If that had been my only intro to ASM, I'd probably hide it under several layers of abstraction too :-)

User avatar
DexOS
Posts: 876
Joined: Wed May 16, 2012 6:32 pm
Contact: Website

Re: Assembly Code

Mon May 21, 2012 9:40 pm

Theses no need to have a war over languages, but every compiler has a Assembler as its back end.
Gcc uses gas
Read this: http://en.wikibooks.org/wiki/GNU_C_Comp ... chitecture
See how they work, there can be no other languages without assemblers.
Languages come and go, but there will alway be assembly.
The problem is how some coders look down on the language below the one they use.
C coders for example, hate ASM coders, because some C coders spend half there time belittling V-basic or python coders.
But they can not do that to ASM coders, so they say how useless it is or its a wast of time etc.
The thing is, ASM coders see ASM has easy, its not rocket science.

Fact all todays top coders learn't on a combo of basic and ASM, fact kids are not learning has much has kids from the past.
How do we fix this ?
Batteries not included, Some assembly required.

andyl
Posts: 265
Joined: Tue Jan 10, 2012 11:05 am

Re: Assembly Code

Tue May 22, 2012 12:12 am

DexOS wrote:Theses no need to have a war over languages, but every compiler has a Assembler as its back end.
Not true in general. Compiling via an intermediate assembly level is one valid approach.
See how they work, there can be no other languages without assemblers.
Again untrue. I've written (limited) Forth systems that work without an assembler.
Languages come and go, but there will alway be assembly.
But what architecture? The difference between some architectures are massive.
The problem is how some coders look down on the language below the one they use.
C coders for example, hate ASM coders, because some C coders spend half there time belittling V-basic or python coders.
But they can not do that to ASM coders, so they say how useless it is or its a wast of time etc.
What tosh. That is divisive thinking. You obviously don't get out to talk to many real developers do you? Most of us work in more than one language. In fact the best know and use multiple languages often in the same project.

BTW - I've written some micro-code. Does that mean I can look down on ASM coders :lol:
The thing is, ASM coders see ASM has easy, its not rocket science.
Yes but it is inappropriate for a lot of things. To write a large program will take far longer using a low level language than using something at a more appropriate level of abstraction, and will typically take longer to debug, and be harder to maintain.
Fact all todays top coders learn't on a combo of basic and ASM, fact kids are not learning has much has kids from the past
Again you seem to have an obsession about "top coders" whatever or whoever they are.

User avatar
__----__----__
Posts: 16
Joined: Tue May 15, 2012 9:33 am

Re: Assembly Code

Tue May 22, 2012 7:44 am

I suppose we could discuss/argue over the benefits of assembler ad nauseum
but let's leave it as some people think it's great, some will use it now and again
whilst others think it's a waste of time.
Vive la différence.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Assembly Code

Tue May 22, 2012 8:40 am

I learned BASIC first, and then we were taught a pretend assembler -- base 10, floating point only. I can't say that taught me a lot. Not any more than the theory lessons on the von-Neumann architecture anyway.

Then I got my first computer and learned to program in machine code because there was not even an assembler. That did teach me stuff, and it stood me in good stead when education caught up in the second year under-grad when we were learning how instruction decode works, and probably later when I was debugging core dumps. Some years later I built a 320000 computer from scratch and programmed it's OS in machine code (it was rather simple -- just proof of concept.)

But the machine I started on was very simple -- about 45 different instruction codes, and I was not a truly competent assembler programmer until I had been in industry for a year -- maybe six years of regular practise -- and I never considered myself good at it. In the same time I acheived fluency in three different high-level languages: BASIC, Pascal and C.

So I don't think that a dumbed-down introduction to assembler would achieve much, and any worthwhile treatment would be counterproductive simply because it took so long. You can teach many concepts with assembler, but only a fraction of the syllabus with any efficiency. For example you can code a binary tree in assembler without too much trouble, but the code is impenetrable. If you code it in C or Pascal or Python it comes to life.

antiloquax
Posts: 406
Joined: Sun Nov 20, 2011 11:37 am
Contact: Website

Re: Assembly Code

Tue May 22, 2012 9:13 am

My feeling about Assembly Code is that it's incredibly helpful to get an insight into exactly what is happening at the lower level when we use a loop (or whatever) in a higher level language.
I doubt I'd use it for writing anything complex, but I'd like to get my head around some of the basics. It's interesting to read that the ARM chip apparently can't do division!
mark

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Assembly Code

Tue May 22, 2012 10:49 am

Rule of thumb for Assembly programming: If you need to use division you're doing it wrong. ;-)

Return to “Other programming languages”