Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 34

© DHBK 2005 1/Chapter3

Course contents
1. Introduction to microprocessor systems
2. The Intel 8088/8086 microprocessors
3. Assembly programming for 8086
4. Memory and I/O Interfacing
5. Interrupt
6. Didect memory access (DMA)
7. Real life microprocessors
© DHBK 2005 2/Chapter3

Chapter 3 Assembly programming for 8086


3.1 Structure of an assembly program
3.2 Assembly programming for IBM PC
3.3 Basic program structrures in 8086 assembly
3.4 Programming examples
© DHBK 2005 3/Chapter3

Chapter 3 Assembly programming for 8086


3.1 Structure of assembly programs
3.1.1 Syntax
3.1.2 Data declaration
3.1.3 Variable and contant declaration
3.1.4 Structure of an assembly program
3.2 Assembly programming for IBM PC
3.3 Basic program structrures in 8086 assembly
3.4 Programming examples
© DHBK 2005 4/Chapter3

Chapter 3 Assembly programming for 8086


3.1 Structure of assembly programs
3.1.1 Syntax
3.1.2 Data declaration
3.1.3 Variable and contant declaration
3.1.4 Structure of an assembly program
3.2 Assembly programming for IBM PC
3.3 Basic program structrures in 8086 assembly
3.4 Programming examples
© DHBK 2005 5/Chapter3

3.1.1 Syntax
1. .Model Small
Memory model declaration
2. .Stack 100
Stack declaration
3. .Data
4.
5.
Tbao DB ‘Chuoi da sap xep:’, 10, 13
MGB DB ‘a’, ‘Y’, ‘G’, ‘T’, ‘y’, ‘Z’, ‘U’, ‘B’, ‘D’, ‘E’, Data segment declaration
6. DB ‘$’
7. .Code

8. MAIN Proc
Code segment declaration
9. MOV AX, @Data ;khoi dau DS
10.
11.
MOV
MOV
DS, AX
BX, 10 ;BX: so phan tu cua mang Start of main program
12. LEA DX, MGB ;DX chi vao dau mang byte
13. DEC BX ;so vong so sanh phai lam
14. LAP: MOV SI, DX ; SI chi vao dau mang
15. MOV CX, BX ; CX so lan so cua vong so
16. MOV DI, SI ;gia su ptu dau la max

Comments start with;


17. MOV AL, [DI] ;AL chua phan tu max
18. TIMMAX:
19. INC SI ;chi vao phan tu ben canh
20. CMP [SI], AL ; phan tu moi > max?
21. JNG TIEP ;khong, tim max
22. MOV DI, SI ; dung, DI chi vao max
23. MOV AL, [DI] ;AL chua phan tu max
24. TIEP: LOOP TIMMAX ;tim max cua mot vong so
25. CALL DOICHO ;doi cho max voi so moi
26. DEC BX ;so vong so con lai
27. JNZ LAP ;lam tiep vong so moi
28. MOV AH, 9 ; hien thi chuoi da sap xep
29. LEA DX, Tbao
30. INT 21H
31. MOV AH, 4CH ;ve DOS
32. INT 21H
33. MAIN Endp
34.
35.
DOICHO Proc
PUSH AX End of main program
36. MOV AL, [SI]
37. XCHG AL, [DI]
38.
39.
MOV
POP
[SI], AL
AX Start of subprogams
40. RET
41.
42.
DOICHO Endp
END MAIN End of code segment
© DHBK 2005 6/Chapter3

3.1.1 Syntax
• Name Field Opcode Operands ; comment
• Assembler is not case sensitive
• Name field:
 Contains label, variable, subprogram name
 Length: 1 to 31 characters
 Name must not contain space nor start with a digit
 Can contain special characters: ? . @ _ $ %
 When used . must be placed first
 Labels must end with :
 Examples:
TWO_WORD
?1
two-word
.@?
1word
Let’s_go
© DHBK 2005 7/Chapter3

Chapter 3 Assembly programming for 8086


3.1 Structure of assembly programs
3.1.1 Syntax
3.1.2 Data declaration
3.1.3 Variable and contant declaration
3.1.4 Structure of an assembly program
3.2 Assembly programming for IBM PC
3.3 Basic program structrures in 8086 assembly
3.4 Programming examples
© DHBK 2005 8/Chapter3

3.1.2 Data declaration


• Data:
 Binary number: 0011B
 Decimal number: 1234
 Hexadecimal number: 1EF1H, 0ABBAH
 Character, string: ‘A’, “abcd”
© DHBK 2005 9/Chapter3

Chapter 3 Assembly programming for 8086


3.1 Structure of assembly programs
3.1.1 Syntax
3.1.2 Data declaration
3.1.3 Variable and contant declaration
3.1.4 Structure of an assembly program
3.2 Assembly programming for IBM PC
3.3 Basic program structrures in 8086 assembly
3.4 Programming examples
© DHBK 2005 10/Chapter3

3.1.3 Variable and contant declaration


• DB (Define Byte): Define one byte variables
• DW (Define Word): Define 2 byte variables
• DD (Define Double word): Define 4 byte variables

• One byte variable:


 Name DB initial value
 Examples:
 B1 DB 4
MOV AL, B1
 B1 DB ?
 C1 DB ‘$’ LEA BX, B1
 C1 DB 34 MOV AL, [BX]
© DHBK 2005 11/Chapter3

3.1.3 Variable and contant declaration


• Word variable:
 Name DW initial value 1300A
 Examples: 13009
13008 9
 W1 DW 4
13007 8
 W2 DW ? 13006 7
• Array declaration: 13005 6
13004 5
 M1 DB 4, 5, 6, 7, 8, 9
13003 4 M1
13002
 M2 DB 100 DUP(0) 13001
 M3 DB 100 DUP(?) 13000

 M4 DB 4, 3, 2, 2 DUP (1, 2 DUP(5), 6)

 M4 DB 4, 3, 2, 1, 5, 5, 6, 1, 5, 5, 6
© DHBK 2005 12/Chapter3

3.1.3 Variable and contant declaration


• Two dimentional array declaration:
1300A
1 6 3
13009
4 2 5 13008 5
13007 2
13006 4
13005 3
 M1 DB 1, 6, 3 13004 6
DB 4, 2, 5 13003 1 M1
13002
13001
13000
 M2 DB 1, 4
MOV AL, M1 ; copy 1 vao AL
DB 6, 2
MOV AH, M1[2]
DB 3, 5 MOV BX, 1
MOV SI, 1
MOV CL, M1[BX+SI]
MOV AX, Word Ptr M1[BX+SI+2]
MOV DL, M1[BX][SI]
© DHBK 2005 13/Chapter3

3.1.3 Variable and contant declaration


• String declaration
 STR1 DB ‘string’
 STR2 DB 73h, 74h, 72h, 69h, 6Eh, 67h
 STR3 DB 73h, 74h, ‘r’, ‘i’, 6Eh, 67h

• Constant declaration
 Constants can be declared within the body of a program
 Often declared in the data section
 Examples:
 CR EQU 0Dh ; carriage return
 LF EQU 0Ah ; line feed
 CHAO EQU ‘CR Hello’

 MSG DB CHAO, ‘$’


© DHBK 2005 14/Chapter3

Chapter 3 Assembly programming for 8086


3.1 Structure of assembly programs
3.1.1 Syntax
3.1.2 Data declaration
3.1.3 Variable and contant declaration
3.1.4 Structure of an assembly program
3.2 Assembly programming for IBM PC
3.3 Basic program structrures in 8086 assembly
3.4 Programming examples
© DHBK 2005 15/Chapter3

3.1.4 Structure of an assembly program


• Memory model declaration
 .MODEL memory model
 Example: .Model Small

Models Description

Tiny (hẹp) Data and code share one segment

Small (nhỏ) One data segement, one code segment

Medium 1 data segment, several code segments


(trung bình)
Compact (gọn) 1 code segment, several data segments

Large (lớn) several code segments, several data segments, maximum arrray size is 64
KB
Huge (đồ sộ) several code segments, several data segments, arrrays can be more than 64
KB
© DHBK 2005 16/Chapter3

3.1.4 Structure of an assembly program


• Stack segment declaration
 .Stack size ( in bytes)
 E.g.:
 .Stack 100 ; declare 100 byte stack segment
 Default value: 1 KB

• Data segment declaration:


 .Data
 Variables and constants declaration

• Code segment declaration:


 .Code
© DHBK 2005 17/Chapter3

3.1.4 Structure of an assembly program


• Template for assembly programs to be compiled to .EXE file

.Model Small
.Stack 100
.Data
;constants and variables declaration
.Code
MAIN Proc
;Initialize for DS
MOV AX, @data
MOV DS, AX
;Insert your code here

;return to DOS using function 4CH of INT 21H


MOV AH, 4CH
INT 21H
MAIN Endp
;Subprogram
END MAIN
© DHBK 2005 18/Chapter3

3.1.4 Structure of an assembly program


• Example Hello.EXE
.Model Small
.Stack 100
.Data
CRLF DB 13,10,’$’
MSG DB ‘Hello! $’
.Code
MAIN Proc
;Initialize for DS
MOV AX, @data
MOV DS, AX
;Carriage return and line feed using function 9 of INT 21H
MOV AH,9
LEA DX, CRLF
INT 21H
;Display MSG using function 9 of INT 21H
MOV AH,9
LEA DX, MSG
INT 21H
; return to DOS using function 4CH of INT 21H
MOV AH, 4CH
INT 21H
MAIN Endp
END MAIN
© DHBK 2005 19/Chapter3

3.1.4 Structure of an assembly program


• Template for assembly programs to be compiled to .COM files
 One segment for Code, Data, Stack
 Return to DOS using INT 20H
.Model Tiny
.Code
ORG 100h
START: JMP CONTINUE
;constants and variables declaration
CONTINUE:
MAIN Proc
;insert your code here
INT 20H ;return to DOS
MAIN Endp
;subprogram if any
END START
© DHBK 2005 20/Chapter3

3.1.4 Structure of an assembly program

FFFFH
SP
Stack expansion

Code and data expansion

CONTINUE:

Data
0100H JMP CONTINUE IP
Program segment prefix
0000H
© DHBK 2005 21/Chapter3

3.1.4 Structure of an assembly program


• Example: Hello.COM .Model Tiny
.Code
ORG 100H
START: JMP CONTINUE
CRLF DB 13,10,’$’
MSG DB ‘Hello! $’
CONTINUE:
MAIN Proc
;Carriage return and line feed using function 9 of INT 21H
MOV AH,9
LEA DX, CRLF
INT 21H
;Display MSG using function 9 of INT 21H
MOV AH,9
LEA DX, MSG
INT 21H
;return to DOS
INT 20H
MAIN Endp
END START
© DHBK 2005 22/Chapter3

3.1.4 Structure of an assembly program

Stack

SS Program
Program CS
DS
100h ES 100h
PSP PSP

.COM . EXE
© DHBK 2005 23/Chapter3

Chapter 3 Assembly programming for 8086


3.1 Structure of an assembly program
3.2 Assembly programming for IBM PC
3.3 Basic program structrures in 8086 assembly
3.4 Programming examples
© DHBK 2005 24/Chapter3

3.2 Assembly programming for IBM PC

Using a text editor to enter assembly source file


*.asm

Using MASM to assemble the source file to


object code *.obj

Using LINK to link object files to exe file


*.exe

Run the program


© DHBK 2005 25/Chapter3

Chapter 3 Assembly programming for 8086


3.1 Structure of an assembly program
3.2 Assembly programming for IBM PC
3.3 Basic program structrures in 8086 assembly
3.4 Programming examples
© DHBK 2005 26/Chapter3

3.3.1 If-then structure


• If condition then task
• Example:

; If AX<0
CMP AX, 0 ; AX<0 ?
JNL End_if ; NO, exist
; then
NEG AX ; YES, AX=-AX
End_if: MOV BX, AX ;BX=AX
© DHBK 2005 27/Chapter3

3.3.2 If-then-else
• If condition then task 1 else task 2
• E.g: if AX<BX then CX=0 else CX=1

; if AX<BX
CMP AX, BX ; AX<BX ?
JL Then_ ; yes, CX=0
;else
MOV CX, 1 ; no, CX=1
JMP End_if
Then_: MOV CX, 0;
End_if:
© DHBK 2005 28/Chapter3

3.3.3 Case structure


• Case Expression
Value1: task 1
Value2: task 2
...
ValueN: task N
END CASE CMP AX, 0 ;
• Example: JL AM ; AX<0
JE Khong ; AX=0
if AX<0 then CX=-1 JG DUONG; AX>0
If AX=0 then CX=0 AM: MOV CX, -1
If AX>0 then CX=1 JMP End_case
Khong: MOV CX, 0
JMP End_case

DUONG: MOV CX, 1


End_case:
© DHBK 2005 29/Chapter3

3.3.4 For-Do
• For Nr_of_iteration Do task

Initialize the counter Example: Display one line of $ on the screen

task
MOV CX, 80 ;number of loop
MOV AH,2 ;display one character
Decrease the counter MOV DL,’$’ ;DL stores the ascii code of
character to be displayed
HIEN: INT 21H
LOOP HIEN
N ;End_for
counter=0?

Y
© DHBK 2005 30/Chapter3

3.3.5 While-Do structure


• While condtion Do task
Example:
AX=0, BX=0
While AX<>10 then
BX=BX+1 và AX=AX+2
XOR AX, AX ;AX=0
XOR BX, BX ;BX=0
TIEP:
S CMP AX,10 ;so sánh AX với 10
Điều kiện JE End_while ;kết thúc nếu AX<>10
INC BX ;BX=BX+1
Đ ADD AX,2 ;AX=AX+2
JMP TIEP
End_while:
công việc
© DHBK 2005 31/Chapter3

3.3.6 Repeat-until structure


• Repeat task until condition

Example: enter a character from the keyboard until Enter


is typed
task

MOV AH,1 ;function to read one character


Continue: INT 21H ; read one character and
store ascii code of entered character in AL
condition
CMP AL, 13 ; Enter is pressed?
JNE Continue ; no, continue
Y End_:
N
© DHBK 2005 32/Chapter3

Chapter 3 Assembly programming for 8086


3.1 Structure of an assembly program
3.2 Assembly programming for IBM PC
3.3 Basic program structrures in 8086 assembly
3.4 Programming examples
© DHBK 2005 33/Chapter3

3.4.1 Input/output data


• Two methods:
 Using IN, OUT instructions
 Need to know the address of I/O port
 Different systems have different address
 Usning interupt service routines of DOS and BIOS
 Simple to use
 System independent
• Interrupt 21h of DOS:
 Function 1: read one character from the keyboard
 Input: AH=1
 Output: AL= ASCII code of the character, AL=0 when function keys are pressed
 Function 2: Display 1 character on the screen
 Input: AH=2
DL=ASCII code of character to be displayed
 Function 9: Display string ending with $ on the screen
 Input: AH=9
DX=address of the string to be displayed
 Function 4CH: Quit .exe program
 Input: AH=4CH
© DHBK 2005 34/Chapter3

3.4.2 Programming examples


• Example 1: Write a program which asks the user to enter one
lower case character and diplays the upper case format of the
entered character
 Sample display:
 Enter a lower case character: a
A
• Example 2: Write a progarm which asks the user to enter a
number in binary format (16 bit at most) and stores the entered
number in BX. Display the content of BX on the screen.

You might also like