810 likes | 2.23k Views
Chapter 4 Data Movement Instructions. Instructor : Dr. Yu Youling. Content. Assembler Machine Language PUSH/POP LOAD Effective Address String Data Transfer Miscellaneous Data Transfer Instructions Segment Override Prefix. Why for Machine Ianguage Instructions.
E N D
Chapter 4Data Movement Instructions Instructor:Dr. Yu Youling
Content • Assembler • Machine Language • PUSH/POP • LOAD Effective Address • String Data Transfer • Miscellaneous Data Transfer Instructions • Segment Override Prefix
Why for Machine Ianguage Instructions • Luckily we do not often need to code into hexadecimal form • Using an assembler. • Assembler translates assembly code into machine language • Machine language is the native binary code μP understands • The reason for introducing the Machine language Instruction • Settle up a feeling for the way instructions are coded • Understand why different instruction contain different numbers of bytes • Help generating efficient code
Machine Language • Data Transfer Instruction • MOV is still the main data transfer instruction • There are many variations that perform special tasks such as PUSH and POP • Program Coding • Not often code in hexadecimal machine language • An understanding tends to help with learning the instruction set and the form of instructions in the memory
Instruction Modes • Instructions operate in the 16- or 32-bit mode. • 16-bit mode instructions are used in DOS • 32-bit mode instructions are used in Windows. • Override Prefix (超越前缀) • A prefix is used to switch between modes for one instruction at a time. • They toggle size of register and operand address from 16-bit to 32-bit or vice versa • The register override prefix is 66H and is a byte that precedes the opcode in the memory, • The address override prefix is a 67H and like 66H precedes the opcode in the memory
Instruction Modes • In the 16-bit mode • The register size prefix switches from 8- and 16-bit registers to 8- and 32-bit registers. • In the 32-bit mode • The register size prefix switches from 8- and 32-bit registers to 8- and 16-bit registers. (DOS) MOV EAX,EBX ;reg prefix used (Windows) MOV AX,BX ;reg prefix used
The Instruction Format • Machine Language Instruction • Vary from one bytes to as many as 13 bytes • Over 100,000 variations
The Opcode Byte(s) • 1-2 Bytes • Opcode - Select the operation • first 6 bits, i.e. 100010MOV • D – indicate the direction of data flow • D=0, Data: REG R/M • D=1, Data: R/M REG • W – indicate whether data is a byte or word(double words) • W=0, byte • W=1, word or double words, depends on the register size override prefix (66H)
The MOD/REG/(R/M) Field • The Field • The REG contains the register • The R/M field contains a register or a memory addressing mode, i.e. in [BX] • Specifies how R/M is used and whether a displacement exists • Memory - • 00: No displacement • 01: 8-bit sign-extended displacement 16/32-bit • 10: 16/32-bit signed displacement, depends on address size override prefix (67H) • Register • 11: R/M is a register • Examples • MOV AL,[DI] • MOV AL,[DI+2] • MOV AL,[DL+1000H]
Register Assignment • Register • REG field • R/M field when MOD=11
Register Assignment • Two-byte Instruction : 8BECH • 100010 11 11 101 100 • In 16-bit Instruction Mode, no override prefix • Opecode=100010 (MOV) • D=1 (R/MREG) • W=1 (word) • MOD=11 (R/M is a register) • REG=101 (BP) • R/M=100 (SP) • So, the Instruction is MOV BP,SP
Register Assignment • Two-byte Instruction : 668BE8H • 01100110 100010 11 11 101 000 • In 16-bit Instruction Mode, with 66H • Opecode=100010 (MOV) • D=1 (R/MREG) • W=1 (doubleword) • MOD=11 (R/M is a register) • REG=101 (EBP) • R/M=000 (EAX) • So, the Instruction is MOV EBP,EAX
The R/M Field • MOD=00, No Displacement • MOD=01, 8-bit sign-extended displacement • MOD=11, 16/32-bit signed displacement
The R/M Field • MOV DL, [DI] • Opcode 100010 • D=1 (R/MREG) • W=0 (byte) • MOD=00 (No Displacement) • REG=010 (DL) • R/M=101 (DI) • So, the machine language instruction is 8A15H
The R/M Field • MOV DL, [DI+1] • Opcode 100010 • D=1 (R/MREG) • W=0 (byte) • MOD=01 (8-bit sign-extended Displacement) • REG=010 (DL) • R/M=101 (DI) • Displacement=01 (displacement) • So, the machine language instruction is 8A5501H • For MOV DL, [DI+1000H], there is 8A950010H
Segment MOV Instructions • Opcode 100011, not 100010 • Not often directly address segment registers • It is important to understand the limitations of the segment register MOV instruction. • Immediate data cannot be moved into a segment register. • CS cannot successfully be loaded with a segment register MOV. • MOV CS, R/M and POP CS are not allowed.
PUSH/POP • Important instructions that store and retrieve data from the LIFO(last-in, first out) stack memory • Six forms • Register • Memory • Immediate • Segment register • Flags • All registers • Function with either 16- or 32-bit data.
PUSH • PUSH • Function with SS:SP • 16/32-bit data processing mode • Example: Figure 4-11 • PUSHA/PUSHAD (push all registers) • Order: AX, CX, DX, BX, SP, BP, SI, DI (or 32-bit registers in the same order) • Require 16 bytes of stack memory • See figure 4-12 • See Table 4-7 • PUSH reg16/reg32 • PUSH mem16/mem32 • PUSH seg • PUSH imm8/imm16 • PUSHD imm32 • PUSHF/PUSHFD
POP • POP • Function with SS:SP • 16/32-bit data processing mode • Example: Figure 4-13 • POPA/POPAD (POP all registers) • Order: DI, SI, BP, SP, BX, DX, CX, AX (or 32-bit registers in the same order) • Remove 16 bytes of stack memory • See Table 4-8 • POP reg16/reg32 • POP mem16/mem32 • POP seg • POPF/POPFD
Initializing the stack • When the stack area is initialized, both SS and SP registers should be loaded • SS the bottom location of the stack segment • SP the size of the stack • If stack area is 10000H-1FFFFH, size is FFFFH • SS1000H • SP0000H • Top location is contiguous with bottom location • See figure 4-14 • Can be automatically loaded by the assembler and linker program. • Using STACK segment definition.
Load Effective Address • Load offset address • LEA • Load offset address retrieved from a memory location • LDS, LES, LSS, LFS, LGS
LEA • The LEA instruction loads the effective address of a memory location into a pointer or index register. • LEA loads a 16- or 32-bit register with offset address • At times we do the same operation with a MOV and the keyword OFFSET MOV BX,OFFSET FRED LEA BX,FRED Both instruction accomplish the same task. • See Example 4-3
Load Segment and Index • LDS, LES, LFS, LGS, and LSS load a 16- or 32-bit register with offset address and a corresponding segment register DS, ES, FS, GS, or SS with a segment address • LDS, LES, LSS, LFG, and LGS allow a segment registers and a pointer to both be loaded from memory. LDS BX,BOB loads DS and BX with the offset and segment address stored in a 32-bit memory location called BOB. • See Example 4-4
String Data Transfer Instructions • String data transfer instructions • LODS, STOS, MOVS, INS, and OUTS. • These instructions use the direction flag bit to select the way that a pointer is modified after the instruction • D = 0 auto-increment • D = 1 auto-decrement. • Many of these instructions can be prefixed with a REP (repeat) to repeat the instruction the number of times stored in the CX register.
String Data Transfer Instructions • Direction Flag • D=0, auto-increment • D=1, auto-decrement • DI,SI • DI with extra segment ES • SI with data segment DS, can be segment overrided • Permissible forms with suffix • B, byte • W, word • D, doubleword • REP and CX • DX, I/O address for INS/OUTS
String Data Transfer Instructions • By default DI access data in extra segment and SI in data segment • LODS loads AL, AX, or EAX with data addressed by SI in data segment and increments or decrements SI • STOS stores AL, AX or EAX at the extra segment addressed by DI and increments or decrements DI • REPS STOS repeats the instruction the number of times stored in CX, i.e. terminates when CX=0 • See Example 4-5 • MOVS is the only instruction that transfers data between memory locations • See Example 4-6, 4-7, 4-8 • INS transfers data from I/O device into extra segment addressed by DI; I/O address is in DX register • OUTS transfers data from data segment memory addressed by SI to an I/O device addressed by DX
Miscellaneous • XCHG exchange contents of a register with any other register or memory location • XLAT converts the contents of AL register into a number stored in memory table • AL: the number • BX: the offset address of the table • See Example 4-11, Figure 4-17
Miscellaneous • IN and OUT instructions perform I/O operations • Two I/O addressing modes: fixed-port and variable port • In fixed-port addressing the port address appears in instructions, e.g. when using ROM • In variable-port addressing I/O address in a register • The two I/O instructions transfer data between an I/O device and the accumulator (AL, AX, and EAX). • 16-bit I/O address means there are up to 64K I/O devices possible in a system
IN and OUT • See Example 4-12
Miscellaneous • MOVSX is move and sign extend; MOVZX is move and zero-extend • For different size register operation
Miscellaneous • BSWAP • Convert between little endian form with big endian form • CMOV • Conditional move • Depends on the last flags influenced by some prior instructions
Segment Override Prefix • Segment override prefix can be added to almost any instructions in the memory addressing mode • JMP and CALL instruction can be used with segment override prefix • Example • MOV AX, ES:[DI] • LODS ES:DATA1
Assembler(汇编程序) • Directive(伪指令) • Storing Data in a memory segment (定义指令) • DB, define byte • DW, define word, 2 byte • DD, define doubleword, 4 bytes • DQ, define quadword, 8 bytes • DT, define ten bytes • DUP (占位指令) • ?, location reservation, initialized with 0 • ALIGN (对齐指令) • ALIGN 2 • ALIGN 4 • ALIGN 8
Assembler(汇编程序) • Directive(伪指令) • EQU, THIS, ORG, ASSUME • TEN EQU 10 • DATA1 EQU THIS BYTE • ORG 100H (改变偏移地址) • ASSUME CS:CODE_SEG • PROC, ENDP • Memory Organization • MODELs • Tiny, small, medium, large, huge
Assembly Program .MODEL TINY/SMALL .386 .DATA DATA1 DB/DW/DD 50 dup(?) ;comment .CODE .STARTUP MOV CX, 50 AGAIN: MOV AX, ES:[046CH] INC BX LOOP AGAIN .EXIT END
Homework • 第一部分 • 6,7,8,10,19,21,24,28,36