Nothing Special   »   [go: up one dir, main page]

Assemply Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 58

ASSEMPLY LAB

Eng. Sara Alkuhlani


2

LAB 1
3

Introduction
• What is an Assembly language?
• An assembly language is a type of low-level programming
language.
• Communicate directly with a computer’s hardware.
• It is converted by the assembler into executable machine-
language programs.
4

Numbering and Coding systems


• The most useful systems are:
1. Decimal system:
is composed of 10 numbers (0,1,2,3,4,5,6,7,8,9).
2. Binary System:
is composed of 2 binary digits (0,1).
3. Hexadecimal System:
is composed of 10 numbers {0,1,2,3,4,5,6,7,8,9} and 6
letters (A,B,C,D,E,F).
5

Numbering and Coding systems


• Converting from Decimal to Binary:

• Converting from Binary to Decimal :


6

Numbering and Coding systems


• Converting from Binary to Hex:
Replacing each 4-bit binary number with

Its hex equivalent starting from right to left.

• Converting from Hex to Binary:


Each hex digit is replaced with its 4-bit

binary equivalent
7

8086 CPU registers


15 8 7 0
AX AH AL Accumulator

Data Group BX BH BL Base


General purpose
register CX CH CL Counter

DX DH DL Data

SP Stack Pointer

BP Base Pointer
Pointer and
Index registers
SI Source Index

DI Destination Index
8

8086 CPU registers


15 0
CS Code Segment
Segment DS Data Segment
registers SS Stack Segment
ES Extra Segment

Flag 15 0
register  OF DF IF TF SF ZF  AF  PF  CF
9

General Purpose Register (GPR)


1. Ax (Accumulator) used for arithmetic an logic
expression and used for input and output data also
used in multiplication and division expression.
2. Bx (Base Register) used for addressing the memory.
3. Cx (Counter Register) used as a counter of loops.
4. Dx (Data Register) used in multiplication and division
expression and used for input and output data.
• All registers (Ax,Bx,Cx,Dx) are 16 bit register and consist
of low part and high part ex Ax as shown in the side.
10

Index and Pointer Register


1. SI (source Index) pointer that point to the source data in
the memory.
2. DI (Destination Index) pointer that point to the location
that the data will store in
3. SP (Stack Pointer) pointer that is point at the current
memory of stack location (top of stack)
4. BP (Base Pointer) pointer that point at location in stack,
and can also point to another segment
11

Segment Registers (SR)


1. CS (Code Segment) points at the segment containing
the code of the current program.
2. DS (Data Segment) generally points at segment when
variable are defined.
3. SS (Stack Segment) point at the segment containing
the stack.
4. ES (Extra Segment) is a segment register used as an
extra data segment
12

Status and control Registers


1. IP (instruction Pointer) it contain the address of the next
instruction to be execute within the current code
segment. IP & CS combine to form the complete
address of the next instruction.
2. Flag: a special register used to show the status of the
CPU or the results of arithmetic operations. Two types
(control flag & status flag).
:‫مالحظة‬
.)4 bit Hex( ‫( أي‬16 bit Binary( ‫• الرجسترات تستقبل كحد أقصى‬
‫ فً حالة عدم الكتابة‬, 0101B ‫ أو‬1234H ‫ مثال‬,‫• ٌجب كتابة نوع النظام للرقم‬
.decimal ‫فسٌعتبر الرقم عشري‬
‫‪13‬‬

‫‪Memory‬‬
‫• ٌرمز للذاكرة باسم الموقع بٌن أقواس مربعة ] [ مثال‪[BX] :‬‬
‫مالحظات‪:‬‬ ‫•‬
‫عند ادخال ‪ 4 bit Hex‬إلى الذاكرة ٌتم تقسٌمها إلى قسمٌن؛ أي انها تشبه النوع‬ ‫•‬
‫األول من الرجسترات )‪(AX,BX,CX,DX‬‬
‫الجزء األول ٌأخذ ‪ 2bit‬التً على الٌمٌن وٌرمز له كمثال‪[BX] :‬‬ ‫•‬
‫الجزء الثاني ٌأخذ ‪ 2bit‬التً على الٌسار وٌرمز له بموقع الذاكرة‪ 1+‬كمثال‪:‬‬ ‫•‬
‫]‪[BX+1‬‬
14

How to write a code in assembly


language?
• An assembly language instruction consists of four
fields, which they are:
[label:] instruction operands [;comment]
brackets [ ] indicate that field is optional
 Label field: (optional) refer to a line of code by name.
 Instruction & Operands fields: perform the real work of
program such as MOV Ax,6763h.
 Comments field: (optional) begin by “;” at the end of line,
the assembler ignore comments.
15

Directives:
• Directives (also called pseudo-instructions): statements give
directions to the assembler about how it should translate the
assembly instructions into machine code.
1. MODEL directive: selects the size of the memory model
(SMALL, MEDIUM, COMPACT, LARGE, and HUGE).
• Model directive: write as:

• Small model: is one of the most widely used. It uses a


maximum of 64K-Bytes of memory for code and another
64K-Bytes for data.
16

Directives:
2. Segment directive:
o .stack (marks the beginning of the stack segment)
o .data (define data that the program will use)
o .code (write instruction here)

 Every line in the assembly program must be correspond


to one of these segments.
17

Example:
18

MOV instruction
MOV destination, source
;copy source operand to destination
Ex:
MOV AX,468FH
To From
Segment Register Memory
Memory Segment Register
Segment Register Register
Register Segment Register
Memory Immediate
Register Immediate
Register Register
19

MOV instruction
• Example: the following program first loads CL with value
55H, then moves this value around to various registers
inside the CPU.

 OPEN emu8086 and write the first program.


20

MOV instruction
We have some notes:
1. Values can be moved among all registers except (FR).
2. Values can not loaded directly into any segment register
(CS, DS, ES, SS). But first load it to a non segment
register and then move it to the segment register
3. Moving a large value into register will cause error.
21

End lab1
No Home work today

Just Study hard for the next lab.


See you.
22

LAB 2
23

Data types
• DB (define Byte – 8-bit)
• DW (define word – 16- bit)
• DD (define Double Word – 32-bit)
• DUP (duplicate) to define a location of memory and give
its initial value as:
Data1 = DB 6 DUP(0FFh) ; define 6 location with FFH
and the first location pointed by Data1
• EQU (equate) for defining constant ex:
EQU 5AH
24

ADD instruction
• ADD destination, source
;add source operand to destination
• Ex:

• Then: AL=59H , BL=34H


• The ADD instruction can be rewritten as

• Writing a program that define and add two numbers of


type DB.
25

Example:
26

Flag Registers:

• Status flags:

1. Cary Flag (CF) set to 1 if there is a carry from d7 or d15 out.


2. Party Flag (PF) set to 1 if the lower order byte has an even number
of 1's.
3. Auxiliary Flag (AF) set to 1 if there is a carry from d3 to d4.
4. Zero Flag (ZF) set to 1 if the result is zero.
5. Sign Flag (SF) set to 1 if the result is negative (must bit = 1).
6. Overflow Flag (OF) will explain later.
27

Flag Registers:
• Add instruction affect on the flag registers:
1. CF, Carry flag
2. ZF, Zero flag
3. SF, Sign flag
4. PF, parity flag
5. AF, Auxiliary carry flag

• Exercises: Write assembly program that define and add


the numbers 435AH and A34BH and show how flag
registers are affected.
28

80x86 addressing modes:


• In 8086 microprocessor, address bus has 20-bits then its
access memory 𝟐𝟐𝟎 = 1,048,676 = 1Mbytes. (00000h) to
(FFFFFh).

In the other side


• All registers have 16-bits then its access 𝟐𝟏𝟔 = 65536 =
64K-bytes.
• So … its impossible to access the memory, for this
reason, the memory is divided to segments (each
segment = 64K-bytes) (0000h - FFFFh).
• Inside each segment, any address can be identify by
offset.
29

80x86 addressing modes:


F0000
E0000 7000 FFFF

D0000 One segment


64-KByte
C0000
:
70000
60000
50000 7000 0250

40000 0250
30000 7000 0000
20000
10000 Seg offs
00000
30

80x86 addressing modes:


• 8086 has three types of addresses (physical – offset - logical)
Physical address: is the 20-bits address that is actually
put on the address bus, this is the actual physical location
in RAM or ROM.
Offset address: is a location within a 64k-bytes segment
range.
Logical address: consists of a segment value and offset
address.
• The logical address of an instruction always consist of CS
& IP (CS:IP)
31

80x86 addressing modes:


 Physical address = (segment value * 16) + offset
 Or by shifting the segment value left one hex digit and then
adding it to the offset address.
 Example: if logical address (9A32h:001Bh), determine the
physical address??
9A320 + 001B = 9A33B (physical address)

 Then, to determine the address in the memory, used


segment value & offset (Segment : Offset).
Ex. (AABB:5566). Its called logical address.
32

Example for addressing mode


If CS=25F6H and IP=635AH find
1. The logical address
2. The offset address
3. The physical address
4. The lower range
5. The upper range
The solution
1. The logical address is 25F6:635A
2. The offset address is 635AH
3. The physical address is 25F60 + 635A = 2C2BA
4. The lower range is 25F60 + 0000 = 25F60
5. The upper address is 25F60 + FFFF = 35F5F
33

Exercise

• Add 5 bytes of data (25H, 12H, 15H, 1FH, and 2BH).


34

End lab2
35

LAB 3
36

Overflow Flag:
• The overflow is happened if the result of an operation on
signed number is too large for the register . The CPU
indicate the problem by rising the OF flag but it is up to
the programmer to take care of it.
• Example
MOV AL,96 96 0110 0000
MOV BL,70 +70 +0100 0110
166 1010 0110
ADD AL,BL
CF = 0 , ZF = 0 , AF = 0 , PF = 1 , SF = 1 , OF = 1
37

Overflow Flag:
• When the overflow flag (OF) is set to 1 at 8-bit operation?
1. If there is a carry from D6 to D7 but no carry out from D7
(CF = 0)
2. If there is a carry from D7 out (CF = 1) but no carry from D6
to D7
• Overflow flag in 16-bit operations:
In a 16-bit operation OF set to 1 in either of two cases:
1. If there is a carry from D14 to D15 but no carry out from D15
(CF = 0)
2. If there is a carry from D15 out (CF = 1) but no carry from
D14 to D15
38

Overflow Flag:
MOV AX,6E2FH 6E2F 0110 1110 0010 1111
MOV CX,13D4H +13D4 +0001 0011 1101 0100
8203 1000 0010 0000 0011
ADD CX,AX
• CF = 0 , ZF = 0 , AF = 1 , PF = 1 , SF = 1 , OF = 1

• Exercise:
Show how the flag register is affected by:
MOV AX,B4F5H
ADD AX,95EBH
39

SUB instruction
• SUB destination, source ;subtract source operand from
destination
EX: SUB A,B ;means A=A-B
• In subtraction, we take the 2's complement of the source
operand and then add it with destination.
• Example: The 2's complement of 31H is
MOV AL,0FH 0011 0001 1100 1111 CF
SUB AL,31H 0F 0000 1111
+CF +1100 1111
DE 1101 1110
CF = 1 , ZF = 0 , AF = 1 , PF = 1 , SF = 1 , OF = 0
There is no carry from the subtraction but CF = 1 means borrow
40

INC and DEC Instructions


• INC destination ;destination= destination+1
• DEC destination ;destination= destination-1

• The instruction of INC (increment) and DEC


(decrement) does not affect the carry flag.
• These two instruction used in Loop instruction.
41

Addressing mode
Addressing mode: represent How CPU access the data.
• 80x86 provide a seven distinct addressing modes:
1. Register
2. Immediate
3. Direct
4. Register indirect
5. Based relative
6. Indexed relative
7. Based indexed relative
42

Addressing mode
1. Register addressing mode:
In this addressing mode, the data is taking from one
register to another register means no access to memory in
addressing mode.
Example: MOV BX,DX

2. Immediate addressing mode:


In this addressing mode, the source operand is constant.
Example: MOV AX,2550H
Note (immediate value must be only source it couldn’t be a
destination like:
MOV 2250H,AX ;wrong)
43

Addressing mode
3. Direct addressing mode:
In direct addressing mode Data is in memory location(s) and the
address of the data in memory come immediately after
instruction.
The different between immediate addressing mode and
direct addressing mode is
In immediate addressing, the operand its-self provided with the
instruction, whereas in direct addressing the address of the
operand is provided like:
MOV Dl, [2400]; move the content of DS:2400H into DL
44

Addressing mode
4. Register Indirect addressing mode:
In register indirect, the address of the memory location where
the operand inside is hold by a register ,and the registers that is
used for this purpose are:
SI,DI and BX if these three register are used as pointer, they must
be combined with DS in order to generate the 20-bit physical
address.
Example:
MOV AL, [BX] ; move into AL the contents of memory location DS:BX
MOV CL, [SI] ; move into CL the contents of memory location DS:SI
MOV DL, [DI] ; move into DL the contents of memory location DS:DI
45

Addressing mode
5. Based relative addressing mode:
In this type of addressing, base register (BX and BP) as well as
displacement value is used to calculate the effective address.
The default segments used for calculating physical address are
DS for BX and SS for BP.
Example:
MOV CX, [BX] + 10; move DS:BX +10 and DS:BX +10 +1 into CX,
MOV AL, [BP] + 5; move SS: BP +5 into AL
46

Addressing mode
6. Indexed relative addressing mode:
• The Indexed relative addressing mode work as Based relative
except the register (SI and DI) hold the offset address
Example:
MOV DX, [SI] + 5; Physical address = DS(shift left) + SI +5
MOV CH, [DI] + 20; Physical address = DS(shift left) + DI +20
7. Based Indexed addressing mode:
By combining based and indexed addressing mode a new
addressing is derived called the based indexed addressing mode
Example:
MOV AL, [BX][SI] + 8; Physical address = DS(shift left) + BX + SI +8
47

Exercise:

Define tow data of type DW D98A and 646F , then do the


following operation:
ADD , SUB , INC , DEC .
48

LAB 4
49

Review Questions
1. Show how the flag register is affected by adding the
following data: AAAA , 5556
2. Which register is used to access the code segment?
3. What is the range of physical addresses if CS=FF59?
4. If BX=1234h and the instruction “MOV [2400],BX” were
executed, what would be the contents of memory
locations at offsets 2400 and 2401?
50

The Stack
Stack is a section of read/write memory (RAM) used by the
CPU to store information temporarily.
• Register inside the CPU that point to stack:
1. SS (stack Segment) which point to the segment contain the
stack
2. SP (Stack Pointer) point at the current memory location (the
top of the stack). It decrement as the data pushed and
increment as the popped
• Instruction that deal with stack are:
1. Push means push data to stack (store the data inside the
stack)
2. Pop means pop data from stack (return the data from stack
to CPU)
51

The Stack
• Note:
1. Push and Pop instruction always used with word register (16-
bit) means Push AX not push AL or Push AH
2. At push operation SP is decrement by two, and at Pop it
increment twice
 Example 1:
Assume SP= 32F6H, Ax = 24B6H, DI = 85F9H and DX = 5F93H
write the address that SP point to and the contain of the stack
after these push instruction
PUSH AX
PUSH DI
PUSH DX
52

The Stack
53

The Stack
 Example 2:
Assume SP 18FAH show the content of the stack and register
after these pop instruction
POP CX
POP DX
POP BX
54

The Stack
 Pushing onto the stack:
• Notice in Example 1 that as each PUSH is executed, the
contents of the register are saved on the stack and SP is
decremented by 2.
• Notice also how the data is stored on the stack. In 8086, the
lower byte is stored in the memory location with the lower
address.
 Popping the stack:
• Popping the contents of the stack back into the 8086 CPU is the
opposite process of pushing. With every pop, the top 2 bytes of
the stack are copied to the register specified by the instruction
and the SP is incremented twice.
55

The Stack
• Note:
1. In 8086 literature the top of the stack is the last stack
location occupied.
2. BP is another register that can be used as an offset into
the stack.
56

Exercises on addressing mode:


• Example 1

• Example 2
57

Exercises on addressing mode:


• Example 3

The offset register for various segments:

Segment register CS DS ES SS
Offset register IP SI,DI,BX SI,DI,BX SP,BP
58

Exercise

You might also like