Ok what is Assembly like as a language(I have never seen any code in Assembly) and why is it necessary for game programmers to know??
Printable View
Ok what is Assembly like as a language(I have never seen any code in Assembly) and why is it necessary for game programmers to know??
Assembly is estually machine code. It is usfull but no necessary to know.
it's faster!
but u need to write more!
Actually, that depends on the coder :DQuote:
Originally posted by pode
it's faster!
Can anyone give me a quick example of assembly code like a line or 2 if you don't mind. Also how is it faster if it is machine code which as far as I know is all 0's and 1's so how is that faster. Please help.
example
what does the above code do?? nothing useful! :D that's the kind of syntax you are looking at though.Code:XOR EAX, EAX
INC EAX
SHL EAX, 2
PUSH EAX
CALL MyFunc
MOV EBX, [ECX]
Generally people will say that ASM code is faster because you have more control over what is going on and therefore you dont put in unnecessary code. But like Magos says, it depends on the coder.
These days, the optimisations that compilers do for you is enough to get the extra speed. i'll bet there are few people on this board that can write better ASM code than an optimising compiler.
Most of the time if you are looking for extra speed, you should rethink your algorithm! or the way you are going about your algorithm.
Bobish is right, it's handy to know... and useful when you really need to use it, and it really helps understand what's going on inside the PC when you are running yorr code.. but it's not necessary
hope that helps
U.
To do something realistic in ASM (not just part of your code using an inline assembler) is a hair tearing experience (but amazingly there are still diehard dedicated ASM coders in abundance out there on the web!)
I had a mess around for a while with 32Bit ASM calling WINAPI functions....not because I felt it was important, but it helped me work out and understand a few key things that I took for granted before......For example I have attached a program I wrote in for the TASM32 assembler.......It produces a window on the screen with a catchy caption!
It resizes the text when the window size changes...uses custom icons (the TASM package comes with BRC - the borland resource compiler which works for C progs too).....implements a basic message procedure..and does what you would expect to do in VC++ with virtually no effort.....but I must admit doing this in ASM was a killer at times.....
Still...keeps me from doing anything constructive with life :)
Hey, where could I pick up a good 32 bit assembly compiler? I'd like to get back into assembly; last time I touched it I was programming apps for the P100 :) (Weren't those the days?)
Probably the best free 32bit assembler (well for windows coding if that's your interest) is MASM......
The best pace to get it is this site here which is owned by a guy called Steve Hutchesson....he writes libraries and updates for it from time to time too.....
hey thanx alot.
i'm a fan of TASM myself. do a search for TASM 5 on google and i'm sure you'll find a download somewhere. Someones even making an IDE for it.
good luck!
U.
Ok I would thank you all (if I had any idea what the heck you were talking about:confused: , jj) How in the world is Assembly faster(depending on the coder I know) than lets say C or C++, how does it give you more control, and what parts of games would you use it in.
Also I got one other question, I don't know how old all of you guys are but what is a good age to start learing C, C++, or Assembly. I am 14 right now and trying to learn C when did you start and how long am I in for studying.
When you write code in C, or C++ it is
translated into assembly by the compiler,
and then assembled by the assembler.
Since the compiler is turning the high level
code into assembly, it has the tendency
to include alot of unnecessary code.
This problem has been worked on since
compilers were first developed, and we've
come a long way. This is the reason that
it is possible to write faster code in asm.
- transmitt
Assembly programs are direct instructions to your computer. Each command corresponds to a specific machine code instruction. It has a very small number of simple commands, basically MOV (move) this here or ADD that to this. It is much more time consuming than high level languages but the benefit is you know exactly what your code is doing. When you program in C/C++ or other high level languages, you use a compiler to translate the code to assembly before it becomes machine code. Even though commercial compilers are highly optimized (better than your average assembly coder) it is not a direct translation. In game programming, speed is a main goal so you don't want any overhead. Thus, game programmers use assembly when they need to make sure things get done quickly (quite often for low-level graphics routines). It should be noted that unless you are a good assembly programmer it would be much better to use a language like C or C++ and let the compiler take care of optimizations (you could actually make the program go slower).
Here is a link you might find useful.
http://webster.cs.ucr.edu/Page_asm/ArtOfAsm.html
I hate it when someone else posts the same thing at the same time.
When you say that it is faster to program in Assembly do you mean it is faster to make or it runs faster because of how it is set up.
Obviously you haven't read the threads closely. Here's a small assembly program from one of my classes. It takes more time to program in assembly but the speed of the program itself can be reduced if done properly.
Code:;;Program that prompts for user to enter two signed numbers. Prompts user to
;;select and operation to do to the numbers Add, subtract, multiply or divide
;;Calculates and displays the result of the selected option
INCLUDE PCMAC.INC
.MODEL SMALL
.STACK 100h
CR EQU 13
LF EQU 10
ENDSTR EQU '$'
.DATA
Msg1 DB 'Enter the first number: ', ENDSTR
Msg2 DB 'Enter the second number: ', ENDSTR
Msg3 DB 'Do you want to add, subtract, multiply or divide?', ENDSTR
Msg4 DB '(Enter A,S,M or D): ', ENDSTR
Msg5 DB 'The Product is: ', ENDSTR
Msg6 DB 'The Sum is: ', ENDSTR
Msg7 DB 'The Difference is: ', ENDSTR
Msg8 DB 'The Dividend is: ', ENDSTR
Msg9 DB 'The Remainder is: ', ENDSTR
NUM1 DW ? ;;variable that holds the first number
NUM2 DW ? ;;variable that holds the second number
CHOICE DB ? ;;holds user's choice
REMAIN DW ? ;;holds the remainder from division
.CODE
EXTRN GetDec : NEAR, PutDec : NEAR
A5 PROC
mov ax, @DATA
mov ds, ax
_PutStr Msg1 ;;displays prompt to get first number
call GetDec
mov Num1, ax ;;saves first number as NUM1
_PutStr Msg2 ;;displays prompt to get second number
call GetDec
mov Num2, ax ;;saves second number as NUM2
mov bx, NUM2 ;;puts NUM2 into bx for arthimetic for entire program
_PutCh LF
_PutStr Msg3 ;;Prompts user to enter their selection
_PutStr Msg4 ;; A, S, M or D
_GetCh Echo
mov CHOICE, al ;;saves user's choice as CHOICE
_PutCh LF, LF, CR
mov al, CHOICE
cmp al, 53h ;;compares CHOICE for subtraction option
je SUBTRACT
cmp al, 41h ;;compares CHOICE for addition option
je ADDIT
cmp al, 4Dh ;;compares CHOICE for multiplication option
je MULTIPLY
;;falls through to division if no jump
;;---------------------Division Option-----------------------------
_PutStr Msg8 ;;outputs string to display quotient
mov ax, NUM1
cwd
idiv bx ;;divides NUM1 by NUM2
mov REMAIN, dx ;;saves remainder in REMAIN
call PutDec ;;displays quotient
_PutCh LF, CR
_PutStr Msg9 ;;outputs string to display remainder
mov ax, REMAIN
call PutDec ;;displays remainder
jmp DONE ;;jumps to end of program
;;---------------------Addition Option------------------------------
ADDIT: _PutStr Msg6 ;;outputs string to display sum
mov ax, NUM1
add ax,bx ;;adds NUM2 to NUM1
call PutDec ;;outputs sum
jmp DONE ;;jumps to end of program
;;-------------------------Subtraction Option-----------------------
SUBTRACT: _PutStr Msg7 ;;outputs string to display difference
mov ax, NUM1
sub ax, bx ;;subtracts NUM2 from NUM1
call PutDec ;;outputs difference
jmp DONE ;;jumps to end of program
;;----------------------------Multiplication Option-----------------
MULTIPLY: _PutStr Msg5 ;;outputs string to display product
mov ax, NUM1
imul bx ;;multiplies NUM1 by NUM2
call PutDec ;;outputs product
DONE: _EXIT 0
A5 ENDP
END A5
Wow, I've never seen assembly and I understood most of that. Yes I know there were comments, but I didn't actually read them. Who does? :D
Assembly Language and machine code are two (2) different things. Assembly is a language, just like 'C' is a language. It is a higher-level representation of machine code. C is a higher-level representation of Assembly.
Assembly is the language you write code in, when working with an Assembler. It allows you to have labels, to name functions, to access API functions via name, and to have variables by names, rather than RAM offsets.
'Machine' code is the object itself. Actual binary. If you were to see a hex coredump-- you'd be looking at machine code. Nobody writes in machine code (like maybe 1% of the folks on _old_ systems might still have to do it for DoD and governmental projects for bootstrap loaders, or such).
The reason game programmers use more assembly than the average programmer, is because in games, cycle times count-- you want to get every erg of energy out of the processor, and the most effecient way to do that is to use assembly judiciously to improve frame rates.
No compiler can currently beat an expert professional assembly programmer at optimization. A compiler has _no idea_ what the programmer is trying to do-- it just pulls snippets in from libraries and binds them together. So a compiler's code tends to be slightly generic by nature.
---
I recommend that all C/C++ programmers try to learn enough assembly that they can write stubs or snippets as necessary. Not only will this dramatically improve your ability to debug your higher-level code, but it will also open your eyes to many things about how a computer works, how your software runs, how the O/S works--- all things that a professional programmer learns.
---
Understanding how things work, lets you overcome seemingly otherwise insurmountable obstacles. For example, let's say you created an interrupt to a callback function. But while in that callback function, you can't access your global variables or created variables at all, and yet you need to allocate variables so that you can do the work)-- how could you do this?
If it were me, I would take the queue element structure, an O/S structure, resize it by 32-bits, and store the pointer to a RAMblock in those newly acquired bytes. The RAMblock would be a structure containing enough space for my callback's variables. Then I'd set the rest of the interrupt up and be on my way.
Then when the interrupt fired and the callback got called, I could use a little assembly language on entry to my callback to recover the address of the queue element structure that my interrupt process hangs off of. I could then reclaim that pointer, and be able to access variables from within my callback.
what can I say, I live this stuff everyday.
Well
Seeing as how I am currently just trying to start learning C I took 1 look at that code in Assebly then at the clock and realized I had been staring for quite some time...
Anyways that was quite possibly the most confusing thing I have ever seen(sorry to say).
This gave me another question though, you said that if you can program in C and C++ then you should learn enough assebly to make snippets of code. Then how do you insert or use these "snippets"?
Do you simply insert them like say a library or what??
The actual insertion depends on the compiler, however most of them will use an
Code:asm{
//assembly here}
//or
_asm{
//assembly here}
//or
__asm{
//assembly here}
Ok so you just basically put them in as you would javascript in HTML, that is seems simple enough as soon as I learn more about C's language and grammar(man I thought programming with one language would be hard, what about putting in like 3 or 4 others, I guess I am going to be doing a lot of reading :D )