Data Transfers, Addressing, and Arithmetic Lecture 6
Data Transfer Instructions Operand Types Three basic types of operands: *Immediate: a constant integer (8, 16, or 32 bits) -value is encoded within the instruction *Register: the name of a register -register name is converted to a number and encoded within the instruction *Memory: reference to a location in memory memory address is encoded within the instruction, or a register holds the address of a memory location
Direct Memory Operands -A direct memory operand is a named reference to storage in memory -The named reference (label) is automatically dereferenced by the assembler. The brackets imply a deference operation .Data Var1 byte 10h .code Mov al, var1 Mov al, [var1]
MOV Instruction *MOV Instruction Move (copy) from source to destination *Syntax: MOV destination, source -destination operand’s contents change source operand’s contents do not change -Both operands must be the same size -Both operands cannot be memory operands -CS, EIP, and IP cannot be the destination -No immediate to segment moves
Here is a list of the general variants of MOV, excluding segment registers: -MOV reg, reg -MOV mem, reg -MOV reg, mem -MOV mem, imm -MOV reg, imm
Examples: .data count BYTE 100 wVal WORD 2 .code mov bl,count mov ax,wVal mov count,al mov al,wVal ; error, AL is 8 bits mov ax,count ; error, AX is 16 bits mov eax,count ; error, EAX is 32 bits
Arithmetic Instructions – Processing Binary Data Description ADC Add with carry IDIV Divide signed ADD Add IMUL Multiply signed CBW Convert byte to word MUL Multiply unsigned CDQ Convert doubleword to quadword NEG Negate CWD Convert word to doubleword SBB Subtract with borrow CWDE Convert word to extended doubleword SUB Subtract DIV Divide unsigned
XCHG Instruction XCHG Instruction exchanges the values of two operand. At least one operand must be a register – -No immediate operands are permitted
example
INC and DEC Instructions -Add 1, subtract 1 from destination operand, operand may be register or memory . INC destination : Logic: destination ← destination + 1 DEC destination: Logic: destination ← destination – 1
Example:
ADD Instruction ADD Instruction ADD destination, source: Logic: destination ← destination + source
Example:
SUB Instruction SUB Instructions o SUB destination, source Logic: destination ← destination – source
example
7.3.1 Addition and Subtraction Of Binary Data Format for ADD and SUB instructions: (note: there are no direct memory-to-memory operations)
NEG Instruction NEG (negate) Instruction -Reverses the sign of an operand - Operand can be a register or memory operand
example
• Implementing Arithmetic Expressions
Exercise Assume that BYTE1 is defined as DB 05, show the result for the following instructions:
Indirect Addressing -An indirect operand holds the address of a variable, usually an array or string. -It can be dereferenced (just like a pointer).
example
Arrays Array Sum Example -Indirect operands are ideal for traversing an array -The register in brackets must be incremented by a value that matches the array type
Indexed Operands Indexed operands -An indexed operand adds a constant to a register to generate an effective address. There are two notational forms: [label + reg] label[reg]
example
Pointers Pointers Declare a pointer variable that contains the offset of another variable
example
Shift instructions SHR/SAR/SHRD: Shifting Bits Right SHR – shift logical right SAR – shift arithmetic right
Example of the SHR instruction
SHL/SAL/SHLR : Shifting Bits Left
Example:
JMP and Loop Instructions JMP Instruction : JMP is an unconditional jump to a label that is usually within the same procedure . Syntax: JMP target Logic: EIP ← target JMP Example : top: . jmp top
LOOP Instruction LOOP Instruction The LOOP instruction creates a counting loop Syntax: LOOP target The assembler calculates the distance, in bytes, between the offset of the following instruction and the offset of the target label. It is called the relative offset The relative offset is added to EIP.
LOOP Example Add 1 to AX each time the loop repeats When the loop ends, AX = 5 and ECX = 0 mov ax, 0 mov ecx,5 L1: add ax loop L1
Unconditional Transfer Instructions
1. CALL and RET[n] Instructions CALL instruction is to shift control to the procedure that is called. RET[n] instruction is to return control to the procedure that made the call. Usually the RET[n] instruction is the last instruction to be executed in the called procedure. Its format:
3. INT instruction enable program to interrupt its own processing INT exits normal processing and access the Interrupt Vector Table in low memory to determine the address of requested routine. The operation then transfer to BIOS or OS for specified action and returns to the program to resume processing. Refer to section 5.5.6 for examples of INT instructions.
Conditional JUMP Instructions Format for conditional jump (Jnnn) is: There are varieties of conditional jump instructions that transfer control depending on settings in the flags register: ZF, CF, OF, AF and SF. Table 7.7.1 below shows some example of conditional jump that are used for unsigned data. The LOOP A20 statement in Table 7.5.3, can be replaced by using the conditional statement, JNZ A20 and DEC CX as in Table 7.7.2.
Table 7.7.2
conditional jump instructions usually used with other instructions: CMP, INC, DEC etc. the following are examples of conditional jump instruction:
Thanks for listening