500 likes | 778 Views
Chapters 4, 5, and 6. Ch. 4: data movement instructions Mov, shift, push, pop, etc. Ch. 5: arithmetic and logic instructions Add, sub, and, or, mul, div, etc. Ch. 6: program control instructions Jump, call, etc. Number of Data Operands. Zero-operand instructions
E N D
Chapters 4, 5, and 6 • Ch. 4: data movement instructions • Mov, shift, push, pop, etc. • Ch. 5: arithmetic and logic instructions • Add, sub, and, or, mul, div, etc. • Ch. 6: program control instructions • Jump, call, etc.
Number of Data Operands • Zero-operand instructions • data is accessed from a “default” location, which is typically the “stack” (a LIFO (last-in-first-out) queue) • One-operand instructions • the accumulator (ACC) register is used as the default second data input and destination • Two-operand instructions • one of the data inputs is the default destination • Three-operand instructions
3 operand instruction ADD d, s1, s2 ; d := s1+s2 2 operand instruction ADD d, s1 ; d := d+s1 1 operand instruction ADD s1 ; ACC := ACC+s1 0 operand instruction ADD ; top_of_stack := top_of_stack + next_on_stack
Data Movement Instructions • MOV Variations • Move from register to register • Move from memory to register • Move from memory to register • Move from/to segment registers • Different types of addressing modes (Ch. 3) • Different sizes of data and data alignment • Byte, word, double-word • Word and double-word alignment
x86 Instruction Format OPcode (1-2 bytes) MOD REG R/M scaled index displacement(0~4bytes) immediate(0~4bytes) Scaled index byte General form of 1st byte Direction of data flow(D) D=0 : REG -> R/M D=1 : R/M -> REG Data size W=0 : data size is byte W=1 : data size is word or doubleword s s D W ss 00 = x1 01 = x2 10 = x4 11 = x8 index base OPcode REG
MOD Field • MOV AL, [DI] • MOV AL, [DI+2] • MOV AL, [DI+1000H]
Binary (or Machine Language) Representation • MOV BP, SP • Fig 4-4 REG OPcode (1-2 bytes) MOD REG R/M scaled index displacement(0~4bytes) immediate(0~4bytes)
Binary (or Machine Language) Representation • MOV DL, [DI] • Fig. 4-5 OPcode (1-2 bytes) MOD REG R/M scaled index displacement(0~4bytes) immediate(0~4bytes)
Binary (or Machine Language) Representation • MOV WORD PTR [BX+1000H], 1234H • Fig. 4-9 OPcode (1-2 bytes) MOD REG R/M scaled index displacement(0~4bytes) immediate(0~4bytes)
Stack Operations • Stack implemented using stack pointers and stack operations • LIFO (last-in first-out) data structure • SS (Stack Segment) register contains the beginning of stack segment • In real-mode, start of stack = SS * 10h, length = 64K • SP (Stack Pointer) contains current TOS (top of stack)
PUSH and POP • Stack: LIFO (last in first out) • SP grows downwards • PUSH AX
PUSH and POP PUSH BX : SP <- SP-2 POP CX : SP <- SP+2
Cyclic Nature in PUSH/POP • PUSH CX ; cyclic address calculation
Load-Effective Address • Assume an array called LIST • MOV BX, LIST • MOV BX, OFFSET LIST ; assembler calculates the offset • LEA BX, LIST ; microprocessor calculates the offset
String Data Transfers • LODS (load string), STOS (store string), MOVS (move string), INS (IN string), OUTS (out string) • Uses DI register for the destination (in ES) or the SI register for the source (in DS) • LODS • loads AL, AX, or EAX with data at DS:[SI] • SI auto-increments if D=0 (auto-decrements if D=1) • STOS • Stores AL, AX, or EAX at ES:[DI] • DI auto-increments if D=0 (auto-decrements if D=1)
String Data Transfers • LODS cases
String Data Transfers • REP (repeat) prefix • Causes CX (count) register to decrement by 1 each time string instruction executes • Instruction terminates when CX = 0
String Data Transfers • MOVS • The only memory-to-memory transfer • Transfers data from DS:[SI] to ES:[DI]
String Data Transfers • INS (input string) • Transfers data from an I/O device (whose address is in DX) to ES:[DI] • OUTS • From DS:[SI] to an I/O device (by DX)
IN and OUT IN AX, 11H OUT 10H, AX Port data Port data 1234H abcdH Data bus Data bus Port address Port address Microprocessor Microprocessor Address bus Address bus 10H 11H Control signal Control signal IOWC IORC AX = 1234H AX <= abcdH
Assembler Directives • Refer to Table 4-22 • Procedure
Homework • Chapter 4: 9, 21
Chapter 5Arithmetic and Logic Instructions • Addition • Table 5-1 and Table 5-3 • ADD, ADC (add with carry) • Subtraction • Table 5-4 and Table 5-6 • SUB, SBB (subtract with borrow) • Comparison • Implemented using subtraction • Just set flags; don’t change register value • Other arithmetic and logic instructions
Addition-with-Carry (ADC) • Addition of data whose size is larger than register
Multiplication (MUL, IMUL) • Multiplicand is always in AL (AX or EAX) • Product is in AX (DX-AX, or EDX-EAX)
Division • 8b division = 16b/8b • DIV CL ; AX / CL • Dividend: AX, quotient: AL, remainder: AH (with dividend’s sign) • 16 division • Dividend: DX-AX, quotient: AX, remainder: DX • Errors • Divide by zero • Divide overflow (divide by too small a number)
Division • Round the quotient
Logical AND and OR • AND is often used for masking • OR is often used for setting ‘1’
Exclusive OR • Selective inversion • XOR CH, CH • 2byte instruction • MOV CH, 0H • 3byte instruction
TEST, NOT, NEG • TEST instruction performs the AND operation without changing the destination operand • NOT: logical inversion (1’s complement) • NEG: arithmetic sign inversion (2’s complement)
SHIFT • SHL AX, 1 • SHR BX, 12 • SAR • Arithmetic right shift
ROTATE • Shift a wide number
String Comparisons • SCAS (string scan instruction) • The contents of the extra segment memory location addressed by DI is compared with AL, AX, or EAX • CMPS (compare strings instruction) • Compared DS:[SI] and ES:[DI] • Auto-increment (auto-decrement) SI and DI
Chapter 6Program Control Instructions • Jump • Procedure
Comparison (CMP) • CMP is a subtraction that changes only the flag bits
Program Control Instructions • Unconditional branch (or jump): JMP • Conditional branch (or jump): J<cond> • Table 6-1 • Assembler directives used for program control • Makes assembly more like a high-level language • .if, .else, .elseif, .endif • do-while loops • repeat-until loops • procedures (procedure vs. macro)
JMP Instructions short jump near jump far jump
Procedure • CALL procedure_name • Pushes the return address, i.e., the address of the instruction following the CALL on the stack • Jump does not consider return address!!! • RET instruction (in the procedure) removes an address from the stack to put it in IP • NEAR and FAR calls • 3B (NEAR) and 5B (FAR) instruction: similar to Jump • Return address is 2B (NEAR) and 4B (FAR)
Procedure Examples • USES pushes/pops registers on/from stack on procedure entry/exit
CALL with Register Operand • Jump to the offset address in the register operand