1
IKI10230Pengantar Organisasi Komputer
Kuliah no. 05.a: Data Transfer InstructionsSumber:1. Paul Carter, PC Assembly Language2. Hamacher. Computer Organization, ed-53. Materi kuliah CS61C/2000 & CS152/1997, UCB4. Intel Architecture Software Developer’s Manual
17 Maret 2004
L. Yohanes Stefanus ([email protected])Bobby Nazief ([email protected])
bahan kuliah: http://www.cs.ui.ac.id/kuliah/POK/
2
KONVENSI
3
Set Instruksi Intel x86
° OP-CODE DEST,SRC ; DEST [SRC] OP [DEST]
° Hanya salah satu dari SRC atau DEST dapat berupa memory-operand
° ACC: representasi untuk AL/AX/EAX
° Sebagian instruksi menyebabkan perubahan nilai FLAGS, khususnya instruksi-instruksi Aritmatika & Logika
4
DATA TRANSFER INSTRUCTIONS
5
Data Transfer Instructions
° General-purpose data movement• Move
• Conditional Move
° Exchange
° Stack manipulation
° Type-conversion
6
Data Transfer Instructions: Summary (1/2)
1. MOV Move
2. CMOVE/CMOVZ Conditional move if equal/Conditional move if zero
3. CMOVNE/CMOVNZ Conditional move if not equal/Conditional move if not zero
4. CMOVA/CMOVNBE Conditional move if above/Conditional move if not below or equal
5. CMOVAE/CMOVNB Conditional move if above or equal/Conditional move if not below
6. CMOVB/CMOVNAE Conditional move if below/Conditional move if not above or equal
7. CMOVBE/CMOVNA Conditional move if below or equal/Conditional move if not above
8. CMOVG/CMOVNLE Conditional move if greater/Conditional move if not less or equal
9. CMOVGE/CMOVNL Conditional move if greater or equal/Conditional move if not less
10. CMOVL/CMOVNGE Conditional move if less/Conditional move if not greater or equal
11. CMOVLE/CMOVNG Conditional move if less or equal/Conditional move if not greater
12. CMOVC Conditional move if carry
13. CMOVNC Conditional move if not carry
14. CMOVO Conditional move if overflow
15. CMOVNO Conditional move if not overflow
16. CMOVS Conditional move if sign (negative)
17. CMOVNS Conditional move if not sign (non-negative)
18. CMOVP/CMOVPE Conditional move if parity/Conditional move if parity even
19. CMOVNP/CMOVPO Conditional move if not parity/Conditional move if parity odd
7
Data Transfer Instructions: Summary (2/2)
20. XCHG Exchange
21. BSWAP Byte swap
22. XADD Exchange and add
23. CMPXCHG Compare and exchange
24. CMPXCHG8B Compare and exchange 8 bytes
25. PUSH Push onto stack
26. POP Pop off of stack
27. PUSHA/PUSHAD Push general-purpose registers onto stack
28. POPA/POPAD Pop general-purpose registers from stack
29. CWD/CDQ Convert word to doubleword/Convert doubleword to quadword
30. CBW/CWDE Convert byte to word/Convert word to doubleword in EAX register
31. MOVSX Move and sign extend
32. MOVZX Move and zero extend
8
Data Transfer Instructions: Move° MOV DEST,SRC ; DEST [SRC]
• cannot be used to load the CS register
• cannot be used to do memory-to-memory transfer
9
Format Instruksi MOV (1/2)
REG/MEM [REG]• MOV r/m8,r8 Move r8 to r/m8
• MOV r/m16,r16 Move r16 to r/m16
• MOV r/m32,r32 Move r32 to r/m32
REG [REG/MEM]• MOV r8,r/m8 Move r/m8 to r8
• MOV r16,r/m16 Move r/m16 to r16
• MOV r32,r/m32 Move r/m32 to r32
REG/MEM [SEG-REG]• MOV r/m16,Sreg Move segment register to r/m16
SEG-REG [REG/MEM] • MOV Sreg,r/m16 Move r/m16 to segment register
10
Format Instruksi MOV (2/2)AL/AX/EAX [MEM]
• MOV AL,moffs8 Move byte at (seg:offset) to AL
• MOV AX,moffs16 Move word at (seg:offset) to AX
• MOV EAX,moffs32 Move doubleword at (seg:offset) to EAX
MEM [AL/AX/EAX]• MOV moffs8,AL Move AL to (seg:offset)
• MOV moffs16,AX Move AX to (seg:offset)
• MOV moffs32,EAX Move EAX to (seg:offset)
REG IMM• MOV r8,imm8 Move imm8 to r8
• MOV r16,imm16 Move imm16 to r16
• MOV r32,imm32 Move imm32 to r32
[REG/MEM] IMM• MOV r/m8,imm8 Move imm8 to r/m8
• MOV r/m16,imm16 Move imm16 to r/m16
• MOV r/m32,imm32 Move imm32 to r/m32
11
Data Transfer Instructions: Conditional Move
12
Format Instruksi CMOVcc
° CMOVA r16, r/m16 Move if above (CF=0 and ZF=0)° CMOVA r32, r/m32 Move if above (CF=0 and ZF=0)
° CMOVAE r16, r/m16 Move if above or equal (CF=0)° CMOVAE r32, r/m32 Move if above or equal (CF=0)
° CMOVB r16, r/m16 Move if below (CF=1)° CMOVB r32, r/m32 Move if below (CF=1)
° CMOVBE r16, r/m16 Move if below or equal (CF=1 or ZF=1)° CMOVBE r32, r/m32 Move if below or equal (CF=1 or ZF=1)
° CMOVC r16, r/m16 Move if carry (CF=1)° CMOVC r32, r/m32 Move if carry (CF=1)
° CMOVE r16, r/m16 Move if equal (ZF=1)° CMOVE r32, r/m32 Move if equal (ZF=1)
13
Data Transfer Instructions: Exchange
° XCHG DEST,SRC ;Exchange• [DEST] ↔ [SRC]
° BSWAP R32 ;Byte swap• R32[7..0] ↔ R32[31..24]
• R32[15..8] ↔ R32[23..16]
° XADD DEST,SRC ;Exchange and add• TEMP ← [SRC] + [DEST]
• SRC ← [DEST]
• DEST ← [TEMP]
° CMPXCHG DEST,SRC ;Compare and exchange/* accumulator = AL, AX, or EAX, depending on whether */
IF ([accumulator] == [DEST]) { ZF ← 1; DEST ← [SRC] }
ELSE { ZF ← 0; accumulator ← [DEST] }
° CMPXCHG8B [DATA] ;Compare & exchange 8 bytesIF ([EDX:EAX] == [DEST]) { ZF ← 1; DEST ← [ECX:EBX] }
ELSE { ZF ← 0; EDX:EAX ← [DEST] }
14
Format Instruksi XCHG
° XCHG AX,r16 Exchange r16 with AX
° XCHG r16,AX Exchange AX with r16
° XCHG EAX, r32 Exchange r32 with EAX
° XCHG r32, EAX Exchange EAX with r32
° XCHG r/m8, r8 Exchange r8 with byte from r/m8
° XCHG r8, r/m8 Exchange byte from r/m8 with r8
° XCHG r/m16, r16 Exchange r16 with word from r/m16
° XCHG r16, r/m16 Exchange word from r/m16 with r16
° XCHG r/m32, r32 Exchange r32 with doubleword from r/m32
° XCHG r32, r/m32 Exchange doubleword from r/m32 with r32
15
Format Instruksi BSWAP & XADD
° BSWAP r32 Reverses the byte order of a 32-bit register.
° XADD r/m8, r8 Exchange r8 and r/m8; load sum into r/m8.
° XADD r/m16, r16 Exchange r16 and r/m16; load sum into r/m16.
° XADD r/m32, r32 Exchange r32 and r/m32; load sum into r/m32.
16
Format Instruksi CMPXCHG & CMPXCHG8B
° CMPXCHG r/m8,r8 Compare AL with r/m8. If equal,ZF is set and r8 is loaded intor/m8. Else, clear ZF and load
r/m8into AL.
° CMPXCHG r/m16,r16 Compare AX with r/m16. If equal,ZF is set and r16 is loaded intor/m16. Else, clear ZF and loadr/m16 into AX
° CMPXCHG r/m32,r32 Compare EAX with r/m32. Ifequal, ZF is set and r32 is
loadedinto r/m32. Else, clear ZF andload r/m32 into EAX
° CMPXCHG8B m64 Compare EDX:EAX with m64. Ifequal, set ZF and load
ECX:EBXinto m64. Else, clear ZF and
loadm64 into EDX:EAX.
17
Data Transfer Instructions: Stack Manipulation
° PUSH SRC ; Push onto stack; GP-regs, SEG-regs, MEM, Immd.
• ESP [ESP] – 4/2
• [ESP] [SRC]
18
Format Instruksi PUSH
° PUSH r/m16 Push r/m16• PUSH WORD [DATA]
° PUSH r/m32 Push r/m32• PUSH DWORD [DATA]
° PUSH r16 Push r16° PUSH r32 Push r32° PUSH imm8 Push imm8° PUSH imm16 Push imm16° PUSH imm32 Push imm32
° PUSH CS Push CS° PUSH SS Push SS° PUSH DS Push DS° PUSH ES Push ES° PUSH FS Push FS° PUSH GS Push GS
19
Data Transfer Instructions: Stack Manipulation
° POP DEST ; Pop off of stack; GP-regs, SEG-regs (-CS), MEM
• DEST [[ESP]]
• ESP [ESP] + 4/2
20
Format Instruksi POP
° POP r/m16 Pop top of stack into m16; increment stack pointer
• POP WORD [DATA]
° POP r/m32 Pop top of stack into m32; increment stack pointer
• POP DWORD [DATA]
° POP r16 Pop top of stack into r16; increment stack pointer
° POP r32 Pop top of stack into r32; increment stack pointer
° POP DS Pop top of stack into DS; increment stack pointer
° POP ES Pop top of stack into ES; increment stack pointer
° POP SS Pop top of stack into SS; increment stack pointer
° POP FS Pop top of stack into FS; increment stack pointer
° POP GS Pop top of stack into GS; increment stack pointer
21
Data Transfer Instructions: Stack Manipulation
° PUSHA/PUSHAD ; Push (16-/32-bit) GP-regs onto stack
° POPA/POPAD ; Pop (16-/32-bit) GP-regs from stack
22
Data Transfer Instructions: Type Conversion
° CBW ; Convert byte to word• AX ← SignExtend[AL]
° CWD ; Convert word to doubleword• DX:AX ← SignExtend[AX]
° CWDE ; Convert word to doubleword• EAX ← SignExtend[AX]
° CDQ ; Convert doubleword to quadword• EDX:EAX ← SignExtend[EAX]
° MOVSX DEST,SRC; Move and sign extend• DEST ← SignExtend[SRC]
° MOVZX DEST,SRC ; Move and zero extend• DEST ← ZeroExtend[SRC]
23
Format Instruksi MOVSX & MOVZX
° MOVSX r16,r/m8 Move byte to word with sign-extension
° MOVSX r32,r/m8 Move byte to doubleword, sign-extension
° MOVSX r32,r/m16 Move word to doubleword, sign-extension
° MOVZX r16,r/m8 Move byte to word with zero-extension
° MOVZX r32,r/m8 Move byte to doubleword, zero-extension
° MOVZX r32,r/m16 Move word to doubleword, zero-extension
24
first.asm (1/3)
1. %include "asm_io.inc"
2. segment .data
3. ;
4. ; These labels refer to strings used for output
5. ;
6. prompt1 db "Enter a number: ", 0 ; don't forget nul terminator
7. prompt2 db "Enter another number: ", 0
8. outmsg1 db "You entered ", 0
9. outmsg2 db " and ", 0
10.outmsg3 db ", the sum of these is ", 0
11.segment .bss
12. ;
13. ; These labels refer to double words used to store the inputs
14. ;
15. input1 resd 100
16. input2 resd 1
25
first.asm (2/3)
17.segment .text
18. global _asm_main
19._asm_main:
20. enter 0,0 ; setup routine
21. pusha
22. mov eax, prompt1 ; print out prompt
23. call print_string
24. call read_int ; read integer
25. mov [input1], eax ; store into input1
26. mov eax, prompt2 ; print out prompt
27. call print_string
28. call read_int ; read integer
29. mov [input2], eax ; store into input2
30. ...
26
first.asm (3/3)31. mov eax, [input1] ; eax = dword at input1
32. add eax, [input2] ; eax += dword at input2
33. mov ebx, eax ; ebx = eax
34. mov eax, outmsg1
35. call print_string ; print out first message
36. mov eax, [input1]
37. call print_int ; print out input1
38. mov eax, outmsg2
39. call print_string ; print out second message
40. mov eax, [input2]
41. call print_int ; print out input2
42. mov eax, outmsg3
43. call print_string ; print out third message
44. mov eax, ebx
45. call print_int ; print out sum (ebx)
46. call print_nl ; print new-line
47. popa
48. mov eax, 0 ; return back to C
49. leave
50. ret
27
first.exe