Assembly Language??

Started by
38 comments, last by Jason2Jason 21 years, 7 months ago
quote:Original post by Anonymous Poster
Perhaps you might try using your compilers optimiser. I don''t think you''ll find all (optimising) compilers do what you have suggested at all.
I''ve tried that. The point here is that the compiler has no idea that I don''t have any use for that variable after that loop, and thereby it is meaningles to update it all the time. Only I know that! And frankly, I would be pissed if it didn''t update it without me telling it to do so; and I have no idea how to do that without using assembly.

Advertisement
First post!

Summary: use asm to speed up critical sections of your code.

As to learning asm, check out http://webster.cs.ucr.edu/Page_asm/ArtOfAsm.html
Once you''re good, look at http://www.azillionmonkeys.com/qed/tech.shtml and http://www.agner.org/assem/ for tips on optimizing.

You can beat VC++ easily with stuff like optimizing memory access and branch reduction (I have yet to see it emit cmovcc). If you use SIMD instructions, you win hands down (I got a speedup of 2.5x on a recent project using 3DNow! Pro).

@SwSh:

Your C++ code needs to declare the function:
extern "C" void asm_funct();

In your asm file, mark the function as global:
global _asm_funct
(don''t forget the leading underscore)

assemble with nasm -f coff x.asm ; link with x.o, and you should be good.

HTH+HAND
Jan Wassenberg , www.stud.uni-karlsruhe.de/~urkt/
E8 17 00 42 CE DC D2 DC E4 EA C4 40 CA DA C2 D8 CC 40 CA D0 E8 40E0 CA CA 96 5B B0 16 50 D7 D4 02 B2 02 86 E2 CD 21 58 48 79 F2 C3
A very BIG thanks!
Assembly v. C++ depends on the type of processor. CISC benefit more from assebly because the programmer can make better use of the wide array of instructions than a compiler. However, a RISC system compiles C++ practically equal or even better (in large programs) that a human could write assembly because of the reduced instruction set. At my school we are learning assembly for the MIPS architecture, I highly recommend it (Nintendo and other large companies use MIPS processors a lot).
Assembly v. C++ depends on the type of processor. CISC benefit more from assebly because the programmer can make better use of the wide array of instructions than a compiler. However, a RISC system compiles C++ practically equal or even better (in large programs) that a human could write assembly because of the reduced instruction set. At my school we are learning assembly for the MIPS architecture, I highly recommend it (Nintendo and other large companies use MIPS processors a lot).
You shouldn''t learn assembly until you know at least one other langauge and you have an idea about how the processor actually works.

I think people really underestimate how important assembly is. Everyone says: "You won''t need it that much, so you shouldn''t spend too much time with it," but that simply is not true. Assembly is the next step to machine code, and learning it really helps you with your overall programming skills. When you learn assembly you also learn about your computer and how things work. Anyone can agree that this will help you build better programs and fix bugs when they surface. Besides all of that, Assembly is a fun language (you''re probably thinking: "are you crazy?") The thing I like about it is that it is basically bare bones. You are directly entering commands to the processor so you know exactly what is going on and you can track down bugs easily. When written correctly and commented, assembly code can be easier to read than the same C++ code. Reading uncommented garbled assembly snippets (ie. disassemblies) is a very big pain, I''ll admit that. But sometimes a straightforward command driven syntax is better on the eyes than a complex mess created in C.

I kinda feel like I''ve been rambling .. sorry What I really want to say is to try making some assembly programs yourself, if you like it, don''t get discouraged by what people say about it ("It''s no longer needed," "It''s not structured," etc)
quote:Original post by blueEbola
You shouldn''t learn assembly until you know at least one other langauge and you have an idea about how the processor actually works.
Just want to correct a little here, although I think you agree based on the rest of your post, that learning assembly is probably the first step and the best way to learn how the processor works.

quote:I''ve tried that. The point here is that the compiler has no idea that I don''t have any use for that variable after that loop


Why would the compiler have no idea that you didn''t have any need for the variable? Which compilers have you tried this on? I just tried MSVC and it produced code remarkably similar to your asm version -


  #include <stdio.h>int main(){	int i=10;	while(i--)	{		printf("%d\n",i);	}	return 0;}  


produces


  _main	PROC NEAR					; COMDAT; 16   : {  00000	56		 push	 esi; 17   : 	int i=10;  00001	6a 0a		 push	 10			; 0000000aH  00003	5e		 pop	 esi$L1325:; 18   : 	while(i--)  00004	4e		 dec	 esi; 19   : 	{; 20   : 		printf("%d\n",i);  00005	56		 push	 esi  00006	68 00 00 00 00	 push	 OFFSET FLAT:??_C@_03PMGGPEJJ@?$CFd?6?$AA@  0000b	ff 15 00 00 00	00		 call	 DWORD PTR __imp__printf  00011	85 f6		 test	 esi, esi  00013	59		 pop	 ecx  00014	59		 pop	 ecx  00015	75 ed		 jne	 SHORT $L1325; 21   : 	}; 22   : ; 23   : 	return 0;  00017	33 c0		 xor	 eax, eax  00019	5e		 pop	 esi; 24   : }  0001a	c3		 ret	 0_main	ENDP  


quote:And frankly, I would be pissed if it didn''t update it without me telling it to do so; and I have no idea how to do that without using assembly.


That''s why it''s an optimising compiler. It can figure out stuff like this.





I think the reason it didn''t produce such code when I compiled, was that it was a line drawing routine with 3x5 different loops using the same variable as counter. It would always only use one of these loop, but due to how it is structured the compile couldn''t (and shouldn''t!) know that intent.
quote:but due to how it is structured the compile couldn''t (and shouldn''t!) know that intent.


Why not? I''m not sure what you meant by your description of your code, but in theory the compiler should be able to tell the duration of the use of a variable and whether this would make it a candidate for register storage. I tried a nested loop based on the code I posted above that did different things to the variable ''i ''(and continued to use the variable after the loops had finished), and couldn''t befuddle the compiler enough into writing the intermediate results to memory.

I suppose that it you''re using allot of different counters then there''s a chance that the compiler might choose the wrong one to cache in a register; in which case you may have to hand optimise it. But this is a bit of a stretch from your all compilers are ''this'' stupid post.

This topic is closed to new replies.

Advertisement