Transcript mov ax

MASM Instructions
1
MASM 8086 Instruction - Basic Structure
Label
Operator
Operand[s]
;Comment
Label - optional alphanumeric string
1st character must be a-z,A-Z,?,@,_,$
Last character must be :
Operator - assembly language instruction
mnemonic: an instruction format for humans
Assembler translates mnemonic into hexadecimal opcode
example: mov is f8h
Operand[s] - 0 to 3 pieces of data required by instruction
Can be several different forms
Delineated by commas
immediate, register name, memory data, memory address
Comment - Extremely useful in assembler language
These fields are separated by White Space (tab, blank, \n, etc.)
Microprocessor
Computer Architectures Lab
MASM Instructions
2
8086 Instruction - Example
Label
Operator
INIT: mov
Label
Operator
Operands
Comment
ax, bx
-
Operand[s]
;Comment
; Copy contents of bx into ax
INIT:
mov
ax and bx
alphanumeric string between ; and \n
• Not case sensitive
• Unlike other assemblers, destination operand is first
• mov is the mnemonic that the assembler translates into an
opcode
Microprocessor
Computer Architectures Lab
MASM Instructions
3
Anatomy of MASM Source File
• Assembler program divided into segments
• Segments defined in 1 or more modules
– contain instructions, data, assembler directives
• Each module is a separate file
– Assembler translates modules to object files
• Linker does several things
–
–
–
–
Combines multiple object files
Resolves relative addresses
Inserts loader code
Creates executable
Microprocessor
Computer Architectures Lab
MASM Instructions
4
Assembler Language Segment Types
• Stack
– For dynamic data storage
– Source file defines size
– Must have exactly 1
• Data
–
–
–
–
For static data Storage
Source file defines size
Source file defines content (optional)
Can have 0 or more
• Code
– For machine Instructions
– Must have 1 or more
Microprocessor
Computer Architectures Lab
MASM Instructions
5
Using MASM Assembler
• to get help:
C:\> masm /h
• Can just invoke MASM with no arguments:
C:\> masm
Source Filename
Object Filename
Source Listing
Cross Reference
[.ASM]:
[HELLO.OBJ]:
[NUL.LST]:
[NUL.CRF]:
hello
• .ASM - Assembler source file prepared by programmer
• .OBJ - Translated source file by assembler
• .LST - Listing file, documents “Translation” process
» Errors, Addresses, Symbols, etc
• .CRF – Cross reference file
Microprocessor
Computer Architectures Lab
MASM Instructions
6
Using MASM Assembler/Linker (Cont.)
• Another way to invoke assembler:
C:\> masm hello,,hello,hello
• This causes MASM to create:
HELLO.OBJ
HELLO.LST
HELLO.CRF
• Yet another way:
C:\> masm hello
• This causes MASM to create:
HELLO.OBJ
• Next step is to create executable file using the linker:
C:\> link hello
• This causes linker to create:
HELLO.EXE
Microprocessor
Computer Architectures Lab
MASM Instructions
7
MASM Assembler Language
• Each module contains 4 types of statements:
1.
2.
3.
4.
Executable instructions
MASM assembler directives
MASM macroinstruction definitions
MASM macroinstruction calls
Executable Instr.: Instructions that the x86 can fetch from
memory and execute
MASM Dir.: Programmer supplied directives that guide the
“translation” process
MASM Macro Defs. and Calls:
Microprocessor
Computer Architectures Lab
MASM Instructions
8
x86 Instruction Type Classifications
• DATA TRANSFER
– General
– Strings
– Special Purpose
mov
ax, [DAT1] ;ax gets contents of mem
cmpsb
xchg
;if DS:SI=ES:DI then ZF=1
ax, bx
;ax gets bx and bx gets ax
• ARITHMETIC/LOGIC
–
–
–
–
–
Integer
ASCII, BCD
Floating Point
Logical
Shifting
add
ax, bx
aaa
;ax gets ax+bx
;changes ASCII # to int.
fadd
DAT
;ST get ST+DAT
and
ax, bx
;ax gets ax AND bx
ror
ax, 2
;ax contents shifted-2 right
jnz
LABEL1
;if ZF=1 then IP=LABEL1
int
21h
;invoke INT handler 21h
call
SUB1
• CONTROL TRANSFER
–
–
–
–
–
–
Branching
Interrupt
Subroutine
Modify Flag
Halt Processor
No Operation
Microprocessor
;invoke subroutine, SUB1
cli
;IF gets zero
hlt
;need RESET to run again
nop
;
Computer Architectures Lab
MASM Instructions
9
x86 Instruction Set Summary
(Not Included in Following Slides)
• Floating Point
8087
• Special Protected Mode
80386
• MMX (DSP)
Pentium MMX
• SSE (Streaming SIMD Extension)
- Special SIMD
Pentium III
• Others (few specialized added with each generation)
Microprocessor
Computer Architectures Lab
MASM Instructions
10
x86 Instruction Set Summary
(Data Transfer)
CBW
CWD
IN
LAHF
LDS
LEA
LES
LODS
MOV
MOVS
OUT
POP
POPF
PUSH
PUSHF
SAHF
SCAS
STOS
XCHG
XLAT
Microprocessor
;Convert Byte to Word AL  AX
;Convert Word to Double in AX DX,AX
;Input
;Load AH from Flags
;Load pointer to DS
;Load EA to register
;Load pointer to ES
;Load memory at SI into AX
;Move
;Move memory at SI to DI
;Output
;Pop
;Pop Flags
;Push
;Push Flags
;Store AH into Flags
;Scan memory at DI compared to AX
;Store AX into memory at DI
;Exchange
;Translate byte to AL
Computer Architectures Lab
MASM Instructions
11
x86 Instruction Set Summary
(Arithmetic/Logical)
AAA
AAD
AAM
AAS
ADC
ADD
AND
CMC
CMP
CMPS
CWD
DAA
DAS
DEC
DIV
IDIV
MUL
IMUL
INC
Microprocessor
;ASCII Adjust for Add in AX
;ASCII Adjust for Divide in AX
;ASCII Adjust for Multiply in AX
;ASCII Adjust for Subtract in AX
;Add with Carry
;Add
;Logical AND
;Complement Carry
;Compare
;Compare memory at SI and DI
;Convert Word to Double in AX  DX,AX
;Decimal Adjust for Add in AX
;Decimal Adjust for Subtract in AX
;Decrement
;Divide (unsigned) in AX(,DX)
;Divide (signed) in AX(,DX)
;Multiply (unsigned) in AX(,DX)
;Multiply (signed) in AX(,DX)
;Increment
Computer Architectures Lab
MASM Instructions
12
x86 Instruction Set Summary
(Arithmetic/Logical Cont.)
NEG
NOT
OR
RCL
RCR
ROL
ROR
SAR
SBB
SCAS
SHL/SAL
SHR
SUB
TEST
XLAT
XOR
Microprocessor
;Negate
;Logical NOT
;Logical inclusive OR
;Rotate through Carry Left
;Rotate through Carry Right
;Rotate Left
;Rotate Right
;Shift Arithmetic Right
;Subtract with Borrow
;Scan memory at DI compared to AX
;Shift logical/Arithmetic Left
;Shift logical Right
;Subtract
;AND function to flags
;Translate byte to AL
;Logical Exclusive OR
Computer Architectures Lab
MASM Instructions
13
x86 Instruction Set Summary
(Control/Branch Cont.)
CALL
CLC
CLD
CLI
ESC
HLT
INT
INTO
IRET
JB/JNAE
JBE/JNA
JCXZ
JE/JZ
JL/JNGE
JLE/JNG
JMP
JNB/JAE
JNBE/JA
JNE/JNZ
JNL/JGE
Microprocessor
;Call
;Clear Carry
;Clear Direction
;Clear Interrupt
;Escape (to external device)
;Halt
;Interrupt
;Interrupt on Overflow
;Interrupt Return
;Jump on Below/Not Above or Equal
;Jump on Below or Equal/Not Above
;Jump on CX Zero
;Jump on Equal/Zero
;Jump on Less/Not Greater or Equal
;Jump on Less or Equal/Not Greater
;Unconditional Jump
;Jump on Not Below/Above or Equal
;Jump on Not Below or Equal/Above
;Jump on Not Equal/Not Zero
;Jump on Not Less/Greater or Equal
Computer Architectures Lab
MASM Instructions
14
x86 Instruction Set Summary
(Control/Branch)
JNLE/JG
JNO
JNP/JPO
JNS
JO
JP/JPE
JS
LOCK
LOOP
LOOPNZ/LOOPNE
LOOPZ/LOOPE
NOP
REP/REPNE/REPNZ
REPE/REPZ
RET
SEG
STC
STD
STI
TEST
WAIT
Microprocessor
;Jump on Not Less or Equal/Greater
;Jump on Not Overflow
;Jump on Not Parity/Parity Odd
;Jump on Not Sign
;Jump on Overflow
;Jump on Parity/Parity Even
;Jump on Sign
;Bus Lock prefix
;Loop CX times
;Loop while Not Zero/Not Equal
;Loop while Zero/Equal
;No Operation (= XCHG AX,AX)
;Repeat/Repeat Not Equal/Not Zero
;Repeat Equal/Zero
;Return from call
;Segment register
;Set Carry
;Set Direction
;Set Interrupt
;AND function to flags
;Wait
Computer Architectures Lab
MASM Instructions
15
MASM Program Example
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;
This is an example program. It prints the
;
;
character string "Hello World" to the DOS standard output
;
;
using the DOS service interrupt, function 9.
;
;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
hellostk SEGMENT BYTE STACK 'STACK'
;Define the stack segment
DB 100h DUP(?)
;Set maximum stack size to 256 bytes (100h)
hellostk ENDS
hellodat
dos_print
strng
hellodat
SEGMENT BYTE 'DATA' ;Define the data segment
EQU 9
;define a constant via EQU
DB 'Hello World',13,10,'$' ;Define the character string
ENDS
hellocod
START:
SEGMENT BYTE 'CODE' ;Define
mov ax, SEG hellodat
mov ds, ax
mov ah, dos_print
mov dx,OFFSET strng
int 21h
mov ax, 4c00h
int 21h
ENDS
END
START
hellocod
Microprocessor
the Code segment
;ax <-- data segment start address
;ds <-- initialize data segment register
;ah <-- 9 DOS 21h string function
;dx <-- beginning of string
;DOS service interrupt
;ax <-- 4c DOS 21h program halt function
;DOS service interrupt
; ‘END label’ defines program entry
Computer Architectures Lab
MASM Instructions
16
Another Way to define Segments
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
Use 'assume' directive to define segment types
;
;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
hellostk SEGMENT
;Define a segment
DB 100h DUP(?)
hellostk ENDS
hellodat
dos_print
strng
hellodat
SEGMENT
;define a segment
EQU 9
;define a constant
DB 'Hello World',13,10,'$' ;Define the character string
ENDS
hellocod
SEGMENT
;define a segment
assume cs:hellocod, ds:hellodat, ss: hellostk
mov ax, hellodat
;ax <-- data segment start address
mov ds, ax
;ds <-- initialize data segment register
mov ah, dos_print
;ah <-- 9 DOS 21h string function
mov dx,OFFSET strng
;dx <-- beginning of string
int 21h
;DOS service interrupt
mov ax, 4c00h
;ax <-- 4c DOS 21h program halt function
int 21h
;DOS service interrupt
ENDS
START
START:
hellocod
END
Microprocessor
Computer Architectures Lab
MASM Instructions
17
Yet another way to define Segs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
Use .stack,.data,.code directives to define segment types
;
;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.stack 100h
; reserve 256 bytes of stack space
.data
dos_print EQU 9
strng
DB 'Hello World',13,10,'$'
;define a constant
;Define the character string
.code
START:
mov
mov
mov
mov
int
mov
int
ax, SEG strng
ds, ax
ah, dos_print
dx,OFFSET strng
21h
ax, 4c00h
21h
END
START
Microprocessor
;ax <-- data segment start address
;ds <-- initialize data segment register
;ah <-- 9 DOS 21h string function
;dx <-- beginning of string
;DOS service interrupt
;ax <-- 4c DOS 21h program halt function
;DOS service interrupt
Computer Architectures Lab
MASM Instructions
18
Masm Assembler Directives
end label
end of program, label is entry point
proc far|near
begin a procedure; far, near keywords
specify if procedure in different code
segment (far), or same code segment (near)
endp
end of procedure
page
set a page format for the listing file
title
title of the listing file
.code
mark start of code segment
.data
mark start of data segment
.stack
set size of stack segment
Microprocessor
Computer Architectures Lab
MASM Instructions
19
Data Allocation Directives
db
define byte
dw
define word (2 bytes)
dd
define double word (4 bytes)
dq
define quadword (8 bytes)
dt
define tenbytes
equ
equate, assign numeric expression to a name
Examples:
db 100 dup (?)
define 100 bytes, with no initial values for bytes
db “Hello”
define 5 bytes, ASCII equivalent of “Hello”.
maxint equ
32767
count
10 * 20
equ
Microprocessor
; calculate a value (200)
Computer Architectures Lab