1 lecture 4: data transfer, addressing, and arithmetic assembly language for intel-based computers,...
Post on 19-Dec-2015
225 views
TRANSCRIPT
1
Lecture 4: Data Transfer, Addressing, and Arithmetic
Assembly Language for
Intel-Based Computers,4th edition
Kip R. Irvine
Outline
Data Transfer Instructions
Addition and Subtraction
Data-related Operators and Directives
Indirect Addressing
JMP and LOOP Instructions
Data Transfer Instructions Operand Types Immediate operands (imm): The data is a
constantRegister operands (reg): The data item is in a
register
--- Very fast. EfficientMemory operands (mem): The data item is in
memory---Slower. SEE pp. 98-99 for details
Data Transfer Instructions MOV Instruction
Copies dataGeneral format MOV destination, sourceFormats - legal operands
MOV reg, regMOV reg, memMOV mem, regMOV reg, immedMOV mem, immed
immed means a constant
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 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
Data Transfer Instructions MOV Instruction Examples
.databVal db 20wVal dw 0AAAAhdVal DWORD 0FEDCBA09h.code … mov AL, 10
mov EAX, 10 mov BX, wVal mov dVal, ECX ; .386 required mov CX, bVal mov DH, 1234 ; illegal
; illegal
Immediate operands do not have a length
attribute
Data Transfer Instructions 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
Data Transfer Instructions Zero/Sign Extension of Integers Examples
.databVal db 20wVal SWORD 8AAAhdVal DWORD 0FEDCBA09h.code … mov AL, bVal mov BX, wVal movzx ECX, AL movSX EDX, wVal movSX,EAX, 1234
ECX=?
EDX=?
Anything wrong?
Data Transfer Instructions XCHG Instruction
XCHG exchanges the contents of source and destingation
Formatxchg reg, regxchg reg, memxchg mem, reg
Examples: xchg AX, BX xchg AH, bData xchg wData, CX
mov's rules apply, does not change flags
Data Transfer Instructions 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
Data Transfer Instructions 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 List1 2 List+2
34 List+45
0Ah000Bh000Ch00
Data segment (initial)
Data Transfer Instructions Direct-offset Operands
Really just a variation of direct addressing Include + or - or even [] after variable nameExample
Msg db "abcdef"... mov AL, Msg ; AL = __ mov AL, Msg+0 ; AL = __ mov BL, Msg+1 ; BL = __ mov CL, Msg[4]; CL = __
Example.386.model flat.dataList dd 10h, 20h, 30h, 40hX dd 2hY dd 1234h …
mov EBX, List[8]; EBX = mov EAX, List+4 ; EAX = mov ECX, X-2 ; ECX =
Data Transfer Instructions Direct-offset Operands
Outline
Data Transfer Instructions
Addition and Subtraction
Data-related Operators and Directives
Indirect Addressing
JMP and LOOP Instructions
INC and DECAddition and Subtraction
Used to add or subtract 1 Format
inc destinationdec destinationwhere destination is a register or memory
Examples inc AX ; increment AX dec bVal ; decrement bVal
Changes flags except carry flag
ADD and SUB
Addition and Subtraction
Used to add or subtractadd destination, sourceAdds the source to the destinationsub destination, sourceSubtracts the source from the destination
All of "mov"s rules apply (e.g. no memory to memory operations allowed).
Status flags are affected
Examples for ADD and SUB
Addition and Subtraction
Compile x = a + b - 10a= 10b= 5 ... mov AX, a add AX, b sub AX, 10 mov x, AX ; x = __
NEG Instruction
Addition and Subtraction
Convert a number to its 2’s complment Format
neg regneg mem
Examples neg AX
neg bValStatus flags are affected
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
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
mov AX, 10 ; AX = __, flags unchangedadd AX, 20 ; AX = __, Z=0, S=0, C=0, O=0sub AX, 31 ; AX = __, Z=_, S=_, C=_, O=_inc AX ; AX = __, Z=_, S=_, C=_, O=_add AX, 40000 ; AX= ____, Z=_, S=_, C=_, O=_
Flag Examples
Addition and Subtraction
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
Outline
Data Transfer Instructions
Addition and Subtraction
Data-related Operators and Directives
Indirect Addressing
JMP and LOOP Instructions
Data-related Operators
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 OperatorExample.dataList word 10h, 20h, 30h, 40hX db 2hY 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
Data-related Operators 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 =
?
Data-related Operators TYPE Operator
It returns the size, in bytes, of a variable: .datavar2 DW 1, 2, 3var4 DD 4.code mov BX, TYPE var2 ;BX = 2 mov BX, TYPE var4 ;BX= 4Handy 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
Data-related Operators LENGTHOF Operator
Counts the number of elements in array, defined by the values appearing on the same line as its label..datavar2 DB 1, 2, 3
DB 4, 5, 6var4 DD 5 DUP(3 DUP(?)),
10, 20, 30
.code mov BX, lengthof var2 ;BX = mov AX, lengthof var4 ;AX=
Data-related Operators SIZEOF Operator
Returns a value that is equivalent to multiplying LEGNTHOF by TYPE..datavar2 DB 1, 2, 3
DB 4, 5, 6var4 DD 5 DUP(3 DUP(?)),
10, 20, 30
.code mov BX, sizeof var2 ;BX = mov AX, sizeof var4 ;AX=
Data-related Directives 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
Data-related Directives 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 bData1
0A00
Data segment
wData
.databData label bytewData 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
Outline
Data Transfer Instructions
Addition and Subtraction
Data-related Operators and Directives
Indirect Addressing
JMP and LOOP Instructions
Indirect Addressing Problems
Add all word elements in an array called List. Solution:
mov AX, 0add AX, Listadd 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
– Example: mov AX, [BX]
Indirect Addressing 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
23252729313335
600
AX = 600
BX= 25
Indirect Addressing Examples
.dataList 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 ListL3: add AX, [SI] ; add value add SI, 2 ; point to next value loop L3 ; repeat as needed
Indirect Addressing 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 constantconstant[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]
Indirect Addressing Based and Indexed Addressing
Examples
.data
List word …
mov AX, List[SI]
AX
SI 4ListList+2List+4List+6List+8List+10List+12
Memory
100
100
mov SI, OFFSET Listmov AX, 4[SI]
AX
SI OFFSET List
ListList+2List+4List+6List+8List+10List+12
Memory
100
100
Indirect Addressing Based and Indexed Addressing
Examples
Indirect Addressing Based and Indexed Addressing
Examplesmov BX, OFFSET Listmov SI, 4mov AX, [BX+SI]
AX
SI 4
ListList+2List+4List+6List+8List+10List+12
Memory
100
100
BX OFFSET List+
Array dw 11, 12, 13, 14, 15 dw 21, 22, 23, 24, 25 dw 31, 32, 33, 34, 35NumCol = 5 … mov BX, NumCol mov SI, 3 mov AX, Array[BX+SI]
; mov AX, Array[BX][SI]
Indirect Addressing Based and Indexed Addressing
Examples
AX = ?
Indirect Addressing 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.
Indirect Addressing Pointer Examples
Array dw 11h, 12h, 13h, 14h, 15hY dw 21hX dw 3145h, 32h, 33hpArray dword Array
pY dword Y … mov esi, pArray mov eax, 3[esi] mov esi, pY
mov ebx, 2[esi]
Outline
Data Transfer Instructions
Addition and Subtraction
Data-related Operators and Directives
Indirect Addressing
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)
JMP and LOOP Instructions JMP
Instruction JMP targetLabel
0005 E9 0100 jmp L10008 100 [00] db 100h DUP (0)0108 EB 04 L1: jmp L2010A 01 02 03 04 db 1,2,3,4010E B4 01 L2: mov AH, 10110 CD 21 int 21h0112 A2 0002 R mov Char, AL0115 EB F7 jmp L2
Jump calculationsOld IP 0008 010A 0117Offset +0100 + 04 +FFF7 New IP 0108 010E 010E
JMP and LOOP Instructions 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 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 … Exercise – backward copying a string
Outline
Data Transfer Instructions
Addition and Subtraction
Data-related Operators and Directives
Indirect Addressing
JMP and LOOP Instructions