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.
Printable View
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 )