Download lesson03.ppt

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
Intel’s 8086 instruction-set
A look at the main categories of
processor instructions for the
earliest Intel x86 CPU
Data-Transfer
• These instructions simply copy some data
from one place in the system to another
– From one register to another register
– From a memory location to a register
– From a register to a memory location
– From within an instruction to a register
– From within an instruction to a memory cell
‘mov’
• It’s the most frequently-used instruction
immediate data
general register
or memory
general register
segment register
Some other data-transfers
•
•
•
•
•
Exchange (‘xchg’)
Push/Pop
Pushf/Popf
In/Out
Load pointer (‘lds’, ‘les’, ‘lfs’, lgs’, ‘lss’)
• Note: A distinctive feature data-transfers is
that FLAGS are unmodified (except ‘popf’)
Arithmetic Instructions
•
•
•
•
•
•
Addition: ‘add’ and ‘adc’
Subtraction: ‘sub’ and ‘sbb’
Multiplication: ‘mul’ and ‘imul’
Division: ‘div’ and ‘idiv’
Increment/Decrement: ‘inc’ and ‘dec’
Comparison/Negation: ‘cmp’ and ‘neg’
• All these instructions do modify FLAGS
Boolean Logic Instructions
•
•
•
•
•
Bitwise AND: ‘and’
Bitwise OR: ‘or’
Bitwise XOR: ‘xor’
Bitwise TEST: ‘test’
Bitwise complement: ‘not’
• These instructions also modify FLAGS
Shift/Rotate Instructions
•
•
•
•
Left-shifts: ‘shl’ and ‘sal’
Right-shifts: ‘shr’ and ‘sar’
Left-rotations: ‘rol’ and ‘rcl’
Right-rotations: ‘ror’ and ‘rcr’
• These instructions affect the Carry-Flag
Control Transfers
• These instructions modify register RIP so
as to alter the normal fetch-execute cycle
• Unconditional Jumps: ‘jmp’ and ‘ljmp’
• Subroutine Calls: ‘call’ and ‘lcall’
• Subroutine Returns: ‘ret’ and ‘lret’
• Conditional Jumps: ‘jz’, ‘jnz’, ‘jc’, ‘jnc’, etc
• These transfers do not modify the FLAGS
String Manipulations
• This unusual group of complex instructions
is for efficient word-processing operations
– Move string: ‘movs’
– Compare string: ‘cmps’
– Scan string: ‘scas’
– Store string: ‘stos’
– Load string: ‘lods’
– Repeat prefixes: ‘rep’, ‘repe’ and ‘repne’
Processor Control
• This group of instructions is for specialized
systems programming situations
– Halt the fetch-ececute cycle: ‘hlt’
– Wait for coprocessor to finish: ‘wait’
– Lock the system bus temporarily: ‘lock’
– Adjust the Direction Flag: ‘cld’ or ‘std’
– Adjust the Interrupt Flag: ‘cli’ or ‘sti’
– Adjust the Carry Flag: ‘cmc’, ‘clc’ or stc’
Special-purpose instruction
initializations
the loop body
decrement RCX
no
RCX == 0?
yes
This two-step construct
occurs so often within
ordinary programs that
a special instruction is
available to do both in
a single instruction
Stack operation: ‘push’
register
data
64-bits
64-bits
data
data
data
data
data
data
unused
data
unused
unused
unused
unused
before ‘push’’
after ‘push’
SS:RSP
SS:RSP
The ‘push’ instruction, applied to a register, performs a two-step operation:
first, the value in the stack-pointer register is decreased by 8, and then the
value in the register operand is copied into the stack-location at SS:RSP
Stack operation: ‘pop’
register
64-bits
64-bits
data
data
data
data
data
SS:RSP
SS:RSP
data
data
unused
unused
unused
unused
unused
before ‘pop’’
after ‘pop’
The ‘pop’ instruction, applied to a register, performs a two-step operation:
first, the value in the stack-location at SS:RSP is copied into that register,
and afterward the value in the stack-pointer register is increased by 8.
An application
• We can apply these stack-operations to a
string of characters in a memory-buffer, so
as to reverse the order of those characters
stack
RSP
buffer
A B C D
A
B
C
D
buffer
D C B A
pop
push
push
push
push
pop
pop
pop
‘reverse.s’
• We’ve constructed a demo-program that
reverses the order of characters which a
user types in
• It uses the ‘push’ and ‘pop’ operations, as
well as the special x86 ‘loop’ instruction
• It also employs the ‘read’ system-call to
get a string of characters typed by a user
The program’s flow
Prompt the user for input
INPUT
Accept the user’s response
Loop to ‘push’ each buffer character onto stack
PROCESS
Loop to ‘pop’ each character to output buffer
Show rearranged characters in output buffer
OUTPUT
Loops are ‘consecutive’
Loops are ‘nested’
In-class exercise
• Can you modify the ‘reverse.s’ program so
all words remain in the same order, but the
order of the letters within each word do get
reversed?
INPUT
This is our moment, this is our time.
OUTPUT
sihT si rou themom, siht si rou emit.
• HINT: You will need a loop-within-a-loop