Download Assembly Programming 2

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

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

Transcript
Assembly Language
Symbol Table
Variables
.DATA
var
DW 0
sum
DD 0
array TIMES 10 DW 0
message DB ’Welcome ’,0
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 are processed within the processor with memory
access.
– Examples
mov EAX,EBX
mov BX,CX
mov AL,CL
Addressing Modes (Cont.)
• Immediate Addressing Mode
– Data are specified as part of the instruction itself.
– The data are in memory, but is located in the code
segment, not in the data segment.
– The immediate data are always a constant, given either
directly or via the EQU directive
– Example
mov AH, 10
mov AX, 322
Memory Addressing Modes
• Direct Addressing Mode
– This mode requires access to the main memory
– They tend to be slower than the immediate and register
addressing modes.
– Locate a datum in the data segment, require specifying
a segment address and an offset value.
Data Segment
char1 DB ‘Y’
array TIMES 20 DD 0
name DB ’David’
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 register, 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
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
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
ESI, name
[EBX], 100
[ESI], 100
mov WORD [EBX], 100
mov BYTE [ESI], 100
BYTE
1
WORD
2
DWORD 4
mov [EBX], WORD 100
mov [ESI], BYTE 100
QWORD 8
Data Transfer Instructions
The XCHG Instruction
– The xchg instruction exchanges 8-, 16-, or 32-bit source and destination
operands.
– Examples
xchg EAX, EDX
xchg [answer], CL
xchg [total], DX
• It cannot be used to exchange two variables located in 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 either in a register or in memory.
Examples
inc EBX ; increment 32-bit register
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, 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
– 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.
Example
mov EAX,1
inc_again:
inc EAX
jmp inc_again
mov EBX,EAX
Conditional Execution
Conditional Jump
The program execution is transferred to the target instruction only if the specified
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 if equal
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.
jz jump if zero (i.e., 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, source
and AL, 01H
or destination, source
or AX, FAH
xor destination, source
xor EAX, DDFFH
not destination
not EBX
Logical Instructions
and 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>