Download Assembly Instructions

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Assembly Instructions
Assembly language instructions may involve mnemonics, labels,
variables, constants, and directives. Examples are as follows.
here1 mov5 ax,23
msg2 db6
"help$"
bytes assigned to msg
var3 db6
10
of 10
num4 equ6 20
;set ax value
;ASCII
;var
assigned
a
value
;num has the value 20
_____________________________________________________________
1 label
4 constant
2 variable of ASCII bytes
5 mnemonic
3 one-byte numeric variable
6 directive
1
Define Byte, Word
Define Byte Variables (memory locations)
Define Word Variables (memory locations)
2
Define Byte, Word
• list db 10,20,30,40
will store the four values in consecutive locations. If the
offset of list is 0000, they will be stored in 0000, 0001,
0002, 0003, respectively. list refers to 0000, list+1 refers
to 0001, etc.
• Value1 dw
2AB6h
will place two bytes in consecutive locations with the low
order byte in the lower memory location as: B6 2A
3
Character or String Constants
• ‘ABC’
• “This is a test.”
• ‘This is my file.’
• Define a variable called message stored
at offset 0000 for any of the above.
For example: message db ‘ABC’.
• Then “B” of “ABC” is at offset 0001 and
“i” of “This …” is at offset 2
4
Pointer
Given:
message db ‘ABC’
;define 3 bytes
P
db message ;P points to message
The variable P contains the offset of
message
5
ASCII Table
6
MOV Instruction
• Allowable MOVs with scratch registers
MOV
MOV
MOV
MOV
MOV
reg,reg
mem,reg
reg,mem
mem,immed
reg,immed
• Allowable MOVs with segment registers (except CS)
MOV
MOV
MOV
MOV
segreg,reg16
segreg,mem16
reg16,segreg
mem16,segreg
7
Direct Operands
8
Illegal Moves
9
Label Directive
.data
countB label byte ;byte attribute,
;no storage allocated
countw dw 0020h
;word attribute
.code
mov al,countB
mov cx,countW
;retrieve low byte of count
;retrieve all of count
countB
countW
20
00
Addressing Modes
Addressing Mode
Direct
Example
mov ax,bx
Description
Effective address (EA) are
the registers
ax,[bx]
EA is the contents of a register
ax,[bp + 1]
EA is the sum of a base register
and a displacement
[di + 2],ax
EA is the sum of an index
register and a displacement
ax,[bx + si]
EA is the sum of a base register
and an index register
ax,[bx + si + 2] EA is the sum of a base
register, an index register and a
displacement
Register Indirect
Based
mov
mov
Indexed
mov
Based Indexed
mov
Based Indexed with
Displacement
mov
11
Based Index Addressing Example
Adding Bytes
In the following example of based indexed addressing, the contents of
16d bytes beginning at ds:1000h and ending with ds:100Fh are
accumulated in the al register.
cs:100 mov al,0
cs:102 mov cx,10h
cs:105 mov si,0
cs:108 mov bx,1000h
cs:10b add al,[bx + si]
cs:10d inc si
cs:11e1 loop 10bh
;initialize AL register
;set loop counter to 10h=16d
;set si=0
;set bx=1000h as offset address
;accum. contents of mem. loc. [bx+si]
;increment si
;return to add
12
Based Index Addressing Example
Adding Words
In the following example of based indexed addressing, the contents of
16d words beginning at ds:1000h and ending with ds:101Dh are
accumulated in the ax register.
cs:100 mov ax,0
cs:103 mov cx,10h
cs:106 mov si,0
cs:109 mov bx,1000h
cs:10c add ax,[bx + si]
cs:10e add si,2
cs:111 loop 10ch
;initialize AX register
;set loop counter to 10h=16d
;set si=0
;set bx=1000h as offset address
;accum. contents of mem. loc. [bx+si]
;increment si by 2
;return to add
13
Stack Operation
To save register contents before calling a subroutine:
;save register contents before calling subroutine
push ax
push bx
push cx
push dx
;restore register contents after returning from subroutine
pop
dx
pop
cx
pop
bx
pop
ax
14
Assemble-Link-Execute Cycle
.asm
MASM
.exe
.obj
.lst
.map
15
Hello World .lst File
0000
0000
0000
0000
0000
0003
0005
0007
000A
000C
000F
0011
title Hello World Program (hello.asm)
;This program displays "Hello, world!"
directive ->.model small <-code and data each < 64K
directive -> .stack 100h <- 256 bytes reserved
directive -> .data
48 65 6C 6C 6F 2C
message db "Hello, world!",0dh,0ah,'$'
20 77 6F 72 6C 64
21 0D 0A 24
directive ->.code
main proc <-begin procedure
B8 ---- R
mov ax,@data <-assigns seg. Addr.
8E D8
mov ds,ax
to DS
B4 09
mov ah,9
BA 0000 R
mov dx,offset message
CD 21
int 21h
B8 4C00
mov ax,4C00h
CD 21
int 21h
main endp <-end procdure
end main <-end assembly
16
Hello World .map File
Start Stop
Length Name
00000H 00010H 00011H _TEXT
00012H 00021H 00010H _DATA
00030H 0012FH 00100H STACK
STACK
Origin
0001:0
Class
CODE
DATA
Group
DGROUP
Program entry point at 0000:0000
17
XCHG Instruction
Problem: move bx to ax and ax to bx
mov cx,ax ;ax stored temporarily in cx
mov ax,bx ;move bx to ax
mov bx,cx ;move cx (really ax) to bx
or use:
xchg ax,bx
Allowed:
xchg reg,reg
xchg reg,mem
xchg mem,reg
18
XCHGing Two Variables
Cannot do: xchg mem1,mem2, but
19
Arithmetic Instructions
INC and DEC Instructions
inc destination
dec destination
;add 1 to destination
;subtract 1 from destination
where destination is reg or mem
Examples:
inc al
dec ax
dec byte ptr membyte
dec memword
inc word ptr memword
;dec 8-bit memory operand
;dec memory operand
;inc 16-bit memory operand
20
Arithmetic Instructions
ADD Instruction
add destination, source
Example: add ax,bx
Contents of Registers
Before
add ax,bx
AX |0FFF
BX |0001
add ax,bx AX |0002
BX |FFFF
After
|1000
|0001
|
|
|0001
|FFFF
| plus a
| carry
21
Arithmetic Instructions
ADD Instruction
Consider the way in which add and adc, add with a carry,
deal differently with the carry flag. Both the AX and BX
registers contain 0000 and the carry flag is set, CY.
add ax,bx
carry)
yields AX=0, BX=0 and NC (no
adc ax,bx
yields AX=1, BX=0 and NC
22
Arithmetic Instructions
SUB Instruction
sub destination, source
Example: sub ax,bx
Contents of Registers
Before
After
sub ax,bx
AX |00A0
BX |0001
|009F
|0001
|
|
sub ax,bx
AX |0005
BX |0006
|FFFF
|0006
|
|
23
Arithmetic Instructions
MUL Instruction
mul multiplier
;multiplicand in ax
;product in dx,ax
Example: mul bx
Contents of Registers
Before
After
mul bx
AX |FFFF
BX |0010
DX |0000
|FFF0
|0010
|000F
|
|
|
24
Arithmetic Instructions
DIV Instruction
div divisor
;dividend in dx,ax: quotient in ax
;remainder in dx
Example: div bx
Contents of Registers
Before
After
div bx
AX |FFF1
BX |0010
DX |000F
|FFFF
|0010
|0001
|
|
|
25
Memory Models
Produces .com
files
What we will
use
Linear
addressing
26
Overlapping Segments
)
27