Download
assembly language n.
Skip this Video
Loading SlideShow in 5 Seconds..
Assembly Language PowerPoint Presentation
Download Presentation
Assembly Language

Assembly Language

278 Vues Download Presentation
Télécharger la présentation

Assembly Language

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. 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 • MOVZX r32, r/m16 • MOVZX r16, r/m8

  9. MOVSX Instruction • Move with sign-extend • Variants • MOVSX r32, r/m8 • MOVSX r32, r/m16 • MOVSX r16, r/m8

  10. LAHF • 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 • 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 = 0000009Bh • 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 • Syntax • 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 • ADD dest, source • General variants (like MOV) • MOV reg, reg • MOV mem, reg • MOV reg, mem • MOV mem, imm • MOV reg, imm

  20. SUB Instruction • Syntax • SUB dest, source • General variants (like MOV) • 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. The sign flag is a copy of the destination's highest bit: mov al,0 sub al,1 ; AL = 11111111b, SF = 1 add al,2 ; AL = 00000001b, SF = 0 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 Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

  26. 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 Signed and Unsigned IntegersA Hardware Viewpoint Added Slide. Gerald Cahill, Antelope Valley College Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

  27. 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 OF = (carry out of the MSB) XOR (carry into the MSB) CF = INVERT (carry out of the MSB) Overflow and Carry FlagsA Hardware Viewpoint 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 0 0 0 00FFh 0 0 0 00h 0 1 1 01h 0 0 1 FFh 1 0 1 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 +127. 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 0 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 1 0 0 1 0 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/2006. 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 12345678h

  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 '12345678',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 ; 30 + 2 • 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 109.

  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) • call DumpRegs

  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) • call DumpRegs • 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 inc esi 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.