Lecture 4 Data Transfer Addressing and Arithmetic Assembly
Lecture 4: Data Transfer, Addressing, and Arithmetic Assembly Language for Intel-Based Computers, 4 th edition Kip R. Irvine 1
Outline q Data Transfer Instructions q Addition and Subtraction q Data-related Operators and Directives q Indirect Addressing q JMP and LOOP Instructions
Data Transfer Instructions n Operand Types Ø Immediate operands (imm): The data is a constant Ø Register operands (reg): The data item is in a register --- Very fast. Efficient Ø Memory operands (mem): The data item is in memory ---Slower. SEE pp. 98 -99 for details
Data Transfer Instructions n MOV Instruction Ø Copies data Ø General format MOV destination, source Ø Formats - legal operands MOV reg, reg MOV reg, mem MOV mem, reg MOV reg, immed MOV mem, immed Ø immed means a constant
Data Transfer Instructions n MOV Instruction Rules Ø Source and destination must have the same size Ø "reg" can be any register except IP q Segment registers use special rules - to be used only when the program runs in real mode - CS cannot be a target operand - immediate values are not allowed q Ø Note: Memory to memory moves are not allowed Ø Does not change flag
Data Transfer Instructions n MOV Instruction Examples . data b. Val db 20 w. Val dw 0 AAAAh d. Val DWORD 0 FEDCBA 09 h. code Immediate operands do not have a length … attribute mov AL, 10 mov EAX, 10 mov BX, w. Val mov d. Val, ECX ; . 386 required mov CX, b. Val ; illegal mov DH, 1234 ; illegal
Data Transfer Instructions n Zero/Sign Extension of Integers Ø MOVZX Instruction q Copies the contents of a source operand into a destination operand zero-extends the value to either 16 or 32 bits q Only used with unsigned integers Ø MOVSX Instruction q Copies the contents of a source operand into a destination operand sign-extends the value to either 16 or 32 bits q Only used with signed integers
Data Transfer Instructions n Zero/Sign Extension of Integers Examples. data b. Val db 20 w. Val SWORD 8 AAAh d. Val DWORD 0 FEDCBA 09 h. code … mov AL, b. Val ECX=? mov BX, w. Val movzx ECX, AL EDX=? mov. SX EDX, w. Val Anything wrong? mov. SX, EAX, 1234
Data Transfer Instructions n XCHG Instruction Ø XCHG exchanges the contents of source and destingation Ø Format xchg reg, reg xchg reg, mem xchg mem, reg Ø Examples: xchg AX, BX xchg AH, b. Data xchg w. Data, CX Ø mov's rules apply, does not change flags
Data Transfer Instructions n LAHF and SAHF Instructions Ø LAHF q Copies the low byte of the EFLAGS register into AH. data q Examples saveflag BYTE ? . code lahf mov saveflag, ah Ø SAHF q Copies register AH into the low byte of the EFLAGS
Data Transfer Instructions n Operands with displacement Ø Recall: to the assembler, variables names are memory offsets - number values that it can calculate with! Data segment (initial) Ø Example. data List word 10, 11, 12. code. . . mov AX, List + 2 mov [List + 4], AX 0 1 2 3 4 5 0 Ah 00 0 Bh 00 0 Ch 00 List+2 List+4 With a bracket or without are both ok
Data Transfer Instructions n Direct-offset Operands Ø Really just a variation of direct addressing Ø Include + or - or even [] after variable name Ø Example Msg. . . db "abcdef" mov mov AL, BL, CL, Msg ; Msg+0 ; Msg+1 ; Msg[4]; AL AL BL CL = = __ __
Data Transfer Instructions n Direct-offset Operands Ø Example . 386. model flat. data List dd 10 h, 20 h, 30 h, 40 h X dd 2 h Y dd 1234 h … mov EBX, List[8]; EBX = mov EAX, List+4 ; EAX = mov ECX, X-2 ; ECX =
Outline q Data Transfer Instructions q Addition and Subtraction q Data-related Operators and Directives q Indirect Addressing q JMP and LOOP Instructions
Addition and Subtraction n INC and DEC Ø Used to add or subtract 1 Ø Format inc destination dec destination where destination is a register or memory Ø Examples inc dec AX b. Val ; increment AX ; decrement b. Val Ø Changes flags except carry flag
Addition and Subtraction n ADD and SUB Ø Used to add or subtract add destination, source Adds the source to the destination sub destination, source Subtracts the source from the destination Ø All of "mov"s rules apply (e. g. no memory to memory operations allowed). Ø Status flags are affected
Addition and Subtraction n Examples for ADD and SUB Compile a= 10 b= 5. . . x = a + b - 10 mov add sub mov AX, a AX, b AX, 10 x, AX ; x = __
Addition and Subtraction n NEG Instruction Ø Convert a number to its 2’s complment Ø Format neg reg neg mem Ø Examples neg AX b. Val Ø Status flags are affected
Addition and Subtraction n Flags Ø Several flags are set after the arithmetic operations ADD, SUB, INC, and DEC Ø The CPU does not know if the calculations are signed or unsigned so both sets of flags are set Ø Zero flag is set to 1 if the result of the calculation is 0, cleared to 0 if the result is nonzero
Addition and Subtraction n Flags Ø The negative flag is set to the leading bit of the result Ø Unsigned arithmetic: The carry flag is set if the result is too large or too small as a unsigned number Ø Signed arithmetic: The overflow flag is set if the result is too large or too small as a signed number
Addition and Subtraction n Flag Examples mov add sub inc add AX, AX, AX AX, 10 ; AX = __, flags unchanged 20 ; AX = __, Z=0, S=0, C=0, O=0 31 ; AX = __, Z=_, S=_, C=_, O=_ 40000 ; AX= ____, Z=_, S=_, C=_, O=_
Addition and Subtraction n Flags Ø The addition test for OVERFLOW Two positive operands were added and their sum is negative q Two negative operands were added and their sum is positive q Ø NEG – may produce an invalid result if the destination operand cannot be stored correctly Mov al, -128 neg al
Outline q Data Transfer Instructions q Addition and Subtraction q Data-related Operators and Directives q Indirect Addressing q JMP and LOOP Instructions
Data-related Operators n OFFSET Operator Ø Return the offset of a data label. The offset represents the distance, in bytes, of the label from the beginning of the data segment Ø An offset is 32 -bit for the protected mode and 16 -bit for the real mode
Data-related Operators OFFSET Operator Ø Example n . data List word 10 h, 20 h, 30 h, 40 h X db 2 h Y dword 1234 h … mov ESI, offset List[8]; ESI= mov ESI, offset X ; ESI= mov ESI, Y ; ESI = Assuming the List were located at offset 00404000 h
Data-related Operators n PTR Operator Ø PTR – override the default size of an operand Ø Example code: Y db FFh X dw 20, 13 … inc byte ptr Y mov ax, Y ; Inc word ptr Y mov ax, Y ; ? ax= ax =
Data-related Operators n TYPE Operator Ø It returns the size, in bytes, of a variable: . data var 2 DW 1, 2, 3 var 4 DD 4. code mov BX, TYPE var 2 ; BX = 2 mov BX, TYPE var 4 ; BX= 4 Handy for array processing. Ex: If SI points to an element of var 2, then to make SI point to the next element, we can simply write: add SI, TYPE var 2
Data-related Operators n LENGTHOF Operator Ø Counts the number of elements in array, defined by the values appearing on the same line as its label. . data var 2 DB 1, 2, 3 DB 4, 5, 6 var 4 DD 5 DUP(3 DUP(? )), 10, 20, 30. code mov BX, lengthof var 2 ; BX = mov AX, lengthof var 4 ; AX=
Data-related Operators n SIZEOF Operator Ø Returns a value that is equivalent to multiplying LEGNTHOF by TYPE. . data var 2 DB 1, 2, 3 DB 4, 5, 6 var 4 DD 5 DUP(3 DUP(? )), 10, 20, 30. code mov BX, sizeof var 2 ; BX = mov AX, sizeof var 4 ; AX=
Data-related Directives n ALIGN Directive Ø Align a variable on a byte, word, doubleword, or paragraph boundary. Format: ALIGN bound If bound=1, the next variable is aligned on a 1 -byte boundary q If bound=2, the next variable is aligned on an evennumbered address q If bound=4, the next address is multiple of 4 q Ø The CPU process data stored at even- numbered addresses faster than those at odd-numbered addresses
Data-related Directives n LABEL Directive Ø It gives a name and a size to an existing storage location. It does not allocate storage. Ø It must be used in conjunction with byte, word, dword, qword. . data Data segment b. Data label byte 0 0 A b. Data w. Data dw 0 Ah 1 00 w. Data. code mov AL, w. Data ; illegal mov AL, b. Data ; works fine mov AX, w. Data ; works fine Ø b. Data is just an alias for the first byte of the storage location w. Data
Outline q Data Transfer Instructions q Addition and Subtraction q Data-related Operators and Directives q Indirect Addressing q JMP and LOOP Instructions
Indirect Addressing n Problems Ø Add all word elements in an array called List. mov AX, 0 add Ø What if the array has. . . add 1000 elements? q Solution: AX, List+2 AX, List+98 use a register as a pointer and find ways to manipulate the register’s value
Indirect Addressing n Notation: [reg] Ø REG may be EAX, EBX, ECX, EDX, ESI, EBP, and ESP to contain the offset of some data. – Example: mov AX, [BX] • Move the value whose address is in BX to AX • Move the value pointed by BX to AX BX= 25 AX = 600 23 25 600 27 29 31 33 35
Indirect Addressing n Examples. data List dw 1, 3, 10, 6, 2, 9, 2, 8, 9 Number = ($ - List)/2. code … ; sum values in list mov AX, 0 ; sum = 0 mov CX, Number ; number of values mov SI, OFFSET List ; ptr to List L 3: add AX, [SI] ; add value add SI, 2 ; point to next value loop L 3 ; repeat as needed
Indirect Addressing n Based and Indexed Addressing Ø A displacement (constant) is added to the base or indexed value to get the offset Ø Notation: Register added to offset variable[reg] Good notation if reg holds [reg+variable] the "subscript". [variable+reg] Ø Notation: Register added to constant[reg] Good notations if the register [reg+constant] holds the offset of the [constant+reg] variable Ø A register holds the offset and the other holds the “subscript” ------ [reg 1+reg 2]
Indirect Addressing n Based and Indexed Addressing Examples. data List word … Memory mov AX, List[SI] SI 4 AX 100 List+2 100 List+4 List+6 List+8 List+10 List+12
Indirect Addressing n Based and Indexed Addressing Examples mov SI, OFFSET List mov AX, 4[SI] List SI OFFSET List AX 100 List+2 List+4 List+6 List+8 List+10 List+12 Memory 100
Indirect Addressing n Based and Indexed Addressing Examples mov BX, OFFSET List mov SI, 4 mov AX, [BX+SI] BX OFFSET List + SI 4 AX 100 List+2 List+4 List+6 List+8 List+10 List+12 Memory 100
Indirect Addressing n Based and Indexed Addressing Examples Array dw 11, 12, 13, 14, 15 dw 21, 22, 23, 24, 25 dw 31, 32, 33, 34, 35 Num. Col = 5 … mov BX, Num. Col mov SI, 3 mov AX, Array[BX+SI] ; mov AX, Array[BX][SI] AX = ?
Indirect Addressing n Pointers My. String db "This is a string" p. My. String dw My. String Ø p. My. String is a word pointer to My. String. It contains the offset of My. String within the data segment.
Indirect Addressing n Pointer Examples Array dw 11 h, 12 h, 13 h, 14 h, 15 h Y dw 21 h X dw 3145 h, 32 h, 33 h p. Array dword Array p. Y dword Y … mov esi, p. Array mov eax, 3[esi] mov esi, p. Y mov ebx, 2[esi]
Outline q Data Transfer Instructions q Addition and Subtraction q Data-related Operators and Directives q Indirect Addressing q JMP and LOOP Instructions
JMP and LOOP Instructions Transfer of Control ¨ Unconditional branch: The new location is always loaded into the IP. Example: JMP (jump) ¨ Conditional branch: The new location is loaded into the IP only if some condition is satisfied. Example: JZ (jump if zero) n
JMP and LOOP Instructions n JMP Ø Instruction JMP target. Label ¨ 0005 E 9 0100 jmp L 1 0008 100 [00] db 100 h DUP (0) 0108 EB 04 L 1: jmp L 2 010 A 01 02 03 04 db 1, 2, 3, 4 010 E B 4 01 L 2: mov AH, 1 0110 CD 21 int 21 h 0112 A 2 0002 R mov Char, AL 0115 EB F 7 jmp L 2 ¨ Jump calculations Old IP 0008 010 A 0117 Offset +0100 + 04 +FFF 7 New IP 0108 010 E
JMP and LOOP Instructions n LOOP, LOOPW, and LOOPD Ø The loop instructions are the easiest way to set up a loop Ø They use CX or ECX as the counter Ø Action: decrement CX or ECX. Jump if the new register value is not 0 Ø Offset is one byte long (-128 to +127) Ø LOOP uses CX if in 16 bit mode, ECX if in 32 bit mode Ø LOOPW uses CX, LOOPD uses ECX
JMP and LOOP Instructions n Examples – Summing the integer array Array dw … 11 h, 12 h, 13 h, 14 h, 15 h mov esi, offset Array mov eax, 0 mov ecx, lengthof Array L 1: add eax, [esi] add esi, 2 loop L 1 … n Exercise – backward copying a string
Outline q Data Transfer Instructions q Addition and Subtraction q Data-related Operators and Directives q Indirect Addressing q JMP and LOOP Instructions
- Slides: 48