Assembly Language - Southeast Missouri State University

Download Report

Transcript Assembly Language - Southeast Missouri State University

Assembly Language
• Advantages
• 1. It reveals the secret of your computer’s
hardware and software.
• 2. Speed.
• 3. Some special applications and occasions.
• Disadvantages
• 1. Not as easy to do the programming.
• 2. Debug is not as easy compared with highlevel language.
• 3. Not transferable between different CPUs.
Registers
• Intel 16-bits registers
AX
BX
AH
BH
AL
BL
Index Registers
BP
SP
SI
CX
CH
CL
DI
DX
DH
DL
Date Registers
CS
SS
Flag
IP
Status and Control Registers
DS
ES
Segment Registers
Registers
• Intel 32-bits registers
EAX
EBP
AX
ESP
BX
EBX
Index Registers
ESI
CX
ECX
EDI
EDX
DX
Date Registers
CS
SS
EFLAGS
EIP
FLAGS
IP
Status and Control Registers
DS
FS
ES
GS
Segment Registers
Flag Register
• 16-bits Flag Register
x x x x O D I T S Z x A x P x C
• The most common used flag bits
O D I
S
O—Overflow
D--- Direction
I-----Interrupt
S----Sign
Z A P C
Z---Zero
A---Auxiliary Carry
P----Parity
C----Carry
General Rules about Flags
•
•
•
•
•
•
“1”---Set the flag.
“0”---Clear the flag.
“?”---May change the flag to an
undetermined value
(Blank)---The flag is not changed
* ----Change the flag to specific rules
associated with the flag
Examples
ADD --- Instruction
O
*
D I
S
*
Z
*
A
*
P
*
C
*
HLT---Stop the CPU until a hardware interrupt occurs.
O D I S Z A P
C
IDIV---Signed Integer Division
?
?
?
?
?
?
The Hello World Program
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
title Hello World Program
(hello.asm)
; This program displays "Hello, world!"
.model small
.stack 100h
.data
message db "Hello, world!",0dh,0ah,'$'
.code
main proc
mov ax,@data
mov ds,ax
mov ah,9
mov dx,offset message
int 21h
mov ax,4C00h
int 21h
main endp
end main
Arithmetic Instructions
•
•
•
•
•
•
•
•
ADD and SUB instructions
ADD reg, reg
ADD mem, reg
ADD reg, mem
ADD reg, immed
ADD mem, immed
ADD accum, immed
Flag O D I S Z A P C
*
*
*
*
*
*
• SUB has the similar instruction format
MS -Debug
• Debug is a MS-DOS command which can
be used to enter machine language into the
computer either to create programs or test
computer peripherals by receiving back
various computer machine code.
ABOUT DEBUG
• Debug is a method of looking at portions of
your computer and writing assembly code
to perform certain tasks on your computer.
• MS-DOS 2.x - 4.x uses debug.com
MS-DOS 5.x and above uses debug.exe
A History of MS-DEBUG
• In 1980, Tim Paterson began working on a
16-bit OS for the 8086 S-100 Bus card he
had designed for SCP (Seattle Computer
Products) the previous year. To help in
getting QDOS (later called 86-DOS) to
work correctly, Tim created a debugger in a
ROM chip; the code for that ROM version
was released into the Public Domain. Later,
Tim adapted the code to run as a .COM
program under QDOS, and also added the
ability to disassemble 8086 machine code.
• In the meantime, Microsoft® had been
busy purchasing the rights to sell Tim's
QDOS to IBM® for their 'secret' PC project.
Tim was hired by Microsoft as the primary
author of their first OS. When he completed
his work on IBM's Personal Computer™
DOS 1.00 in 1981, his DEBUG.COM utility
was included with it. All the functionality
that Tim put into DEBUG is still there and
little has been added to it (the major
exception being the Assemble command;
added under DOS 2.0).
Debug Availability
• AVAILABILITYThe debug command is an
external command and is available in the below
Microsoft Operating Systems.
• All Versions of MS-DOS
Windows 95
Windows 98
Windows ME
Windows NT
Windows 2000
Windows XP
Debug Commands
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
? assemble A [address]
Compare C range address
dump
D [range]
enter
E address [list]
fill
F range list
go
G [=address] [addresses]
hex
H value1 value2 (Learn 2's Complement!)
input
I port
load
L [address] [drive] [firstsector] [number]
move
M range address
name
N [pathname] [arglist]
output
O port byte
Proceed
P [=address] [number]
quit
Q . . . . . . . . . (Learn this first!)
Register
R [register]
search
S range list
trace
T [=address] [number]
unassemble U [range]
write
W [address] [drive] [firstsector] [number]
Debug
•
•
•
•
•
•
•
•
•
•
-F
-D
-E
-T----Make sure you check the IP to see if its
counts is pointing the right location.
-R IP to change it
-A 100
Mov dl, [0200]
Mov [0201], dl
Mov ax, 4c00(h)
Int 21
Example
•
•
•
•
•
Under Windows XP:
1. Click run and type cmd
2. type debug
3. type D40:0 and you will get a display
screen with the first line looks as:
0040:0000 F8 03 F8 02 E8 03 E8 02 - 78 03 78 02 BC 03
The meaning of the line
• F8 03 F8 02 E8 03 E8 02 78 03 7802 BC03
• COM1 COM2 COM3 COM4 LPT1 LPT2 LPT3
The above graph shows you what the feed back means as far as
port, so if you see F8 03, this would be an indication that COM1
is being detected; if you see 00 00, this is an indication that it is
not being detected, which could mean that hardware is bad or that
it is disabled in CMOS.
Some Debug Routine Applications
• TESTING PORTS
CLEAR CMOS
PRINTER TEST
JOYSTICK TEST
ERASE SECTOR 2
ERASE ALL HDD INFORMATION
ERASE HDD AND CLEAR CMOS
DISCHARGE OLD LAPTOP BATTERIES
REBOOT YOUR COMPUTER
CREATE A SLEEP FILE FOR DOS
CHECK BIOS DATE
VIDEO CARD TYPE
CHECK BIOS DATE
• The below debug routine is to check the date of your BIOS.
All BIOS dates on PC compatible computers is stored at
memory address FFFF5h. To display the date of your
BIOS do the following:
• At the C:\> type debug
• - d FFFF:5 L 8
• After typing the above command you should receive a
string similar to:
• FFFF:0000
30 34 2F-33 30 2F 39 38
4/30/98
• The 4/30/98 would be the date of your computer BIOS.
Check Your Computer’s Video
• -d c000:0040
Debug
•
•
•
•
•
•
•
•
Mov si, 0200
Mov ax, [si]
Mov [0270], ax
Mov ax, 4c00
Int 21
-T
-T
-T
Assembling, linking, And Debugging
• Debug and Assembler
Assembler
Source File
Link Library
Linker
Object File
Executable
Program
Listing File
Map File
Loader
Output
Start Stop
Length Name
00000H 004D0H 004D1H _TEXT
004D2H 00665H 00194H _DATA
00670H 0076FH 00100H STACK
Origin
004D:0
Class
CODE
DATA
STACK
Group
DGROUP
Program entry point at 0000:0000
A typical Map File (From Hello.asm)
The main feature for the map file is that it list all the
information about each program segments.
Object File (From Hello.asm)
•
•
•
•
€ C:\IRVINE\CH01\HELLO.asm!
ˆ ¡ CV7–K STACK _DATA DGROUP
_TEXT $$TYPES $$SYMBOLS DEBTYP
DEBSYMSTACKDATACODEø˜ H
•
ö˜ H
ú˜ t ß™ !†
¦™! šÿÿY
Hello
, world!$† ¸ ŽØ´
º Í!¸
LÍ!Õœ È U ÄX” •
•
•
•
•
¨
•
•
•
•
•
•
•
òñ, Š HELLO.obj6
/Microsoft (R) Macro Assembler
Version 6.13.7299
message
main œ ÌRÌvT ŽŠ
ÁP ^
Target Processor Directives
•
•
•
•
•
•
•
•
•
.8086----8086, 8088
.186--.286--.386
.486
.586
.287
.387
When using .386, the program can only run
on 386 and above processors.
Floating-Point Unit
•
80-bit Registers
48-bit Pointer Register
ST(0)
FPU Instruction Pointer
ST(1)
FPU Data Pointer
Tag Register
Control Register
Status Register
16-bit Control Register
ST(7)
Opcode Register
Debug and Debugger
• MASM supplies a good 16-bit debugger named
Code View.
• TASM supplies one named Turbo Debugger.
• For 32-bit Windows Console programs, the
preferred debugger is Microsoft Visual Studio
(msdev.exe), part of Microsoft Visual C++.