Archived

This topic is now archived and is closed to further replies.

Assembly language

This topic is 5149 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have started to learn programming in c# and am getting quite far with it. The only other programming language experience i have before this is c. I am interested in learning how my code actually works and many people have suggested learning assembly language. I would like to start learning it but i dont know which assembler to choose. I was just reading a page with a book called " art of assembly language" and it teaches with an assembler called hla (High level assembler). The author wrote a page called which is the best assembler. After reading that I am persuaded to go with hla although the author is obviously biased towards it because he wrote hla. Are there any asm programmers here that could give me some advice? Any help would be greatly appreciated, Thanks

Share this post


Link to post
Share on other sites
If we''re talking Windows assembler here, I''d say have a look at MASM32. It has plenty of macros to simplify some of the more mundane tasks in assembly (though you''re free not to use them). It comes with utilities that will even help you implement class structures in assembly (though I never used it other than a quick check out). Other than that it''s free and well documented.

Share this post


Link to post
Share on other sites
An assembler might not be the thing you want. Consider, if you''re interested in learning about how your code actually works, why not direct your compiler to dump a listing of assembly code generated from your C source? (and maybe your C# source too). Of course, you''ll still need to understand assembly language, to make sense of the listing, but you don''t need to compile assembler to read it.

AoA is a great assembler reference too, hla or no. Also download the 3 volumes of the "IA-32 Intel Architecture Software Developer’s
Manual" -

ftp://download.intel.com/design/pentiumii/manuals/

24319002.pdf - vol 1
24319102.pdf - vol 2
24319202.pdf - vol 3

Share this post


Link to post
Share on other sites
Thanks guys, I decided to go with hla, it is an assembly language that gets conveted to masm and masm does the rest. Masm seems like the best choice to learn if you want to be a pro asm guy but since i just want to learn how code works in higher level languages i think hla will be fine.

Anyway, there is something i dont get. If assembly language is code the machine can read directly, wouldn''t there be a different language for every different processor out there? Will asm code i write for windows work with all other versions of windows? I dont get it.

Also, can c# work with assembly? Say i had a program in c# that asked how to add 2+2 , could code i wrote in assembly and compiled to a dll calculate that and be called from c#?
Sorry if that sounds stupid, i am totally new to asm.

Thanks again

Share this post


Link to post
Share on other sites
assembly language is one step above something what is often called "machine" language, which is what the assembly gets turnd into, basically each assembly instruction gets turned into a binary opcode instructions which is native to the machine.(*)
so, yes, each machine has a different "language" but, similar types of machines all understand the same bits of language. so all 386,486,pentium,etc understand the 286 "language"

because windows is most commonly run on intel x86 machines, then as a general rule, your asm code will work with most other versions of windows.

I dont know about C#, but in C++ you can just make a __asm {} comment and write your inline assembly code there. I think this would be the easiest way to experiment with assembly.


(*)although this isn''t actually always true, its easier if you just believe it is.

Share this post


Link to post
Share on other sites
quote:
Original post by Elite19
Anyway, there is something i dont get. If assembly language is code the machine can read directly, wouldn't there be a different language for every different processor out there? Will asm code i write for windows work with all other versions of windows? I dont get it.


You absolutely do get it. Assembly is inherently non-portable and it is not only a different language for every different processor the syntax of the assembly for the same CPU can vary by platform and compiler. All a HLA can do, is teach you central concepts. Many people (jokingly) call C portable assembly.

One of the reasons Intel has captured so much market share, is because they have made all of thier CPUs backwards compatible for over twenty years now. 808086 programs can run on a Pentium 4.

Assembly describes a class of languages that are one level removed from machine code. Assembly must be compiled, using an Assembler - it is said that "The Assembler assembles the assembly." Modern assemblers are macro assemblers, meaning you can effectively write functions. Assembly consists of mnemonics that represent similar operation codes (op codes or machine instructions).

The assembly mnemonic is the same, mov, but the machine code is not:

assembly: mov eax, 2
hex: B8 02 00 00 00
binary: 10111000 00000010 00000000 00000000 00000000

assembly: mov edx, eax
hex: 8B D0
binary: 10001011 11010000

assembly: mov eax, edx
hex: 8B C2
binary: 10001011 11000010


You can (and people once did) program in machine code. You key in the program, one byte at a time. You have to figure out the addresses to jump to and remember at what offsets data is stored. You can do this right now on any DOS or Windows PC, by running 'command.com' then 'debug.exe' and start entering op codes in hexadecimal to create a .com file. The code starts at offset 0x100 (the first 256 bytes are used for the com file header).

quote:

Also, can c# work with assembly? Say i had a program in c# that asked how to add 2+2 , could code i wrote in assembly and compiled to a dll calculate that and be called from c#?
Sorry if that sounds stupid, i am totally new to asm.


I think you would write:
int x = 2 + 2;

But yes, you can create a dll using an assembler, or a C or C++ compiler and inject assembly instructions then call it in your .Net program – I think you would use p/invoke to do think.


[edited by - Magmai Kai Holmlor on November 7, 2003 2:50:08 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by LessBread
An assembler might not be the thing you want. Consider, if you''re interested in learning about how your code actually works, why not direct your compiler to dump a listing of assembly code generated from your C source? (and maybe your C# source too). Of course, you''ll still need to understand assembly language, to make sense of the listing, but you don''t need to compile assembler to read it.

AoA is a great assembler reference too, hla or no. Also download the 3 volumes of the "IA-32 Intel Architecture Software Developer’s
Manual" -

ftp://download.intel.com/design/pentiumii/manuals/

24319002.pdf - vol 1
24319102.pdf - vol 2
24319202.pdf - vol 3



I agree. Looking at my code through the debugger is always insightful. It may be better if the code dump is shown step by step, beginning to end.



==================
Benjamin Heath
==================

Share this post


Link to post
Share on other sites
Wow, thanks Magmai. That helped heaps. I am finding assembly very interesting. One thing i''m wondering about though is, do many people out there use it? I mean, it seems like the fastest code you can write is asm other than "machine code" itself. Is it still usefull knowledge to have and wil computers in the future make the asm of today obselete?

Share this post


Link to post
Share on other sites
Thanks Lessbread and Benjamin, I somehow totally missed your posts. That sounds like a good idea but i would still like to play around with asm just for fun(I''m such a nerd)
That just made me think of something else, if high level languages like c++ get conveted to assembly, why would they be slower than assembly programs if they become asm programs anyway? I mean, if you wrote a hello world program in c++ then compile it, it becomes just the same as if you would have wrote hello world in assembly to start with wouldn''t it?

Share this post


Link to post
Share on other sites
quote:
Original post by Elite19
Wow, thanks Magmai. That helped heaps. I am finding assembly very interesting. One thing i''m wondering about though is, do many people out there use it? I mean, it seems like the fastest code you can write is asm other than "machine code" itself. Is it still usefull knowledge to have and wil computers in the future make the asm of today obselete?

If I remember correctly, if it hasn''t been stated before, it is given a 1-1 conversion rate between ASM and machine code, meaning that for every line of Assembly code, that creates one line of machine, while high level languages like C++, VB, Java, etc.. are 1-many conversions, meaning it takes many lines of machine code to actually execute what is represented by 1 line of that code. I don''t think ASM will become obsolete completely because someone has to be programming in it at some point, now it is just system coders, and game programmers that are looking for an added boost in performance with the libraries that they are using. Soon, game devs will probably not use it much, if at all, and then it will only be the systems guys, and I am fairly sure that embedded technologies, would still use a fair amount of ASM, since those machines aren''t quite as powerful as the desktop and laptops that we use today.

bastard2k5 is a Giant Dragon that leaves a Trail of Goo, Hovers Eerily, swallows Aeroplanes Whole, and eats Metal.
(Strength: 9 Agility: 9 Intelligence: 8)
Unleash your Giant Battle Monster.

Share this post


Link to post
Share on other sites
Yes, you can use ASM with C#, you make a C++ DLL and link that with your C# module, create a wrapper class in C# for the C++ DLL''s functionality and call the functions in the wrapper class.

Share this post


Link to post
Share on other sites
quote:
Original post by Elite19
Thanks Lessbread and Benjamin, I somehow totally missed your posts. That sounds like a good idea but i would still like to play around with asm just for fun(I''m such a nerd)
That just made me think of something else, if high level languages like c++ get conveted to assembly, why would they be slower than assembly programs if they become asm programs anyway? I mean, if you wrote a hello world program in c++ then compile it, it becomes just the same as if you would have wrote hello world in assembly to start with wouldn''t it?


C++ has a lot of overhead, maybe not much for a simple program like a Hello world app, but you can do all sorts of things to speed up your programs in assembly that aren''t possible in C/C++. Consider this:

SwapAXBX:
push cx
push dx
mov cx, ax
mov dx, bx
mov ax, dx
mov bx, cx
pop dx
pop cx
ret

Now, whenever you want to swap the CPU registers ax and bx, you can call this function. In C++, the same/similar code, you cannot work directly with the registers and you aren''t sure what variables are in what registers at any given time, so you work with variables all the time. You basically don''t have control on when to use a register or memory variable, so the asm code generated by the c/c++ app is constantly copying variables to/from registers when it would easily be possible to write it in such a way to get rid of a few of these. There is just less wasted CPU cycles in "good" asm code because of the control you have over variables and the registers. If you output a few C/C++ projects to assembly (and you know assembly), you can easily spot places where you can gain a bit of speed, and all these little things add up at the end (especially in a function called often). Even things like calling cos and sin in C/C++ are slower than asm, because in asm you can call cossin and get both cos and sin in a single instruction (although, you can write inline asm in your C++ and create a function that does this also).

Share this post


Link to post
Share on other sites
One thing to note about variables and such in assembler, type checking is very limited, basically just for bytes, words and double words. What is easily conceived of as a structure in C is merely a block of memory in assembler and where in C accessing a member of a structure is fairly straightforward, in assembler you'll need to calculate the byte offsets in the block of memory to access each member of the structure.

There are also a large number of ways to code the same computation on the x86 family of cpus, which are sometimes refered to as "CISC" - "complex instruction set computer". Of course, a computation can be coded in a large number of ways using a high level language too, but I brought that up to preface the point that Ready4Dis's byte swap procedure could also be coded using the bswap or xchg instructions.

One low level arithmetic operation that can be coded in assembler that can't be coded in C (but can be coded in other high level languages), is the fill right shift operator, sometimes expresseds using the symbol >>>. The assembly instruction is shr.

[edited by - lessbread on November 7, 2003 9:12:12 PM]

Share this post


Link to post
Share on other sites
Hi guys,

I use VC++ 6.0 for my programming and was wondering if it''s really worth learning some basic assembly to insert into my programs with the _asm keyword. Just for little things that get done over and over like vector multiplication for example.

Should I bother ?

Share this post


Link to post
Share on other sites
''I use VC++ 6.0 for my programming and was wondering if it''s really worth learning some basic assembly to insert into my programs with the _asm keyword.''

If you can improve on the code that''s produced by the compiler, sure. If not, don''t bother. The real question is does your program really need that level of optimization in the first place.

Share this post


Link to post
Share on other sites
quote:
Original post by LessBread
Also download the 3 volumes of the "IA-32 Intel Architecture Software Developer’s
Manual" -

ftp://download.intel.com/design/pentiumii/manuals/

24319002.pdf - vol 1
24319102.pdf - vol 2
24319202.pdf - vol 3



Only a few thousand pages to read there. The third is mostly for OS developers though.

quote:
Original post by Ready4Dis
C++ has a lot of overhead, maybe not much for a simple program like a Hello world app, but you can do all sorts of things to speed up your programs in assembly that aren''t possible in C/C++. Consider this:

SwapAXBX:
push cx
push dx
mov cx, ax
mov dx, bx
mov ax, dx
mov bx, cx
pop dx
pop cx
ret



Couldn''t you optimize that further with either xor or the xchg opcode?

Share this post


Link to post
Share on other sites
quote:
Original post by DukeAtreides076
Only a few thousand pages to read there. The third is mostly for OS developers though.


No doubt they''re huge, but they''re also well hyperlinked. At any rate, they''re not exactly the kind of books to read cover to cover.

Share this post


Link to post
Share on other sites