Download Assembly Language-Memory Addressing

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 Language
Symbol Table
Variables
.DATA
var
DW 0
sum
DD 0
array TIMES 10 DW 0
message DB ’Welcome
Welcome ’,00
char1 DB ?
Symbol Table
Name
Offset
var
0
sum
2
array
6
message 26
char1
35
Addressing Modes
• Register Addressing Mode
– Internal registers contain the data to be manipulated by
the instruction
– Register-addressing mode is the most efficient way of
specifying data.
– Data
D t are processedd within
ithi the
th processor with
ith memory
access.
– Examples
p
mov EAX,EBX
mov BX,CX
,
mov AL,CL
Addressing Modes (Cont.)
(Cont )
• Immediate Addressing Mode
– Data are specified as part of the instruction itself.
– The data are in memory,
y, but is located in the code
segment, not in the data segment.
– The immediate data are always a constant, given either
di tl or via
directly
i the
th EQU directive
di ti
– Example
mov AH,, 10
mov AX, 322
Memory Addressing Modes
• Direct Addressing Mode
– This mode requires access to the main memory
– Theyy tend to be slower than the immediate and register
g
addressing modes.
– Locate a datum in the data segment, require specifying
a segmentt address
dd
andd an offset
ff t value.
l
Data Segment
char1 DB ‘Y’
array TIMES 20 DD 0
name DB ’David’
’
id’
Code Segment
mov AL, [char1]
mov [char1], ‘X’
mov [array], 46
mov [name], ‘W’
Memory Addressing Modes
• Indirect Addressing Mode
– The offset or effective address of the data are stored in
general registers.
– It is not required for single element variables having.
– It is intended to be used for lists of variables (arrays).
Data Segment
char1 DB ‘Y’
array TIMES 20 DD 0
name DB ’David’
Code Segment
mov EBX, array
mov [EBX], 13
add EBX, 4
mov [EBX], 17
Indirect Addressing Mode
• The effective address can be loaded into a register by
– mov EBX, array
Or
– lea
l
register,
it
source
lea EBX, [array]
mov
Computes the offset values at
assembly time.
More efficient but limited
mov EBX, [array+ESI]; illegal
lea
Resolves the offset value at run
time.
Flexible but require run time
processing
p
ocess g
lea EBX, [array+ESI]; legal
Data Transfer Instructions
The MOV Instruction
– mov destination, source
Possible forms
– mov register, register
– mov register, immediate
– mov memory, immediate
– mov register, memory
– mov memory, register
i
There is no mov instruction to transfer data from memory
to memory (except for strings)
Data Transfer Instructions
The MOV Instruction
• Examples
– mov [var1], BH
– mov EDX, [array]
– mov [name+4], ‘W’
• Ambiguous Moves
Specifier Bytes
mov
mov
mov
mov
EBX, array
EBX
ESI, name
[EBX], 100
[ESI] 100
[ESI],
mov WORD [EBX], 100
mov BYTE [ESI], 100
mov [EBX], WORD 100
mov [ESI], BYTE 100
BYTE
1
WORD
2
DWORD 4
QWORD 8
Data Transfer Instructions
The XCHG Instruction
– The xchg instruction exchanges 8-, 16-, or 32-bit source and destination
operands.
– Examples
•
xchg
h EAX,
EAX EDX
xchg [answer], CL
xchg [total], DX
I cannot be
It
b used
d to exchange
h
two variables
i bl located
l
d in
i memory.
– xchg [var1], [name1] ; is illegal
mov ECX, EAX
mov EAX, EDX
mov EDX, ECX
xchg EAX, EDX
Arithmetic Instructions
The inc and dec
•
•
The inc (INCrement) instruction adds one to its operand and the
dec (DECrement) instruction subtracts one from its operand.
It can be
b either
ith in
i a register
i t or in
i memory.
Examples
inc EBX ; increment 32-bit register
g
dec DL ; decrement 8-bit register
•
Type Specifiers are used to
resolve ambiguity
inc WORD [EBX]
dec BYTE [ESI]
.DATA
count DW 0
value DB 25
.CODE
inc [count] ;unambiguous
dec [value] ;unambiguous
mov EBX, count
inc [EBX] ;ambiguous
mov ESI, value
dec [ESI] ;ambiguous
Arithmetic Instructions
The ADD Instruction
• Is used to add two 8-, 16- or 32-bit operands. The syntax is
– add destination, source ↔ destination = destination + source
– Similar to mov instruction,
instruction it takes the five basic forms of
operands.
– Note
inc EAX
vs.
add EAX,1
The first improves readability and requires less memory space to store
the instruction, both instructions execute at the same speed.
Arithmetic Instructions
• The sub instruction
– It is used to subtract two 8-, 16- or 32-bit numbers.
sub destination, source ↔ destination = destination – source
Examples:
• The cmp instruction
– The cmp instruction is used to compare two operands.
– It is similar to sub instruction except that the result of subtraction is not
saved.
– The operation sets the zero flag
Conditional Execution
jmp- Unconditional Jump
jmp
– It tells the processor that the next instruction to be executed is
located at the label.
– jmp label; label identifies the next instruction to be executed.
executed
Example
mov EAX,1
inc_again:
inc EAX
jmp inc_again
inc again
mov EBX,EAX
Conditional Execution
Conditional Jump
The pprogram
g
execution is transferred to the target
g instruction onlyy if the specified
p
condition is satisfied.
j<cond> label; <cond> identifies the condition of the branch (jump)
Usually, the condition being tested is the result of the last arithmetic or logic operation.
Example:
read_char:
mov DL,0
...
(code for reading a character into AL)
...
cmp AL,0DH
;compare the character to CR
je CR_received
inc CL
jmp read_char
;go to to read another
CR_received
mov DL,AL
…
Conditional Execution
Conditional Jumps
je jump
j
if equall
jg jump if greater
jl jump if less
jge jump if greater than or equal
jle jump if less than or equal
jne jump if not equal
Conditional jumps that test flags.
j jump
jz
j
if zero (i.e.,
(i if ZF = 1))
jnz jump if not zero (i.e., if ZF = 0)
jc jump if carry (i.e., if CF = 1)
jnc jump if not carry (i.e., if CF = 0)
Conditional Execution
Example:
go_back:
inc AL
...
cmp AL,BL
statement_1
mov BL,77H
,
Iteration Instruction
The <loop body> is executed 50 times.
Iteration using jump instructions.
mov CL,50
repeat1:
<loop body>
dec CL
jnz repeat1
Using the loop instruction
mov ECX,50
repeat1:
<loop body>
loop repeat1
Logical Instructions
and destination,
destination source
and AL, 01H
or destination,
destination source
or AX, FAH
xor destination,
destination source
xor EAX, DDFFH
nott destination
d ti ti
not EBX
Logical Instructions
and AL,01H
AL 01H
je bit_is_zero
<code to be executed when the bit is one>
jmp skip1
bit_is_zero:
<code to be executed when the bit is zero>
skip1:
<rest of the code>