Assemply Lab
Assemply Lab
Assemply Lab
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
binary equivalent
7
DX DH DL Data
SP Stack Pointer
BP Base Pointer
Pointer and
Index registers
SI Source Index
DI Destination Index
8
Flag 15 0
register OF DF IF TF SF ZF AF PF CF
9
Memory
• ٌرمز للذاكرة باسم الموقع بٌن أقواس مربعة ] [ مثال[BX] :
مالحظات: •
عند ادخال 4 bit Hexإلى الذاكرة ٌتم تقسٌمها إلى قسمٌن؛ أي انها تشبه النوع •
األول من الرجسترات )(AX,BX,CX,DX
الجزء األول ٌأخذ 2bitالتً على الٌمٌن وٌرمز له كمثال[BX] : •
الجزء الثاني ٌأخذ 2bitالتً على الٌسار وٌرمز له بموقع الذاكرة 1+كمثال: •
][BX+1
14
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:
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)
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.
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
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:
Example:
26
Flag Registers:
• Status flags:
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
40000 0250
30000 7000 0000
20000
10000 Seg offs
00000
30
Exercise
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
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
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:
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
• Example 2
57
Segment register CS DS ES SS
Offset register IP SI,DI,BX SI,DI,BX SP,BP
58
Exercise