LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-05-2018, 05:21 AM   #1
turay
LQ Newbie
 
Registered: Sep 2017
Posts: 18

Rep: Reputation: Disabled
How to learn assembly language the right way?


How to learn assembly language the right way?
 
Old 03-05-2018, 05:24 AM   #2
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 2,529

Rep: Reputation: 1031Reputation: 1031Reputation: 1031Reputation: 1031Reputation: 1031Reputation: 1031Reputation: 1031Reputation: 1031
Is there a WRONG way to learn assembly language?


Assembly language for what processor?
Have you an assembler for your processor?
Have you the machine language reference and register map for your processor?
 
2 members found this post helpful.
Old 03-05-2018, 07:17 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,060
Blog Entries: 4

Rep: Reputation: 3163Reputation: 3163Reputation: 3163Reputation: 3163Reputation: 3163Reputation: 3163Reputation: 3163Reputation: 3163Reputation: 3163Reputation: 3163Reputation: 3163
Assembly language is rarely – if ever – used anymore, because modern microprocessors are engineered to be programmed by compilers, not people. CPU architects then work with compiler designers to cause the best object-code for that particular chip to be generated (and optimized).

So, not only is the procedure much more efficient for humans, but also it's much more efficient for the CPU.

A typical example of assembly code today is the /arch subdirectory of the Linux source-code tree. Even there, you'll notice that nearly all of the components are C modules with asm{...} blocks. In most cases, the architects minimized the use of conditional compilation throughout the source-tree and instead devised a set of functions which could then be implemented in appropriate ways (or be no-ops, as the case may be). They usually devised macros to invoke those functions so that the calls could be omitted entirely if this was appropriate, all without affecting the source code.

In fact, the ground-breaking feature of the original Unix® OS was that most of it was programmed in a high-level language that was basically invented for the purpose: "C." Prior to that time, most work was done in hand-written assembler, on the assumption – erroneous, as it turned out – that this was necessary.

(Although some of the very earliest compilers emitted assembly code that was subsequently assembled. Much like the earliest "C++" compiler emitted "C" source code. Back in the days when you had virtually nothing to work with, you did what you had to do ...)

Last edited by sundialsvcs; 03-05-2018 at 07:23 AM.
 
1 members found this post helpful.
Old 03-05-2018, 07:50 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 6,455
Blog Entries: 12

Rep: Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219
Some general rules of thumb when learning any new form of programming, as well as for this particular topic:
  • When you really look at it, nearly all instructions are in the forms of: move, test, or branch. So you are going to do any of:
    • Store or retrieve data and manipulate it
    • Compare data against a condition, either simple or complex
    • Make a next program direction decision based on the outcome
  • There are also many commands which are "in place", such as bit manipulations, including shifts, and rotates
  • It is helpful to learn the general things done to accomplish common thing, such as mathematical computations, how to do a loop, how to read input, how to produce output, how to manipulate an array or list, the various modes for referencing a location in memory, and understanding the CPU registers and what is available
  • Taking the registers as a leap into my next part, you should then learn what is, or is not special about the CPU you have. Well ... they're all special, however ones like DSPs or pipeline ones have some additional special properties and resources. Overall, you need to be versed in the specific CPU you are working with to understand what resources are available to you for your program actions, and also be aware of the instruction set, and finally the memory model(s) that the CPU is capable of working with.
As far as code, there's nothing saying you can't have functions, they just look far different. You also can separate you code logically into files, and sections and have far more control over what you do when you perform this. You also can get into deeper trouble, as well as write very confusing code. Documenting the code, using name references within the code is helpful, but also to note that if the main data register is named sr0 per the instruction set, then that is the name you have to use and you may find you need to add a lot of comments to aid you as your architecture grows.

A great recommendation is that you find a way to cause an output of the mixed source, assembler listing of C files. Do not use any library functions, turn of optimization, and do very simple actions. Then look at the assembly generated by your C code.
 
2 members found this post helpful.
Old 03-05-2018, 08:17 AM   #5
turay
LQ Newbie
 
Registered: Sep 2017
Posts: 18

Original Poster
Rep: Reputation: Disabled
Thank you all, helpers,
just i am new and want to learn assembly:
1- ٍShould i learn Computer architecture before learn assembly? if yes which book is to start with?
2- Which book i should start with to learn assembly?
3- I want some advices in assembly field.
 
Old 03-05-2018, 08:37 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 6,455
Blog Entries: 12

Rep: Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219
Quote:
Originally Posted by turay View Post
Thank you all, helpers,
just i am new and want to learn assembly:
1- ٍShould i learn Computer architecture before learn assembly? if yes which book is to start with?
2- Which book i should start with to learn assembly?
3- I want some advices in assembly field.
Have you searched for any resources on the web?

There are plenty of kits for simple CPUs where you can do assembly language programming.

I don't recommend you look at a book for assembly, with the exception of the programmer's manual and instruction set for the target CPUs you choose to try assembly programming for.

As far as computer architecture, it absolutely cannot hurt to be versed in how a CPU is formed out of logic. Recommend you also web search for basic computer architecture, there is plenty of information available. Find the wikipedia for computer architecture, review that and the topics recommended to also read, such as about Von Newmann and Harvard; probably most of that See Also section.

There is little people can offer here about this topic, with the exception of the opinions most of us have offered. This topic area is very large, and you really should research this, so as to determine what areas of concentration work best for you.
 
Old 03-05-2018, 09:09 AM   #7
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,068

Rep: Reputation: 673Reputation: 673Reputation: 673Reputation: 673Reputation: 673Reputation: 673
I would also look at micro controller programming in assembly (smaller instruction set, light program complexity) although C language is commonly used in this field too. Assembly code produces smaller program size to upload and a great control feeling, so I think its use is justified here
There are not expansive kits solutions to start with.
 
Old 03-05-2018, 10:31 AM   #8
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 8,031

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
"Programming from the Ground Up", which is free, is quite good.
 
1 members found this post helpful.
Old 03-05-2018, 11:57 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,434

Rep: Reputation: 1054Reputation: 1054Reputation: 1054Reputation: 1054Reputation: 1054Reputation: 1054Reputation: 1054Reputation: 1054
@OP: As a starts, you should read (and learn by heart) each and every books of Andrew S. Tanenbaum.
Also read this: http://www.catb.org/esr/faqs/hacker-howto.html

Last edited by NevemTeve; 03-05-2018 at 11:58 AM.
 
1 members found this post helpful.
Old 03-08-2018, 01:25 AM   #10
deleted23
Member
 
Registered: Nov 2015
Distribution: Arch, Ubuntu Studio
Posts: 43

Rep: Reputation: 1
Quote:
Assembly language for what processor?
Have you an assembler for your processor?
Have you the machine language reference and register map for your processor?

I just love it...
Why don't you just save yourself the time to write such garbage and me (us) the time to read it?

However...
To 'my time' I asked for a reference manual for the Intel 286 and 386DX40 Processor at Intel itself in England.
These days they send them free to my home in Germany but I doubt they still do it today.

Besides... I'd prefer now AMD processors.
Greetings by meltdown and spectre...

Assembler is totally useless to you if you don't know the internal processor structure you want to program for.
Ok, admitted... perhaps not totally useless but you won't understand Assembler if you don't understand microprocessors/controllers in detail.
Very important keywords are register, shift-left, shift-right, global-descriptor-table register, paging to name a few.


Hope the helps a bit.

Greets

Gee
 
Old 03-08-2018, 06:21 AM   #11
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 2,529

Rep: Reputation: 1031Reputation: 1031Reputation: 1031Reputation: 1031Reputation: 1031Reputation: 1031Reputation: 1031Reputation: 1031
Quote:
Originally Posted by bluntroller View Post
I just love it...
Why don't you just save yourself the time to write such garbage and me (us) the time to read it?
I do not understand why you consider my questions "garbage". They seem quite reasonable to me.

I started assembly programming Z-80 processors, then moved on to 6800 series processors before working on Intell 686. Discovering single board and embedded device with unique controllers to code for was wondeful. For each processor I started without an assembler, but obtained one as soon as I could. (You can code directly to machine, but an assemble makes life a LOT easier!) For each processor I had a limited reference, and obtained a complete reference or guide as soon as I could. I would like to know what resources the OP has already gathered, and what start he has made (if any), before adding advice of my own.

I notice that references (of a wide range of accuracy and value) are available on the internet these days. A quick search will turn up several examples.
 
1 members found this post helpful.
Old 03-08-2018, 06:34 AM   #12
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,060
Blog Entries: 4

Rep: Reputation: 3163Reputation: 3163Reputation: 3163Reputation: 3163Reputation: 3163Reputation: 3163Reputation: 3163Reputation: 3163Reputation: 3163Reputation: 3163Reputation: 3163
And even the venerable CP/M® operating system was written in a high-level language, as was Unix®.

"Learning about the microprocessor" is actually quite a time-consuming task if you want to pore through the more than 7 thick manuals which describe the x86 family of chips. "Does this model or that model support this esoteric instruction?" Well, the compiler knows already – just name your target and pick your level of optimization.

As I've said, the extremely-disciplined way in which the Linux team (necessarily) uses assembler for "architecture-specific" pieces is very typical. You'll find pure-assembler in the so-called "trampoline" code (which "bounces" the chip into its final operating mode, never to return), but that's about it.

Last edited by sundialsvcs; 03-08-2018 at 06:37 AM.
 
Old 03-08-2018, 01:00 PM   #13
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,664
Blog Entries: 6

Rep: Reputation: 2524Reputation: 2524Reputation: 2524Reputation: 2524Reputation: 2524Reputation: 2524Reputation: 2524Reputation: 2524Reputation: 2524Reputation: 2524Reputation: 2524
Quote:
Originally Posted by turay View Post
Thank you all, helpers,
just i am new and want to learn assembly:
1- ٍShould i learn Computer architecture before learn assembly? if yes which book is to start with?
2- Which book i should start with to learn assembly?
3- I want some advices in assembly field.
Well, you can't do much without knowing the architecture of the CPU and its glue, so you need to pick a target CPU and/or single board computer, then gear your learning toward that device and board.

Quote:
Originally Posted by wpeckham View Post
I do not understand why you consider my questions "garbage". They seem quite reasonable to me.

I started assembly programming Z-80 processors, then moved on to 6800 series processors before working on Intell 686. Discovering single board and embedded device with unique controllers to code for was wondeful.
Wonderful indeed!

@bluntroller: Please refrain from insults to other members.
 
Old 03-08-2018, 01:33 PM   #14
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 8,031

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
Quote:
Originally Posted by turay View Post
1- ٍShould i learn Computer architecture before learn assembly? if yes which book is to start with?
"Code". By Charles Petzold.
 
Old 03-08-2018, 04:44 PM   #15
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,917

Rep: Reputation: 863Reputation: 863Reputation: 863Reputation: 863Reputation: 863Reputation: 863Reputation: 863
Quote:
Originally Posted by astrogeek View Post
@bluntroller: Please refrain from insults to other members.
It is clear that this statement by bluntroller was totally joking. If you continue to read he also states he loves assembly and considers his own activities a waste of time.

jlinkels
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] A problem. When using assembly language to call the C language function mirage1993 Programming 3 10-03-2014 08:15 AM
Learn Assembly before CUDA? BrajZore Programming 4 03-04-2013 07:18 PM
Is Assembly Language considered a Structured Language? theKbStockpiler Programming 4 01-30-2011 09:09 AM
How to learn assembly language? darkangel29 Programming 8 01-12-2009 12:30 PM
What are some good resources to learn assembly Language? theunixwizard General 16 07-24-2008 03:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration