Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Transfers, Addressing, and Arithmetic

Similar presentations


Presentation on theme: "Data Transfers, Addressing, and Arithmetic"— Presentation transcript:

1 Data Transfers, Addressing, and Arithmetic
Assembly Language Data Transfers, Addressing, and Arithmetic

2 Data Transfer Instructions
Operand Types Immediate Register memory

3 Instruction Operand Notation
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

4 Direct Memory Operands
For example .data var1 BYTE 10h .code mov al, var1 mov al, [var1] ; alternative notation

5 MOV Instruction Format: MOV destination, source Rules
Both operands must be the same size Both operands cannot be memory operands CS, EIP, and IP cannot be destination operands

6 MOV Instruction General variants MOV reg, reg MOV mem, reg
MOV reg, mem MOV mem, imm MOV reg, imm

7 MOV Instruction Segment registers in real mode MOV r/m16, sreg
MOV sreg, r/m16

8 MOVZX Instruction Move with zero-extend Variants MOVZX r32, r/m8

9 MOVSX Instruction Move with sign-extend Variants MOVSX r32, r/m8

10 LAHF SAHF Load the low byte of the EFLAGS register into AH
Includes Sign, Zero, Auxiliary Carry, Parity, and Carry. SAHF Store AH into the low byte of the EFLAGS register

11 XCHG Instruction Rules Variants Both operands must be the same size
Both operands cannot be memory operands CS, EIP, and IP cannot be destination operands Variants XCHG reg, reg XCHG reg, mem XCHG mem, reg

12 Direct-Offset Operands
For example mov al, [arrayB+2] mov al, array+2 ; Alternative notation

13 Example: Data Transfer
TITLE Data Transfer Examples (Moves.asm) ; Chapter 4 example. Demonstration of MOV and ; XCHG with direct and direct-offset operands. ; Last update: 06/01/2006. By K. R. Irvine. INCLUDE Irvine32.inc .data val1 WORD 1000h val2 WORD 2000h arrayB BYTE 10h,20h,30h,40h,50h arrayW WORD 100h,200h,300h arrayD DWORD 10000h,20000h

14 .code main PROC ; MOVZX mov bx,0A69Bh movzx eax,bx ; EAX = 0000A69Bh movzx edx,bl ; EDX = Bh movzx cx,bl ; CX = 009Bh ; MOVSX mov bx,0A69Bh movsx eax,bx ; EAX = FFFFA69Bh movsx edx,bl ; EDX = FFFFFF9Bh mov bl,7Bh movsx cx,bl ; CX = 007Bh

15 ; Memory-to-memory exchange:
mov ax,val1 ; AX = 1000h xchg ax,val2 ; AX = 2000h, val2 = 1000h mov val1,ax ; val1 = 2000h ; Direct-Offset Addressing (byte array): mov al,arrayB ; AL = 10h mov al,[arrayB+1] ; AL = 20h mov al,[arrayB+2] ; AL = 30h

16 ; Direct-Offset Addressing (word array):
mov ax,arrayW ; AX = 100h mov ax,[arrayW+2] ; AX = 200h ; Direct-Offset Addressing (doubleword array): mov eax,arrayD ; EAX = 10000h mov eax,[arrayD+4] ; EAX = 20000h mov eax,[arrayD+TYPE arrayD] ; EAX = 20000h exit main ENDP END main

17 Addition and Subtraction
INC Instruction Increment by 1 Syntax INC reg/mem DEC Instruction Decrement by 1 DEC reg/mem

18 INC and DEC Instruction
Overflow, Sign, Zero, Auxiliary Carry, and Parity flags are changed according to the value of the destination operand Do not affect the Carry flag

19 ADD Instruction Syntax General variants (like MOV) ADD dest, source
MOV reg, reg MOV mem, reg MOV reg, mem MOV mem, imm MOV reg, imm

20 SUB Instruction Syntax General variants (like MOV) SUB dest, source
MOV reg, reg MOV mem, reg MOV reg, mem MOV mem, imm MOV reg, imm

21 ADD and SUB Instructions
Overflow, Sign, Zero, Auxiliary Carry, Parity, and Carry flags are changed according to the value of the destination operand

22 NEG Instruction Convert the number to its two’s complement Syntax
NEG reg NEG mem Overflow, Sign, Zero, Auxiliary Carry, Parity, and Carry flags are changed according to the value of the destination operand

23 Flags Affected by Arithmetic
The ALU has a number of status flags that reflect the outcome of arithmetic (and bitwise) operations based on the contents of the destination operand Essential flags: Zero flag – set when destination equals zero Sign flag – set when destination is negative Carry flag – set when unsigned value is out of range Overflow flag – set when signed value is out of range The MOV instruction never affects the flags. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

24 Zero Flag (ZF) The Zero flag is set when the result of an operation produces zero in the destination operand. mov cx,1 sub cx,1 ; CX = 0, ZF = 1 mov ax,0FFFFh inc ax ; AX = 0, ZF = 1 inc ax ; AX = 1, ZF = 0 Remember... A flag is set when it equals 1. A flag is clear when it equals 0. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

25 Sign Flag (SF) The Sign flag is set when the destination operand is negative. The flag is clear when the destination is positive. mov cx,0 sub cx,1 ; CX = -1, SF = 1 add cx,2 ; CX = 1, SF = 0 The sign flag is a copy of the destination's highest bit: mov al,0 sub al, ; AL = b, SF = 1 add al, ; AL = b, SF = 0 Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

26 Signed and Unsigned Integers A Hardware Viewpoint
All CPU instructions operate exactly the same on signed and unsigned integers The CPU cannot distinguish between signed and unsigned integers YOU, the programmer, are solely responsible for using the correct data type with each instruction Added Slide. Gerald Cahill, Antelope Valley College Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

27 Overflow and Carry Flags A Hardware Viewpoint
How the ADD instruction modifies OF and CF: OF = (carry out of the MSB) XOR (carry into the MSB) CF = (carry out of the MSB) How the SUB instruction modifies OF and CF: NEG the source and ADD it to the destination CF = INVERT (carry out of the MSB) MSB = Most Significant Bit (high-order bit) XOR = eXclusive-OR operation NEG = Negate (same as SUB 0,operand ) Added Slide. Gerald Cahill, Antelope Valley College Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

28 Carry Flag (CF) The Carry flag is set when the result of an operation generates an unsigned value that is out of range (too big or too small for the destination operand). mov al,0FFh add al,1 ; CF = 1, AL = 00 ; Try to go below zero: mov al,0 sub al,1 ; CF = 1, AL = FF The INC and DEC instructions do not affect the Carry flag. Applying NEG to a nonzero operand always sets the Carry flag. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

29 Your turn . . . For each of the following marked entries, show the values of the destination operand and the Sign, Zero, and Carry flags: mov ax,00FFh add ax,1 ; AX= SF= ZF= CF= sub ax,1 ; AX= SF= ZF= CF= add al,1 ; AL= SF= ZF= CF= mov bh,6Ch add bh,95h ; BH= SF= ZF= CF= mov al,2 sub al,3 ; AL= SF= ZF= CF= 0100h 00FFh 00h 01h FFh Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

30 Overflow Flag (OF) The Overflow flag is set when the signed result of an operation is invalid or out of range. ; Example 1 mov al,+127 add al,1 ; OF = 1, AL = ?? ; Example 2 mov al,7Fh ; OF = 1, AL = 80h add al,1 The two examples are identical at the binary level because 7Fh equals To determine the value of the destination operand, it is often easier to calculate in hexadecimal. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

31 A Rule of Thumb When adding two integers, remember that the Overflow flag is only set when . . . Two positive operands are added and their sum is negative Two negative operands are added and their sum is positive What will be the values of the Overflow flag? mov al,80h add al,92h ; OF = mov al,-2 add al,+127 ; OF = 1 Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

32 Your turn . . . What will be the values of the given flags after each operation? mov al,-128 neg al ; CF = OF = mov ax,8000h add ax,2 ; CF = OF = mov ax,0 sub ax,2 ; CF = OF = mov al,-5 sub al,+125 ; OF = 1 Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

33 Example: Addition and Subtraction
TITLE Addition and Subtraction (AddSub3.asm) ; Chapter 4 example. Demonstration of ADD, SUB, ; INC, DEC, and NEG instructions, and how ; they affect the CPU status flags. ; Last update: 06/01/ By K. R. Irvine. INCLUDE Irvine32.inc .data Rval SDWORD ? Xval SDWORD 26 Yval SDWORD 30 Zval SDWORD 40

34 .code main PROC ; INC and DEC mov ax,1000h inc ax ; 1001h dec ax ; 1000h ; Expression: Rval = -Xval + (Yval - Zval) mov eax,Xval neg eax ; -26 mov ebx,Yval sub ebx,Zval ; -10 add eax,ebx mov Rval,eax ; -36

35 ; Zero flag example: mov cx,1 sub cx,1 ; ZF = 1 mov ax,0FFFFh inc ax ; ZF = 1 ; Sign flag example: mov cx,0 sub cx,1 ; SF = 1 mov ax,7FFFh add ax,2 ; SF = 1

36 ; Carry flag example: mov al,0FFh add al,1 ; CF = 1, AL = 00 ; Overflow flag example: mov al,+127 add al,1 ; OF = 1 mov al,-128 sub al,1 ; OF = 1 exit main ENDP END main

37 Data-Related Operators and Directives
OFFSET Returns the offset of a data label For example mov esi, OFFSET bVal mov esi, OFFSET myArray+4

38 ALIGN Aligns a variable on a byte, word, doubleword, or paragraph boundary Syntax ALIGN bound bound can be 1, 2, 4, or 16. For example bVal BYTE ? ALIGN 2

39 PTR Override the declared size of an operand For example
mov ax, WORD PTR myDouble mov ax, WORD PTR [myDouble+2] mov bl, BYTE PTR myDouble

40 TYPE Returns the size, in bytes, of a single element of a variable. LENGTHOF Counts the number of elements in an array, defined by the values appearing on the same line as its label. SIZEOF Returns a value that is equavalent to multiplying LENGTHOF by TYPE.

41 LABEL Insert a label and give it a size attribute without allocating any storage. For example .data val16 LABEL WORD val32 DWORD h

42 Example: TYPE, LENGTHOF
TITLE Operators (Operator.asm) ; Demonstrates the TYPE, LENGTHOF, and SIZEOF operators ; Last update: 06/01/2006. By K. R. Irvine. INCLUDE Irvine32.inc .data byte1 BYTE 10,20,30 array1 WORD 30 DUP(?),0,0 array2 WORD 5 DUP(3 DUP(?)) array3 DWORD 1,2,3,4 digitStr BYTE ' ',0 myArray BYTE 10,20,30,40,50, 60,70,80,90,100

43 ; You can examine the following constant values
; by looking in the listing file (Operator.lst): ; X = LENGTHOF byte1 ; 3 X = LENGTHOF array1 ; X = LENGTHOF array2 ; 5 * 3 X = LENGTHOF array3 ; 4 X = LENGTHOF digitStr ; 9 X = LENGTHOF myArray ; 10 X = SIZEOF byte1 ; 1 * 3 X = SIZEOF array1 ; 2 * (30 + 2) X = SIZEOF array2 ; 2 * (5 * 3) X = SIZEOF array3 ; 4 * 4 X = SIZEOF digitStr ; 1 * 9

44 .code main PROC exit main ENDP END main

45 Exercise 4.7, 4. Overflow Flag
Write a program that uses addition and subtraction to set and clear the Overflow flag. After each addition or subtraction instruction, insert the call DumpRegs statement to display the registers and flags. Using comments, explain how (and why) the Overflow flag was affected by each instruction. Optional: include an ADD instruction that sets both the Carry and Overflow flags. Reference: Page 92.

46 Example TITLE Chapter 4 Exercise 2 INCLUDE Irvine32.inc .data .code
main PROC ; INC does not affect the carry flag: mov al,254 add al,1 ; AL=255, CF=0 call DumpRegs inc al ; AL=0, CF=0 (unchanged)

47 ; DEC does not affect the carry flag:
mov al,1 sub al,1 ; AL=0, CF=0 call DumpRegs dec al ; AL=255, CF=0 (unchanged) exit main ENDP END main

48 Indirect Addressing Indirect Operands Array Sum Example
Indexed Operands Pointers Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

49 Indirect Operands (1 of 2)
An indirect operand holds the address of a variable, usually an array or string. It can be dereferenced (just like a pointer). .data val1 BYTE 10h,20h,30h .code mov esi,OFFSET val1 mov al,[esi] ; dereference ESI (AL = 10h) inc esi mov al,[esi] ; AL = 20h mov al,[esi] ; AL = 30h Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

50 Indirect Operands (2 of 2)
Use PTR to clarify the size attribute of a memory operand. .data myCount WORD 0 .code mov esi,OFFSET myCount inc [esi] ; error: ambiguous inc WORD PTR [esi] ; ok Should PTR be used here? add [esi],20 yes, because [esi] could point to a byte, word, or doubleword Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

51 Array Sum Example Indirect operands are ideal for traversing an array. Note that the register in brackets must be incremented by a value that matches the array type. .data arrayW WORD 1000h,2000h,3000h .code mov esi,OFFSET arrayW mov ax,[esi] add esi,2 ; or: add esi,TYPE arrayW add ax,[esi] add esi,2 add ax,[esi] ; AX = sum of the array ToDo: Modify this example for an array of doublewords. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

52 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] .data arrayW WORD 1000h,2000h,3000h .code mov esi,0 mov ax,[arrayW + esi] ; AX = 1000h mov ax,arrayW[esi] ; alternate format add esi,2 add ax,[arrayW + esi] etc. ToDo: Modify this example for an array of doublewords. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

53 Index Scaling* You can scale an indirect or indexed operand to the offset of an array element. This is done by multiplying the index by the array's TYPE: .data arrayB BYTE 0,1,2,3,4,5 arrayW WORD 0,1,2,3,4,5 arrayD DWORD 0,1,2,3,4,5 .code mov esi,4 mov al,arrayB[esi*TYPE arrayB] ; 04 mov bx,arrayW[esi*TYPE arrayW] ; 0004 mov edx,arrayD[esi*TYPE arrayD] ; * Not in the book Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

54 Pointers You can declare a pointer variable that contains the offset of another variable. .data arrayW WORD 1000h,2000h,3000h ptrW DWORD arrayW .code mov esi,ptrW mov ax,[esi] ; AX = 1000h Alternate format: ptrW DWORD OFFSET arrayW Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

55 JMP and LOOP Instructions
JMP Instruction LOOP Instruction LOOP Example Summing an Integer Array Copying a String Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

56 JMP Instruction JMP is an unconditional jump to a label that is usually within the same procedure. Syntax: JMP target Logic: EIP  target Example: top: . jmp top A jump outside the current procedure must be to a special type of label called a global label (see Section for details). Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

57 LOOP Instruction The LOOP instruction creates a counting loop
Syntax: LOOP target Logic: ECX  ECX – 1 if ECX != 0, jump to target Implementation: 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. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

58 LOOP Example The following loop calculates the sum of the integers : offset machine code source code B mov ax,0 B mov ecx,5 C1 L1: add ax,cx C E2 FB loop L1 E When LOOP is assembled, the current location = E (offset of the next instruction). –5 (FBh) is added to the the current location, causing a jump to location :  E + FB Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

59 Your turn . . . If the relative offset is encoded in a single signed byte, (a) what is the largest possible backward jump? (b) what is the largest possible forward jump? -128 +127 Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

60 Your turn . . . What will be the final value of AX? 10
mov ax,6 mov ecx,4 L1: inc ax loop L1 What will be the final value of AX? 10 How many times will the loop execute? mov ecx,0 X2: inc ax loop X2 4,294,967,296 Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

61 Nested Loop If you need to code a loop within a loop, you must save the outer loop counter's ECX value. In the following example, the outer loop executes 100 times, and the inner loop 20 times. .data count DWORD ? .code mov ecx,100 ; set outer loop count L1: mov count,ecx ; save outer loop count mov ecx,20 ; set inner loop count L2: . . loop L2 ; repeat the inner loop mov ecx,count ; restore outer loop count loop L1 ; repeat the outer loop Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

62 Summing an Integer Array
The following code calculates the sum of an array of 16-bit integers. .data intarray WORD 100h,200h,300h,400h .code mov edi,OFFSET intarray ; address of intarray mov ecx,LENGTHOF intarray ; loop counter mov ax,0 ; zero the accumulator L1: add ax,[edi] ; add an integer add edi,TYPE intarray ; point to next integer loop L1 ; repeat until ECX = 0 Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

63 Copying a String The following code copies a string from source to target: .data source BYTE "This is the source string",0 target BYTE SIZEOF source DUP(0) .code mov esi,0 ; index register mov ecx,SIZEOF source ; loop counter L1: mov al,source[esi] ; get char from source mov target[esi],al ; store it in the target inc esi ; move to next character loop L1 ; repeat for entire string good use of SIZEOF Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.


Download ppt "Data Transfers, Addressing, and Arithmetic"

Similar presentations


Ads by Google