Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 4: Data Transfer, Addressing, and Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

Similar presentations


Presentation on theme: "1 Lecture 4: Data Transfer, Addressing, and Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine."— Presentation transcript:

1

2 1 Lecture 4: Data Transfer, Addressing, and Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine

3 Outline  Data Transfer InstructionsData Transfer Instructions  Addition and SubtractionAddition and Subtraction  Data-related Operators and DirectivesData-related Operators and Directives  Indirect AddressingIndirect Addressing  JMP and LOOP InstructionsJMP and LOOP Instructions

4 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

5 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

6  Source and destination must have the same size  "reg" can be any register except  Does not change flag  Note: Memory to memory moves are not allowed Data Transfer Instructions n MOV Instruction Rules  IP  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

7 Data Transfer Instructions n MOV Instruction Examples.data bVal db 20 wVal dw 0AAAAh dVal DWORD 0FEDCBA09h.code … mov AL, 10 mov EAX, 10 mov BX, wVal mov dVal, ECX ;.386 required mov CX, bVal mov DH, 1234 ; illegal Immediate operands do not have a length attribute

8 Data Transfer Instructions n Zero/Sign Extension of Integers  MOVZX Instruction  MOVSX Instruction  Copies the contents of a source operand into a destination operand and zero-extends the value to either 16 or 32 bits  Only used with unsigned integers  Copies the contents of a source operand into a destination operand and sign-extends the value to either 16 or 32 bits  Only used with signed integers

9 Data Transfer Instructions n Zero/Sign Extension of Integers Examples.data bVal db 20 wVal SWORD 8AAAh dVal DWORD 0FEDCBA09h.code … mov AL, bVal mov BX, wVal movzx ECX, AL movSX EDX, wVal movSX,EAX, 1234 ECX=? EDX=? Anything wrong?

10 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, bData xchg wData, CX  mov's rules apply, does not change flags

11 Data Transfer Instructions n LAHF and SAHF Instructions  LAHF  SAHF  Copies the low byte of the EFLAGS register into AH  Examples  Copies AH into the low byte of the EFLAGS register.data saveflag BYTE ?.code lahf mov saveflag, ah

12 Data Transfer Instructions n Operands with displacement  Recall: to the assembler, variables names are memory offsets - number values that it can calculate with!  Example. data List word 10, 11, 12.code... mov AX, List + 2 mov [List + 4], AX With a bracket or without are both ok 0 List 1 2 List+2 3 4 List+4 5 0Ah 00 0Bh 00 0Ch 00 Data segment (initial)

13 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 AL, Msg ; AL = __ mov AL, Msg+0 ; AL = __ mov BL, Msg+1 ; BL = __ mov CL, Msg[4]; CL = __

14  Example.386.model flat.data List dd 10h, 20h, 30h, 40h X dd 2h Y dd 1234h … mov EBX, List[8]; EBX = mov EAX, List+4 ; EAX = mov ECX, X-2 ; ECX = Data Transfer Instructions n Direct-offset Operands

15 Outline  Data Transfer InstructionsData Transfer Instructions  Addition and SubtractionAddition and Subtraction  Data-related Operators and DirectivesData-related Operators and Directives  Indirect AddressingIndirect Addressing  JMP and LOOP InstructionsJMP and LOOP Instructions

16 n INC and DEC Addition and Subtraction  Used to add or subtract 1  Format inc destination dec destination where destination is a register or memory  Examples inc AX ; increment AX dec bVal ; decrement bVal  Changes flags except carry flag

17 n ADD and SUB Addition and Subtraction  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

18 n Examples for ADD and SUB Addition and Subtraction Compile x = a + b - 10 a= 10 b= 5... mov AX, a add AX, b sub AX, 10 mov x, AX ; x = __

19 n NEG Instruction Addition and Subtraction  Convert a number to its 2’s complment  Format neg reg neg mem  Examples neg AX neg bVal  Status flags are affected

20 n Flags Addition and Subtraction  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

21 n Flags Addition and Subtraction  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

22 mov AX, 10 ; AX = __, flags unchanged add AX, 20 ; AX = __, Z=0, S=0, C=0, O=0 sub AX, 31 ; AX = __, Z=_, S=_, C=_, O=_ inc AX ; AX = __, Z=_, S=_, C=_, O=_ add AX, 40000 ; AX= ____, Z=_, S=_, C=_, O=_ n Flag Examples Addition and Subtraction

23 n Flags Addition and Subtraction  The addition test for OVERFLOW  Two positive operands were added and their sum is negative  Two negative operands were added and their sum is positive  NEG – may produce an invalid result if the destination operand cannot be stored correctly Mov al, -128 neg al

24 Outline  Data Transfer InstructionsData Transfer Instructions  Addition and SubtractionAddition and Subtraction  Data-related Operators and DirectivesData-related Operators and Directives  Indirect AddressingIndirect Addressing  JMP and LOOP InstructionsJMP and LOOP Instructions

25 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

26 Data-related Operators n OFFSET Operator  Example.data List word 10h, 20h, 30h, 40h X db 2h Y dword 1234h … mov ESI, offset List[8]; ESI= mov ESI, offset X ; ESI= mov ESI, Y ; ESI = Assuming the List were located at offset 00404000h

27 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 ; ax= Inc word ptr Y mov ax, Y ; ax = ?

28 Data-related Operators n TYPE Operator  It returns the size, in bytes, of a variable:. data var2 DW 1, 2, 3 var4 DD 4.code mov BX, TYPE var2 ;BX = 2 mov BX, TYPE var4 ;BX= 4 Handy for array processing. Ex: If SI points to an element of var2, then to make SI point to the next element, we can simply write: add SI, TYPE var2

29 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 var2 DB 1, 2, 3 DB 4, 5, 6 var4 DD 5 DUP(3 DUP(?)), 10, 20, 30.code mov BX, lengthof var2 ;BX = mov AX, lengthof var4 ;AX=

30 Data-related Operators n SIZEOF Operator  Returns a value that is equivalent to multiplying LEGNTHOF by TYPE..data var2 DB 1, 2, 3 DB 4, 5, 6 var4 DD 5 DUP(3 DUP(?)), 10, 20, 30.code mov BX, sizeof var2 ;BX = mov AX, sizeof var4 ;AX=

31 Data-related Directives n ALIGN Directive  Align a variable on a byte, word, doubleword, or paragraph boundary.  If bound=1, the next variable is aligned on a 1-byte boundary  If bound=2, the next variable is aligned on an even- numbered address  If bound=4, the next address is multiple of 4  The CPU process data stored at even- numbered addresses faster than those at odd-numbered addresses Format: ALIGN bound

32 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... 0 bData 1 0A 00 Data segment wData.data bData label byte wData dw 0Ah.code mov AL, wData ; illegal mov AL, bData ; works fine mov AX, wData ; works fine  bData is just an alias for the first byte of the storage location wData

33 Outline  Data Transfer InstructionsData Transfer Instructions  Addition and SubtractionAddition and Subtraction  Data-related Operators and DirectivesData-related Operators and Directives  Indirect AddressingIndirect Addressing  JMP and LOOP InstructionsJMP and LOOP Instructions

34 Indirect Addressing n Problems  Add all word elements in an array called List.  Solution: mov AX, 0 add AX, List add AX, List+2... add AX, List+98  What if the array has 1000 elements?  Solution: use a register as a pointer and find ways to manipulate the register’s value

35 –Example: mov AX, [BX] Indirect Addressing n Notation: [reg]  REG may be EAX, EBX, ECX, EDX, ESI, EBP, and ESP to contain the offset of some data. Move the value whose address is in BX to AX Move the value pointed by BX to AX 23 25 27 29 31 33 35 600 AX = 600 BX= 25

36 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 L3: add AX, [SI] ; add value add SI, 2 ; point to next value loop L3 ; repeat as needed

37 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 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” ------ [reg1+reg2]

38 Indirect Addressing n Based and Indexed Addressing Examples.data List word … mov AX, List[SI] AX SI4 List List+2 List+4 List+6 List+8 List+10 List+ 12 Memory 100

39 mov SI, OFFSET List mov AX, 4[SI] AX SI OFFSET List List List+2 List+4 List+6 List+8 List+10 List+12 Memory 100 Indirect Addressing n Based and Indexed Addressing Examples

40 Indirect Addressing n Based and Indexed Addressing Examples mov BX, OFFSET List mov SI, 4 mov AX, [BX+SI] AX SI 4 List List+2 List+4 List+6 List+8 List+10 List+12 Memory 100 BX OFFSET List +

41 Array dw 11, 12, 13, 14, 15 dw 21, 22, 23, 24, 25 dw 31, 32, 33, 34, 35 NumCol = 5 … mov BX, NumCol mov SI, 3 mov AX, Array[BX+SI] ; mov AX, Array[BX][SI] Indirect Addressing n Based and Indexed Addressing Examples AX = ?

42 Indirect Addressing n Pointers MyString db "This is a string" pMyString dw MyString  pMyString is a word pointer to MyString. It contains the offset of MyString within the data segment.

43 Indirect Addressing n Pointer Examples Array dw 11h, 12h, 13h, 14h, 15h Y dw 21h X dw 3145h, 32h, 33h pArray dword Array pY dword Y … mov esi, pArray mov eax, 3[esi] mov esi, pY mov ebx, 2[esi]

44 Outline  Data Transfer InstructionsData Transfer Instructions  Addition and SubtractionAddition and Subtraction  Data-related Operators and DirectivesData-related Operators and Directives  Indirect AddressingIndirect Addressing  JMP and LOOP InstructionsJMP and LOOP Instructions

45 n 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)

46 JMP and LOOP Instructions n JMP  Instruction JMP targetLabel  0005 E9 0100 jmp L1 0008 100 [00] db 100h DUP (0) 0108 EB 04 L1: jmp L2 010A 01 02 03 04 db 1,2,3,4 010E B4 01 L2: mov AH, 1 0110 CD 21 int 21h 0112 A2 0002 R mov Char, AL 0115 EB F7 jmp L2  Jump calculations Old IP 0008 010A 0117 Offset +0100 + 04 +FFF7 New IP 0108 010E 010E

47 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

48 JMP and LOOP Instructions n Examples – Summing the integer array Array dw 11h, 12h, 13h, 14h, 15h … mov esi, offset Array mov eax, 0 mov ecx, lengthof Array L1: add eax, [esi] add esi, 2 loop L1 … n Exercise – backward copying a string

49 Outline  Data Transfer InstructionsData Transfer Instructions  Addition and SubtractionAddition and Subtraction  Data-related Operators and DirectivesData-related Operators and Directives  Indirect AddressingIndirect Addressing  JMP and LOOP InstructionsJMP and LOOP Instructions


Download ppt "1 Lecture 4: Data Transfer, Addressing, and Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine."

Similar presentations


Ads by Google