Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are...
-
Upload
jason-parrish -
Category
Documents
-
view
228 -
download
2
Transcript of Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are...
![Page 1: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/1.jpg)
Chapter 4Basic Instructions
![Page 2: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/2.jpg)
4.1 Copying Data
![Page 3: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/3.jpg)
mov Instructions
• mov (“move”) instructions are really copy instructions, like simple assignment statements in a high-level language
• Format: mov destination, source
registerormemory
register,memoryorimmediate
![Page 4: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/4.jpg)
Operand Restrictions
• Operands must be same size• Can’t move from memory to memory
– mov nbr1, nbr2illegal if nbr1 and nbr2 reference doublewords in memory
– Instead use a registermov eax, nbr2mov nbr1, eax
• Can only move one byte, word or doubleword at a time
![Page 5: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/5.jpg)
Effect on Flags
• In general, an instruction may have one of three effects:– no flags are altered– specific flags are given values depending on
the results of the instruction– some flags may be altered, but their settings
cannot be predicted
• No mov instruction changes any flag
![Page 6: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/6.jpg)
Machine Code• Depends on operand type(s), with several
different opcodes used for mov instructions
• Word-size and doubleword-size instructions use same opcodes, but word-size instructions have 66 prefix byte
• Object and source code from listing fileB0 9B mov al, 15566| B8 009B mov ax, 155B8 0000009B mov eax, 155
![Page 7: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/7.jpg)
ModR/M Byte
• Part of the object code for many instructions
• Used to encode specific registers
• Used to distinguish between instructions that share the same opcode
• Used to specify memory modes
![Page 8: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/8.jpg)
ModR/M Fields
• mod (mode), 2 bits
• reg (register), 3 bits
• r/m (register/memory), 3 bits
• Examples of encodings–mod = 00 and r/m = 101 combined always
means direct memory addressing– reg = 011 means the EBX register in a 32-bit
instruction
![Page 9: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/9.jpg)
xchg Instruction
• Swaps the values referenced by its two operands– Can’t have both operands in memory
• Does not alter any flag
![Page 10: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/10.jpg)
4.2 Integer Addition and Subtraction Instructions
![Page 11: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/11.jpg)
add Instruction• Format: add destination, source• The integer at source is added to the
integer at destination and the sum replaces the old value at destination
• SF, ZF, OF, CF, PF and AF flags are set according to the value of the result of the operation– Example: CF = 1 if there is a carry out of the
sum
![Page 12: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/12.jpg)
Addition Example
• BeforeEAX: 00000075ECX: 000001A2
• Instructionadd eax, ecx
• After EAX: 00000217ECX: 000001A2SF=0 ZF=0 CF=0 OF=0
![Page 13: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/13.jpg)
sub Instruction• Format: sub destination, source• The integer at source is subtracted from
the integer at destination and the difference replaces the old value at destination
• SF, ZF, OF, CF, PF and AF flags are set according to the value of the result of the operation– Example: ZF = 1 if the difference is zero
![Page 14: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/14.jpg)
Subtraction Example
• Beforedoubleword at Dbl: 00000100
• Instructionsub Dbl, 2
• After Dbl: 000000FESF=0 ZF=0 CF=0 OF=0
![Page 15: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/15.jpg)
Instruction Encoding
• Opcode depends on operand types
• The ModR/M byte distinguishes– Between operand types– Between add, sub and other operations for
certain operand types
• An small immediate operand is sometimes encoded as a byte even in a 32-bit instruction
![Page 16: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/16.jpg)
Increment and Decrement Instructions
• inc destination– Adds 1 to destination
• dec destination– Subtracts 1 from destination
• Each sets same flags as add or sub except for CF which isn’t changed
![Page 17: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/17.jpg)
neg Instruction
• neg destination• Negates (takes the 2's complement of) its
operand– A positive value gives a negative result – A negative value will become positive– Zero remains 0
• Affects same flags as add and sub
![Page 18: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/18.jpg)
Programming in Assembly Language
• Start with a design
• Plan register usage– Decide what registers will be used for what
variables in the design– There are only a few available registers
• Plan memory usage
![Page 19: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/19.jpg)
4.3 Multiplication Instructions
![Page 20: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/20.jpg)
Multiplication Instruction Mnemonics
• mul for unsigned multiplication– Operands treated as unsigned numbers
• imul for signed multiplication– Operands treated as signed numbers and
result is positive or negative depending on the signs of the operands
![Page 21: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/21.jpg)
mul Instruction Format• mul source • Single operand may be byte, word,
doubleword or quadword in register or memory (not immediate) and specifies one factor
• Location of other factor is implied– AL for byte-size source– AX for word source– EAX for doubleword source– RAX for quadword source
![Page 22: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/22.jpg)
mul Instruction Operation• When a byte source is multiplied by the
value in AL, the product is put in AX• When a word source is multiplied by the
value in AX, the product is put in DX:AX– The high-order 16 bits in DX and the low-order
16 bits in AX
• When a doubleword source is multiplied by the value in EAX, the product is put in EDX:EAX
• Product of two quadwords in RAX:DAX
![Page 23: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/23.jpg)
Double-Length Product• The “double-length” product ensures that
the result will always fit in the destination location
• If significant bits of the product actually “spill over” into the high-order half (AH, DX or EDX), then CF and OF are both set to 1
• If the high-order half is not significant, then CF and OF are both cleared to 0– For unsigned multiplication, this is when the
high-order half is all 0’s
![Page 24: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/24.jpg)
mul Instruction Example
• BeforeEAX: 00000005 EBX: 00000002EDX: ????????
• Instructionmul ebx
• After EAX: 0000000AEBX: 00000002EDX: 00000000CF=OF=0
![Page 25: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/25.jpg)
imul Instruction Formats
• imul source
• imul register, source
• imul register, source, immediate
![Page 26: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/26.jpg)
imul source
• “Single-operand format”
• Similar to mul source except for signed operands
• CF=OF=0 if each bit in the high-order half is the same as the sign bit in the low-order half
• CF=OF=1 otherwise (the bits in the high-order half are significant)
![Page 27: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/27.jpg)
Single-Operand Example
• BeforeAX: ??05 byte at Factor: FF
• Instructionimul Factor
• After AX: FFFBCF=OF=0
![Page 28: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/28.jpg)
imul register,source• “Two-operand format”• Source operand can be in a register, in
memory, or immediate• Register contains other factor, and also
specifies the destination• Both operands must be word-size or
doubleword-size, not byte-size• Product must “fit” in destination register
– CF and OF are cleared to 0 if result fits– CF and OF are set to 1 if it doesn’t fit
![Page 29: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/29.jpg)
Two-operand Example
• BeforeEBX: 0000000A
• Instructionimul ebx, 10
• After EBX: 00000064CF=OF=0
![Page 30: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/30.jpg)
imul register,source,immediate• “Three-operand format”• The two factors are given by source (register
or memory) and the immediate value• The first operand, a register, specifies the
destination for the product• Operands register and source are the same
size, both 16-bit or both 32-bit (not 8-bit)• If the product will fit in the destination
register, then CF and OF are cleared to 0; if not, they are set to 1
![Page 31: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/31.jpg)
Three-Operand Example
• Beforeword at Value: 08F2 BX: ????
• Instructionimul bx, Value, 1000
• After BX: F150CF=OF=1
![Page 32: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/32.jpg)
4.4 Division Instructions
![Page 33: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/33.jpg)
Division Instruction Formats
•idiv sourcefor signed operands
•div sourcefor unsigned operands
• source identifies the divisor– Byte, word, doubleword or quadword– In memory or register, but not immediate
![Page 34: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/34.jpg)
Implicit Dividend for div and idiv
• Byte source divided into word in AX
• Word source divided into doubleword in DX:AX
• Doubleword source divided into quadword in EDX:EAX
• Quadword source divided into RDX:RAX
![Page 35: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/35.jpg)
Results of div and idiv• Byte-size divisor:
quotient in AL and remainder in AH
• Word-size divisor:quotient in AX and remainder in DX
• Doubleword-size divisor: quotient in EAX and remainder in EDX
• Quadword-size divisor: quotient in RAX and remainder in RDX
![Page 36: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/36.jpg)
Results of div and idiv
• All division operations satisfy the relation dividend = quotient*divisor + remainder– For signed division, the remainder will have
same sign as dividend
![Page 37: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/37.jpg)
Flag Settings
• Division instructions do not set flags to any meaningful values
• They may change previously set values of AF, CF, OF, PF, SF or ZF
![Page 38: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/38.jpg)
Unsigned Division Example• Before
EDX: 00 00 00 00EAX: 00 00 00 64EBX: 00 00 00 0D
• Instructiondiv ebx ; 100/13
• After EDX: 00000009EAX: 00000007
100 = 7 * 13 + 9
![Page 39: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/39.jpg)
Signed Division Example• Before
EDX: FF FF FF FFEAX: FF FF FF 9CECX: 00 00 00 0D
• Instructionidiv ecx ; -100/13
• After EDX: FFFFFFF7EAX: FFFFFFF9
–100 = (–7) * 13 + (–9)
![Page 40: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/40.jpg)
Errors in Division
• Caused by– Dividing by 0, or– Quotient too large to fit in destination
• Triggers an exception– The interrupt handler routine that services this
exception may vary from system to system– When a division error occurs for a program
running under Visual Studio, an error window pops up
![Page 41: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/41.jpg)
Preparing for Division
• Dividend must be extended to double length
• Example– Copy a doubleword dividend to EAX– Extend dividend to EDX:EAX
• For unsigned division, use mov edx, 0• For signed division, use cdq instruction
– Finally use div or idiv instruction
![Page 42: Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.](https://reader035.fdocuments.us/reader035/viewer/2022081506/56649d6e5503460f94a50137/html5/thumbnails/42.jpg)
Convert Instructions• No operand• cbw
sign extends the byte in AL to the word in AX• cwd
sign extends the word in AX to the doubleword in DX:AX
• cdqsign extends the doubleword in EAX to the quadword in EDX:EAX
• cqosign extends the quadword in RAX to RDX:RAX