Assembly language is hard - but it's better than writing machine code !!
Remember that ultimately, EVERYTHING becomes ones and zeroes in the machine...
Assembly language is a symbolic representation of the machine code
used to write programs for an architecture. Primitive!
Once a program has been compiled, it is difficult - or impossible -
to decompile the code back to its HLL.
Debuggers will frequently only show the program code in assembly.
Assembly language is often the only tool available for implementing
some low-level tasks, such as bootloaders, drivers and low-level kernel components.
Assembly code has less overhead than code written in HLLs, so it may run faster.
HOWEVER: Developing a program in assembly is a very time consuming process!.
Assembly instructions have a 1-to-1 relationship with the machine instructions.
An assembler Turns assembly code into machine code.
MOV AL, 5 (Assembly Language)
10110000 00000101 (Machine Language)
Assembly Machine
-------- -------
MOV AX => assembler => 10110000
5 => assembler => 00000101
Do you need to understand computer architecture and software execution better? YES!
To work with assembly language programs we must:
learn how the computer represents data and instructions
learn how the computer manipulates data and instructions
learn how the represented information is organized
learn about assembly language
syntax
representation
learn about the internals of the computer
registers / cpu
ram
organization of data and instructions into a program
learn in detail how a program is executed
ADD adds the contents of the source to the destination
add destination, source
<<--------
add al, 3
add al, bl
add al, x
EXAMPLE ASSEMBLY CODE (from emu)
=====================
name "add-sub"
org 100h
MOV al, 5 ; bin=00000101b
MOV bl, 10 ; hex=0ah or bin=00001010b
; 5 + 10 = 15 (decimal) or hex=0fh or bin=00001111b
add bl, al
; 15 - 1 = 14 (decimal) or hex=0eh or bin=00001110b
sub bl, 1
; print result in binary:
MOV cx, 8
print: MOV ah, 2 ; print function.
MOV dl, '0'
test bl, 10000000b ; test first bit.
jz zero
MOV dl, '1'
zero: int 21h
shl bl, 1
loop print
; print binary suffix:
MOV dl, 'b'
int 21h
; wait for any key press:
MOV ah, 0
int 16h
ret
Same program with machine code
======================================================================
[LINE] LOC: MACHINE CODE SOURCE
======================================================================
[ 1] : name "add-sub"
[ 2] :
[ 3] : org 100h
[ 4] :
[ 5] 0100: B0 05 MOV al, 5 ; bin=00000101b
[ 6] 0102: B3 0A MOV bl, 10 ; hex=0ah or bin=00001010b
[ 7] :
[ 8] : ; 5 + 10 = 15 (decimal) or hex=0fh or bin=00001111b
[ 9] 0104: 02 D8 add bl, al
[ 10] :
[ 11] : ; 15 - 1 = 14 (decimal) or hex=0eh or bin=00001110b
[ 12] 0106: 80 EB 01 sub bl, 1
[ 13] :
[ 14] : ; print result in binary:
[ 15] 0109: B9 08 00 MOV cx, 8
[ 16] 010C: B4 02 print: MOV ah, 2 ; print function.
[ 17] 010E: B2 30 MOV dl, '0'
[ 18] 0110: F6 C3 80 test bl, 10000000b ; test first bit.
[ 19] 0113: 74 02 jz zero
[ 20] 0115: B2 31 MOV dl, '1'
[ 21] 0117: CD 21 zero: int 21h
[ 22] 0119: D0 E3 shl bl, 1
[ 23] 011B: E2 EF loop print
[ 24] :
[ 25] : ; print binary suffix:
[ 26] 011D: B2 62 MOV dl, 'b'
[ 27] 011F: CD 21 int 21h
[ 28] :
[ 29] : ; wait for any key press:
[ 30] 0121: B4 00 MOV ah, 0
[ 31] 0123: CD 16 int 16h
[ 32] :
[ 33] 0125: C3 ret
[ 34] :
; Example assembly language program -- adds 158 to number in memory
; Author: R. Detmer
; Date: 10/2004
.386
.MODEL FLAT
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
.STACK 4096 ; reserve 4096-byte stack
.DATA ; reserve storage for data
number DWORD -105
sum DWORD ?
.CODE ; start of main program code
_start:
MOV eax, number ; first number to EAX
add eax, 158 ; add 158
MOV sum, eax ; sum to memory
INVOKE ExitProcess, 0 ; exit with return code 0
PUBLIC _start ; make entry point public
END ; end of source code
EXAMPLE ASSEMBLY / C++ CODE
==============================
int Func2(int first, int second)
{
__asm
{
MOV eax,0
MOV ecx,second
L1: ;loop adds first to itself, second amout of times
add eax,first ;same as multiplying first by second
loop L1
MOV first,eax ;moves value from eax to variable 'first' to return
}
return first;
}