.. Assembly Language: What is it? Why do we use it?
Assembly language is hard - but it's better than writing machine code !!
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.
Remember that ultimately, EVERYTHING becomes ones and zeroes in the machine...
An assembler Turns assembly code into machine code.
mov AL, 5 (Assembly Language)
10110000 00000101 (Machine Language)
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
learn about the internals of the computer
registers / cpu
organization of data and instructions into a program
learn in detail how a program is executed
.. MOV instruction
"mov" transfers contents of the source to the destination
mov destination, source
mov AL, 3 ; move an IMMEDIATE value of 3 into al
machine code of instruction is:
'B0' => mov AL (with an immediate value)
'03' => actual 3
mov AL, var1 ; move contents of RAM at count to 8 bit AL register
machine code of instruction is:
'A0' => mov AL (with a memory address)
'00010400' => address of var1
Both operands must be of the SAME size
lwsum DD 12345678h ; low byte of longword stored at low address
wsum1 DW 1234h, 5678h ; low byte of EACH word stored at low address
wsum2 DW 5678h, 1234h ; low byte of EACH word stored at low address
bsum1 DB 12h, 34h, 56h, 78h ; low byte of EACH byte stored at low address
bsum2 DB 12h, 34h, 56h, 78h ; low byte of EACH byte stored at low address
; SHOW MEMORY!!
mov wsum1, 255 ; decimal 1255 in ram at wsum
mov AL, 34h ; 34 hex (52 dec) in 8 bit register AL
mov BL, AL ; contents of AL to BL
mov DX, 3*4 ; 12 dec to DX
mov BX, wsum2 ; contents of memory at SUM to BX reg
mov AL, 256 ; error: too large
mov BX, AbyteVar ; error: size mismatch
mov CX, BL ; error: operand not same size
mov wordVar1, wordVar2 ; error: mem-to-mem
.. ADD instruction
"add" adds the contents of the source to the destination
must be same size
no mem to mem
add destination, source
add AL, 3
add AL, BL
add AL, x
add BX, CX
.. Example Program Code
EXAMPLE ASSEMBLY CODE (from emu)
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.
mov DL, '1'
zero: int 21h
shl BL, 1
; print binary suffix:
mov DL, 'b'
; wait for any key press:
mov AH, 0
Same program with machine code (ignores capitalization differences)
[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
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
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)
mov ECX, second
L1: ;loop adds first to itself, second amout of times
add EAX, first ;same as multiplying first by second
mov first, EAX ;moves value from eax to variable 'first' to return
.. The Process of Assembly
Assembler reads what?
How many times through?
How does the symbol table work with the assembler?
What is the final output of the assembler?
So, we're now ready to execute the code?
NO! - what creates the exe?
emu: assembling the assembly code
NOTE: emu8086 creates SIX files
hand in ALL files when submitting an aseembly project into D2L