Transcript Document

ECE 424 Design of Microprocessor-Based Systems
Introduction to Assembly Language
Programming
Haibo Wang
ECE Department
Southern Illinois University
Carbondale, IL 62901
7-1
Overview of Assembly Language
 Advantages:
 Faster as compared to programs written using high-level languages
 Efficient memory usage
 Control down to bit level
 Disadvantages:
 Need to know detail hardware implementation
 Not portable
 Slow to development and difficult to debug
 Basic components in assembly Language:
Instruction, Directive, Label, and Comment
7-2
Example of Assembly Language Program
;NUMOFF.ASM: Turn NUM-LOCK indicator off.
Comments
.MODEL SMALL
.STACK
Assembly directive
.CODE
.STARTUP
MOV
AX,40H
;set AX to 0040H
D1: MOV
DS,AX
;load data segment with 0040H
MOV
SI,17H
AND
BYTE PTR [SI],0DFH ;clear NUM-LOCK bit
Instructions
;load SI with 0017H
.EXIT
END
Assembly directive
Label
7-3
Instruction Format
 General Format of Instructions
Label: Opcode
Operands
; Comment
 Label: It is optional. It provides a symbolic address that can be used in branch instructions
 Opcode: It specifies the type of instructions
 Operands: Instructions of 80x86 family can have one, two, or zero operand
 Comments: Only for programmers’ reference
 Machine Code Format
Opcode
MOV AL, BL
Mode
Operand1 Operand2
1000100011000011
MOV
Register
mode
7-4
Assembler Directives
 List File
 Source File
DATA SEGMENT PARA 'DATA‘
ORG
ORG
7000H
POINTS DB
SUM
DB
16 DUP(?)
?
DATA ENDS
CODE
DATA SEGMENT PARA 'DATA’
0000
SEGMENT PARA 'CODE‘
7000 0010 [00]
POINTS DB
7010
SUM
00
TOTAL:
8000H
MOV
AX,7000H
MOV
DS,AX
MOV
AL,0
•••••••••
CODE ENDS
DB
16 DUP(?)
?
7011
DATA ENDS
0000
CODE
ASSUME CS:CODE, DS:DATA
ORG
7000H
SEGMENT PARA 'CODE'
ASSUME CS:CODE, DS:DATA
ORG
8000 B8 7000 TOTAL: MOV
8003 8E D8 MOV
DS,AX
8005 B0 00 MOV
AL,0
8000H
AX,7000H
•••••••••
END TOTAL
7-5
Assembler Directives
 SEGMENT directive
 ENDS directive
 END directive
 ORG directive
 DB: Define Byte; DW, ….
 ASSUME directive
— Specifies the segment register (segment Register) that will be used to calculate the effective
addresses for all labels and variables defined under a given segment or group name (segment Nam
If CS = 1230H and DS = 5678H, what are the physical memory addresses of
label TOTAL and variable SUM?
7-6
Assembler Directives
 Simplified Segment Directives
 Predefined .Mode Types
.MODEL SMALL
DATA
SEGMENT
CODE
SEGMENT
TINY
one
one
SMALL
one
one
MEDIUM
one
multiple
COMPACT
multiple
one
.DATA
ORG
POINTS DB
SUM
DB
7000H
16 DUP(?)
?
.CODE
TOTAL:
ORG
8000H
LARGE
multiple
multiple
MOV
AX,7000H
HUGE
multiple
multiple
MOV
DS,AX
FLAT*
one
one
MOV
AL,0
•••••••••
* Flat is used for 32-bit addressing
RET
END
TOTAL
7-7
Build Executable Programs
library
Source files
Linker
Assembler
Syntax check
Translate source
files into
OBJ
files
machine code
OBJ
files
Executable
files
Question: What is the difference between *.com and *.exe files?
http://www.faqs.org/faqs/msdos-programmer-faq/part2/section-9.html
 Assemblers
 Microsoft ML, LINK, & DEBUG
 8086 Emulator
 A86
 MASM32 package
 •••••••
7-8
Microsoft MASM and Debug
 Microsoft MASM and Link Programs
ML /c /Fl numoff.asm
Syntax check;
Translate assembly instructions into machine codes
Link numoff
Build the executable file
 Microsoft Debug Program
C:\> debug
-a
0BDF:0100 MOV AX, 40
0BDF:0103
-t
AX = 0040 BX = 0000 CX = 0000 DX = 0000 SP = …………….
…………………………………………..
-q
7-9
8086/8088 Assembler and Emulator
7-10
Difference Between EXE and Binary Files
 Source File
.model small
.data
org 0010H
Var1 DB 12H
.code
MOV AX, 0000
MOV DS, AX
label1: MOV AL, DS:[0100H]
JMP label1
end
 List File
0000
0200 12
0000
0000 B8 0000
0003 8E D8
0005 A0 0100
0008 EB FB
.model small
.data
org 0200H
Var1 DB 12H
.code
MOV AX, 0000
MOV DS, AX
label1: MOV AL, DS:[0100H]
JMP label1
end
7-11
Difference Between EXE and Binary Files
 EXE file
 Binary file
7-12