Assembly Language Lecture
Assembly Language Lecture
Programming
Dr JV Fonou Dombeu
Office: F03
1
Outline
Program from High-Level Language to Machine Language
What is Assembly Language
x86 Registers
8086 Assembly Language
Structure of an Assembly Program
Setting up DOSBox and TASM for Assembly Programming
Samples Assembly Programs
2
Program from High-Level Language to Machine
Language
3
What is Assembly Language
Assembly language – processor-dependent, low level programming
language
Processor-dependent - Each family of CPUs has it own set of
instructions of assembly language
There are many types for Assembly languages for different types of
CPU architectures;
The most popular CPU architectures - ARM architecture, MIPS
architecture and x86 architecture
ARM architecture - is usually used on cellphone and internet
systems, i.e. ios, android.
MIPS architecture - is used in video game console such as Sony
Playstation
x86 architecture - is used on intel PCs which is widely use in today
society,
x86 assemblers vary from 16-bit to 64-bit instructions
Focus is on x86 assembler in this lecture
4
What is Assembly Language (Cont.)
Why Assembly Language is Important
Allow programmer to communicate directly with hardware
using human readable texts
5
What is Assembly Language (Cont.)
To understand and learn assembly language, you need to know
Few basic features of computer hardware such as processor,
memory and registers
6
x86 Registers
7
8086 Assembly Language
8086 Assembly Language general syntax
Opcode Destination operand Source operand
e.g. mov bx,cx ;move the content of cx into bx, i.e. bx=cx
Opcode - identifies the operation to be performed
Destination operand - is where result of operation is stored
Source operand - is where data to be processed is located
Destination & source operands are separated by comma
8086 assembly instructions can not have more than
two operands
three types of operands – register, immediate and memory
Register operand - fast access, no bus access, short instruction size
e.g. mov bx,cx ;move the content of cx into bx, i.e. bx=cx
Immediate operand – represent constants, can only be source
operand
e.g. mov bx,8 ;move 8 into the bx, i.e. bx=8
add bx,12 ;add 12 to bx, i.e. bx=bx+12
Memory operand - require address computation & bus transfer,
memory operands are slow to access
e.g. mov eax, [ebx] ;Move the 4 bytes in memory at the address 8
8086 Assembly Language (Cont.)
Static Declarations – similar to global variables, using special
assembler directives
Data declarations – preceded by the .DATA directive
The directives DB, DW, and DD can be used to declare one, two, and
four byte data locations
Declared locations can be labeled with names for later reference —
this is similar to declaring variables by name
Example declarations:
.DATA
var DB 64 ;Declare a byte, referred to as location var, containing the value 64.
Var2 DB ? ;Declare an uninitialized byte, referred to as location var2.
DB 10 ;Declare a byte with no label, containing the value 10. Its location
is
var2 + 1
X DW ? ;Declare a 2-byte uninitialized value, referred to as location X.
Y DD 30000 ;Declare a 4-byte value, referred to as location Y, initialized to
30000.
9
8086 Assembly Language (Cont.)
Instructions – three categories – data movement, arithmetic, control
flow
Data movement instructions – mov, push, pop, lea
mov instruction - copies the data item referred to by its second
operand (i.e.register contents, memory contents, or a constant
value) into the location referredto by its first operand (i.e. a register
or memory).
Syntax
mov <reg>,<reg>
mov <reg>,<mem>
mov <mem>,<reg>
mov <reg>,<const>
mov <mem>,<const>
e.g. mov eax, ebx ;copy the value in ebx into eax
Syntax
push <reg32>
push <mem>
push <con32>
e.gs.
push eax ;push eax on the stack
push [var] ;push the 4 bytes at address var onto the stack
11
8086 Assembly Language (Cont.)
push and pop instructions – stack instructions
pop instruction – removes the 4-byte data element from the top of
the hardware-supported stack into the specified operand (i.e.
register or memory location).
It first moves the 4 bytes located at memory location [SP] into the
specified register or memory location, and then increments SP by
4.
Syntax
pop <reg32>
pop <mem>
e.gs.
pop edi ;pop the top element of the stack into EDI.
pop [ebx] ;pop the top element of the stack into memory at the
four
;bytes starting at location EBX.
12
8086 Assembly Language (Cont.)
lea (Load effective address) instruction - places the address specified
by its second operand into the register specified by its first operand.
Note, the contents of the memory location are not loaded, only the
effective address is computed and placed into the register. This is
useful for obtaining a pointer into a memory region.
Syntax
lea <reg32>,<mem>
e.gs.
lea eax, [var] ;the value in var is placed in EAX.
lea eax, [val] ;the value val is placed in EAX.
13
8086 Assembly Language (Cont.)
Arithmetic instructions – add, sub, mul, etc
Add - Integer Addition
The add instruction adds together its two operands, storing the result in
its first operand. Note, whereas both operands may be registers, at
most one operand may be a memory location.
Syntax
add <reg>,<reg>
add <reg>,<mem>
add <mem>,<reg>
add <reg>,<con>
add <mem>,<con>
E.gs.
add eax, 10 ;EAX ← EAX + 10
add BYTE PTR [var], 10 ;add 10 to the single byte stored at memory
ddress
var.
14
8086 Assembly Language (Cont.)
Arithmetic instructions – add, sub, mul, etc
Sub - Integer Subtraction
The sub instruction stores in the value of its first operand the result of
subtracting the value of its second operand from the value of its first
operand.
Syntax
sub <reg>,<reg>
sub <reg>,<mem>
sub <mem>,<reg>
sub <reg>,<con>
sub <mem>,<con>
E.gs.
sub al, ah ;AL ← AL - AH
sub eax, 216 ;subtract 216 from the value stored in EAX
15
Structure of Assembly Program
General Structure used in TASM
16
Setting up DOSBox and TASM
You may use the following links to obtain the respective DOSBox and
TASM for your machine.
17
18
Outline
8086 Assembly Language
MS-Dos Interrupt and Function Calls
Samples Assembly Programs
19
8086 Assembly Language (Cont.)
Instructions – three categories – data movement,
arithmetic, control flow
Data movement instructions – mov, push, pop, lea
Arithmetic instructions – add, sub, mul, etc.
Control flow – conditional or unconditional branches,
jumps
– jump, jCondition, cmp
jump - Transfers program control flow to the instruction
at the memory location indicated by the operand
Syntax
jmp <label>
e.g.
mov esi, [ebp+8]
begin: xor ecx, ecx
mov eax, [esi]
jmp begin ;Jump to the instruction labeled begin. 20
8086 Assembly Language (Cont.)
Control flow – conditional or unconditional branches, jumps
– jump, jCondition, cmp
jCondition – conditional jump, based on the status of a set of condition codes
that are stored in a special register called the machine status word/flags.
Machine status word - include information about the last results of arithmetic
operation performed. For example, one bit of this word may indicate if the
last result was zero, negative or positive.
last result of arithmetic operation – can be a cmp operation on the operands.
Syntax
je <label> (jump when equal)
jne <label> (jump when not equal)
jz <label> (jump when last result was zero)
jg <label> (jump when greater than)
jge <label> (jump when greater than or equal to)
jl <label> (jump when less than)
jle <label> (jump when less than or equal to)
e.g.
cmp eax, ebx
jle done ;If the contents of EAX are less than or equal to the contents of EBX,
;jump to the label done. Otherwise, continue to the next instruction.
21
8086 Assembly Language (Cont.)
Control flow – conditional or unconditional branches, jumps
– jump, jCondition, cmp
cmp - Compare the values of the two specified operands
Syntax
cmp <reg>,<reg>
cmp <reg>,<mem>
cmp <mem>,<reg>
cmp <reg>,<con>
e.g.
cmp eax, 10
jeq loop ;If the 4 bytes stored in EAX register are equal to the 4-byte
;integer constant 10,jump to the location labeled loop
.
22
MS-Dos Interrupt and Function Calls
MS-DOS – Microsoft Disk Operating System
DOS includes several built in programs for various systems related
operations.
DOS built in programs – use via special software Interrupt instruction called
INT.
The INT instruction - calls a DOS interrupt service routine (like a function) to
perform a special task. For example, to read from the keyboard or disk or
mouse, or to write a character to the screen we use special DOS functions.
Syntax
INT Interrupt Number
INT 21h – use to provide common services such as input-output, file handling,
and memory management, through various sub-functions.
E.gs. INT 21h sub-functions – 02h, 06h 09h, 4Ch, etc.
Specify sub-function to be executed - AH register is assigned a sub-function
number before the execution of the INT instruction.
E.g. 1. INT 21h sub-functions 02h and 06h: Write Character to Standard
Output
MOV AH, 02H
MOV DL,’A’ 23
INT 21H ;character.asm file name
MS-Dos Interrupt and Function Calls
(Cont.)
E.g. 2. Sub-function 09H of Interrupt 21H displays a $-terminated
string on the screen. The sub-function requires the offset of that
string to be passed in the DX register:
24