Assembly Language Data Transfers, Addressing, and Arithmetic.
-
date post
22-Dec-2015 -
Category
Documents
-
view
263 -
download
6
Transcript of Assembly Language Data Transfers, Addressing, and Arithmetic.
Assembly Language
Data Transfers, Addressing, and Arithmetic
Data Transfer Instructions
Operand Types Immediate Register memory
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples
Instruction Operand NotationInstruction Operand Notation
Direct Memory Operands For example
.data var1 BYTE 10h
.code mov al, var1 mov al, [var1] ; alternative notation
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
MOV Instruction General variants
MOV reg, reg MOV mem, reg MOV reg, mem MOV mem, imm MOV reg, imm
MOV Instruction Segment registers in real mode
MOV r/m16, sreg MOV sreg, r/m16
MOVZX Instruction Move with zero-extend Variants
MOVZX r32, r/m8 MOVZX r32, r/m16 MOVZX r16, r/m8
MOVSX Instruction Move with sign-extend Variants
MOVSX r32, r/m8 MOVSX r32, r/m16 MOVSX r16, r/m8
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
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
Direct-Offset Operands For example
mov al, [arrayB+2] mov al, array+2 ; Alternative notation
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
.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
; 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
; 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
Addition and Subtraction
INC Instruction Increment by 1 Syntax
INC reg/mem
DEC Instruction Decrement by 1 Syntax
DEC reg/mem
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
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
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
ADD and SUB Instructions Overflow, Sign, Zero, Auxiliary Carry,
Parity, and Carry flags are changed according to the value of the destination operand
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
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples
Flags Affected by ArithmeticFlags 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. Web site Examples
Zero Flag (ZF)Zero Flag (ZF)
mov cx,1sub cx,1 ; CX = 0, ZF = 1mov ax,0FFFFhinc ax ; AX = 0, ZF = 1inc ax ; AX = 1, ZF = 0
The Zero flag is set when the result of an operation produces zero in the destination operand.
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. Web site Examples
Sign Flag (SF)Sign Flag (SF)
mov cx,0sub cx,1 ; CX = -1, SF = 1add cx,2 ; CX = 1, SF = 0
The Sign flag is set when the destination operand is negative. The flag is clear when the destination is positive.
The sign flag is a copy of the destination's highest bit:
mov al,0sub al,1 ; AL = 11111111b, SF = 1add al,2 ; AL = 00000001b, SF = 0
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples
Signed and Unsigned IntegersSigned and Unsigned IntegersA Hardware ViewpointA 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. Web site Examples
Overflow and Carry FlagsOverflow and Carry FlagsA Hardware ViewpointA 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
• OF = (carry out of the MSB) XOR (carry into the MSB)
• CF = INVERT (carry out of the MSB)
Added Slide. Gerald Cahill, Antelope Valley College
MSB = Most Significant Bit (high-order bit)
XOR = eXclusive-OR operation
NEG = Negate (same as SUB 0,operand )
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples
Carry Flag (CF)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,0FFhadd al,1 ; CF = 1, AL = 00
; Try to go below zero:
mov al,0sub 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. Web site Examples
Your turn . . .Your turn . . .
mov ax,00FFhadd ax,1 ; AX= SF= ZF= CF=sub ax,1 ; AX= SF= ZF= CF=add al,1 ; AL= SF= ZF= CF=mov bh,6Chadd bh,95h ; BH= SF= ZF= CF=
mov al,2sub al,3 ; AL= SF= ZF= CF=
For each of the following marked entries, show the values of the destination operand and the Sign, Zero, and Carry flags:
0100h 0 0 000FFh 0 0 000h 0 1 1
01h 0 0 1
FFh 1 0 1
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples
Overflow Flag (OF)Overflow Flag (OF)
The Overflow flag is set when the signed result of an operation is invalid or out of range.
; Example 1mov al,+127add al,1 ; OF = 1, AL = ??
; Example 2mov al,7Fh ; OF = 1, AL = 80hadd 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. Web site Examples
A Rule of ThumbA 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,80hadd al,92h ; OF =
mov al,-2add al,+127 ; OF =
1
0
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples
Your turn . . .Your turn . . .
mov al,-128neg al ; CF = OF =
mov ax,8000hadd ax,2 ; CF = OF =
mov ax,0sub ax,2 ; CF = OF =
mov al,-5sub al,+125 ; OF =
What will be the values of the given flags after each operation?
1 1
0 0
1 0
1
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
.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
; 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
; 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
Data-Related Operators and Directives
OFFSET Returns the offset of a data label For example
mov esi, OFFSET bVal mov esi, OFFSET myArray+4
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
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
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.
LABEL Insert a label and give it a size attribute
without allocating any storage. For example
.data val16 LABEL WORD val32 DWORD 12345678h
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
; 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
.code main PROC
exit main ENDP END main
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.
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
; 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
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples
Indirect AddressingIndirect Addressing
• Indirect Operands• Array Sum Example• Indexed Operands• Pointers
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples
Indirect Operands Indirect Operands (1 of 2)(1 of 2)
.dataval1 BYTE 10h,20h,30h.codemov esi,OFFSET val1mov al,[esi] ; dereference ESI (AL = 10h)
inc esimov al,[esi] ; AL = 20h
inc esimov al,[esi] ; AL = 30h
An indirect operand holds the address of a variable, usually an array or string. It can be dereferenced (just like a pointer).
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples
Indirect Operands Indirect Operands (2 of 2)(2 of 2)
.datamyCount WORD 0
.codemov esi,OFFSET myCountinc [esi] ; error: ambiguousinc WORD PTR [esi] ; ok
Use PTR to clarify the size attribute of a memory operand.
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. Web site Examples
Array Sum ExampleArray Sum Example
.dataarrayW WORD 1000h,2000h,3000h.code
mov esi,OFFSET arrayWmov ax,[esi]add esi,2 ; or: add esi,TYPE arrayWadd ax,[esi]add esi,2add ax,[esi] ; AX = sum of the array
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.
ToDo: Modify this example for an array of doublewords.
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples
Indexed OperandsIndexed Operands
.dataarrayW WORD 1000h,2000h,3000h.code
mov esi,0mov ax,[arrayW + esi] ; AX = 1000hmov ax,arrayW[esi] ; alternate formatadd esi,2add ax,[arrayW + esi]etc.
An indexed operand adds a constant to a register to generate an effective address. There are two notational forms:
[label + reg] label[reg]
ToDo: Modify this example for an array of doublewords.
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples
Index Scaling*Index Scaling*
.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] ; 00000004
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:
* Not in the book
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples
PointersPointers
.dataarrayW WORD 1000h,2000h,3000hptrW DWORD arrayW.code
mov esi,ptrWmov ax,[esi] ; AX = 1000h
You can declare a pointer variable that contains the offset of another variable.
Alternate format:
ptrW DWORD OFFSET arrayW
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples
JMP and LOOP InstructionsJMP 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. Web site Examples
JMP InstructionJMP Instruction
top:..jmp top
• JMP is an unconditional jump to a label that is usually within the same procedure.
• Syntax: JMP target
• Logic: EIP target
• Example:
A jump outside the current procedure must be to a special type of label called a global label (see Section 5.5.2.3 for details).
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples
LOOP InstructionLOOP 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. Web site Examples
LOOP ExampleLOOP Example
00000000 66 B8 0000 mov ax,0 00000004 B9 00000005 mov ecx,5
00000009 66 03 C1 L1: add ax,cx0000000C E2 FB loop L10000000E
The following loop calculates the sum of the integers 5 + 4 + 3 +2 + 1:
When LOOP is assembled, the current location = 0000000E (offset of the next instruction). –5 (FBh) is added to the the current location, causing a jump to location 00000009:
00000009 0000000E + FB
offset machine code source code
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples
Your turn . . .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?
(a) 128
(b) +127
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples
Your turn . . .Your turn . . .
What will be the final value of AX?
mov ax,6mov ecx,4
L1:inc axloop L1
How many times will the loop execute?
mov ecx,0X2:
inc axloop X2
10
4,294,967,296
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples
Nested LoopNested 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.
.datacount DWORD ?.code
mov ecx,100 ; set outer loop countL1:
mov count,ecx ; save outer loop countmov ecx,20 ; set inner loop count
L2: ..loop L2 ; repeat the inner loopmov ecx,count ; restore outer loop countloop L1 ; repeat the outer loop
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples
Summing an Integer ArraySumming an Integer Array
.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
The following code calculates the sum of an array of 16-bit integers.
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples
Copying a StringCopying a String
.datasource BYTE "This is the source string",0target BYTE SIZEOF source DUP(0)
.codemov esi,0 ; index registermov ecx,SIZEOF source ; loop counter
L1:mov al,source[esi] ; get char from sourcemov target[esi],al ; store it in the targetinc esi ; move to next characterloop L1 ; repeat for entire string
good use of SIZEOF
The following code copies a string from source to target: