04 Instructions&Addressing 6pdf
04 Instructions&Addressing 6pdf
Basic Instructions
Addressing Modes Operand Types
Data Transfer Instructions
Computer Organization Addition and Subtraction
& Addressing Modes
Assembly Language Programming Jump and Loop Instructions
Copying a String
Summing an Array of Integers
Dr Adnan Gutub
aagutub at uqu.edu.sa
[Adapted from slides of Dr. Kip Irvine: Assembly Language for Intel-Based Computers]
Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55
Most Slides contents have been arranged by Dr Muhamed Mudawar & Dr Aiman El-Maleh from Computer Engineering Dept. at KFUPM
Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55 Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55
1
MOV Examples Zero Extension
.DATA MOVZX Instruction
count BYTE 100
bVal BYTE 20 Fills (extends) the upper part of the destination with zeros
wVal WORD 2
Used to copy a small source into a larger destination
dVal DWORD 5
.CODE Destination must be a register
mov bl, count ; bl = count = 100 movzx r32, r/m8
mov ax, wVal ; ax = wVal = 2
mov count,al ; count = al = 2 movzx r32, r/m16
mov eax, dval ; eax = dval = 5 movzx r16, r/m8
; Assembler will not accept the following moves why?
0 10001111 Source
mov ds, 45 ; immediate move to DS not permitted
mov esi, wVal ; size mismatch
mov eip, dVal ; EIP cannot be the destination
mov bl, 8Fh
mov 25, bVal ; immediate value cannot be destination
mov bVal,count ; memory-to-memory move not permitted
00000000 10001111 Destination movzx ax, bl
Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55 Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55
Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55 Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55
Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55 Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55
2
Direct-Offset Operands - Examples Your Turn . . .
.DATA Given the following definition of arrayD
arrayW WORD 1020h, 3040h, 5060h
.DATA
arrayD DWORD 1, 2, 3, 4
arrayD DWORD 1,2,3
.CODE
mov ax, arrayW+2 ; AX = 3040h Rearrange the three values in the array as: 3, 1, 2
mov ax, arrayW[4] ; AX = 5060h
mov eax,[arrayD+4] ; EAX = 00000002h Solution:
mov eax,[arrayD-3] ; EAX = 01506030h ; Copy first array value into EAX
mov ax, [arrayW+9] ; AX = 0200h
mov eax, arrayD ; EAX = 1
mov ax, [arrayD+3] ; Error: Operands are not same size
mov ax, [arrayW-2]
; Exchange EAX with second array element
; AX = ? Out-of-range address
mov eax,[arrayD+16] ; EAX = ? MASM does not detect error xchg eax, arrayD[4] ; EAX = 2, arrayD = 1,1,3
; Exchange EAX with third array element
1020 3040 5060 1 2 3 4 xchg eax, arrayD[8] ; EAX = 3, arrayD = 1,1,2
20 10 40 30 60 50 01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 ; Copy value in EAX to first array element
+1 +2 +3 +4 +5 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 mov arrayD, eax ; arrayD = 3,1,2
arrayW arrayD
Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55 Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55
Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55 Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55
Solution: Accumulate the sum in the AX register ADD and SUB affect all the six status flags:
mov ax, array
add ax,[array+2] 1. Carry Flag: Set when unsigned arithmetic result is out of range
add ax,[array+4] ; what if sum cannot fit in AX? 2. Overflow Flag: Set when signed arithmetic result is out of range
Solution 2: Accumulate the sum in the EAX register 3. Sign Flag: Copy of sign bit, set when result is negative
movzx eax, array ; error to say: mov eax,array
4. Zero Flag: Set when result is zero
movzx ebx, array[2] ; use movsx for signed integers
add eax, ebx ; error to say: add eax,array[2] 5. Auxiliary Carry Flag: Set when there is a carry from bit 3 to bit 4
movzx ebx, array[4]
add eax, ebx 6. Parity Flag: Set when parity in least-significant byte is even
Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55 Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55
3
More on Carry and Overflow Hardware Viewpoint
Addition: A + B CPU cannot distinguish signed from unsigned integers
The Carry flag is the carry out of the most significant bit YOU, the programmer, give a meaning to binary numbers
The Overflow flag is only set when . . . How the ADD instruction modifies OF and CF:
Two positive operands are added and their sum is negative CF = (carry out of the MSB) MSB = Most Significant Bit
Two negative operands are added and their sum is positive
OF = (carry out of the MSB) XOR (carry into the MSB)
Overflow cannot occur when adding operands of opposite signs
Hardware does SUB by XOR = eXclusive-OR operation
Subtraction: A B
ADDing destination to the 2's complement of the source operand
For Subtraction, the carry flag becomes the borrow flag
How the SUB instruction modifies OF and CF:
Carry flag is set when A has a smaller unsigned value than B
Negate (2's complement) the source and ADD it to destination
The Overflow flag is only set when . . .
A and B have different signs and sign of result sign of A OF = (carry out of the MSB) XOR (carry into the MSB)
Overflow cannot occur when subtracting operands of the same sign CF = INVERT (carry out of the MSB)
Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55 Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55
0 0 1 0 0 1 1 0 26h (38) 0 0 1 0 0 1 1 0 26h (38) Destination can be 8-, 16-, or 32-bit operand
+
1 0 0 1 0 1 0 1 95h (-107) 0 1 1 0 1 0 1 1 6Bh (107) In memory or a register
1 0 0 1 0 0 0 1 91h (-111) 1 0 0 1 0 0 0 1 91h (-111) NO immediate operand
Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55 Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55
4
Extended Arithmetic Next . . .
ADC and SBB are useful for extended arithmetic
Example: 64-bit addition Operand Types
Assume first 64-bit integer operand is stored in EBX:EAX Data Transfer Instructions
Second 64-bit integer operand is stored in EDX:ECX Addition and Subtraction
Solution: Addressing Modes
add eax, ecx ;add lower 32 bits Jump and Loop Instructions
adc ebx, edx ;add upper 32 bits + carry Copying a String
64-bit result is in EBX:EAX Summing an Array of Integers
Intel IA-32 supports 3 fundamental addressing modes Compilers use registers to optimize code
5
Array Sum Example Ambiguous Indirect Operands
Indirect addressing is ideal for traversing an array Consider the following instructions:
.data mov [EBX], 100
array DWORD 10000h,20000h,30000h add [ESI], 20
.code
inc [EDI]
mov esi, OFFSET array ; esi = array address
mov eax,[esi] ; eax = [array] = 10000h Where EBX, ESI, and EDI contain memory addresses
add esi,4 ; why 4? The size of the memory operand is not clear to the assembler
add eax,[esi] ; eax = eax + [array+4]
EBX, ESI, and EDI can be pointers to BYTE, WORD, or DWORD
add esi,4 ; why 4?
add eax,[esi] ; eax = eax + [array+8] Solution: use PTR operator to clarify the operand size
mov BYTE PTR [EBX], 100 ; BYTE operand in memory
Note that ESI register is used as a pointer to array
ESI must be incremented by 4 to access the next array element add WORD PTR [ESI], 20 ; WORD operand in memory
Because each array element is 4 bytes (DWORD) in memory inc DWORD PTR [EDI] ; DWORD operand in memory
Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55 Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55
6
Based-Indexed Examples LEA Instruction
.data LEA = Load Effective Address
matrix DWORD 0, 1, 2, 3, 4 ; 4 rows, 5 cols
DWORD 10,11,12,13,14 LEA r32, mem (Flat-Memory)
DWORD 20,21,22,23,24 LEA r16, mem (Real-Address Mode)
DWORD 30,31,32,33,34
Calculate and load the effective address of a memory operand
ROWSIZE EQU SIZEOF matrix ; 20 bytes per row Flat memory uses 32-bit effective addresses
.code Real-address mode uses 16-bit effective addresses
mov ebx, 2*ROWSIZE ; row index = 2 LEA is similar to MOV OFFSET, except that:
mov esi, 3 ; col index = 3
mov eax, matrix[ebx+esi*4] ; EAX = matrix[2][3] OFFSET operator is executed by the assembler
Used with named variables: address is known to the assembler
mov ebx, 3*ROWSIZE ; row index = 3
LEA instruction computes effective address at runtime
mov esi, 1 ; col index = 1
mov eax, matrix[ebx+esi*4] ; EAX = matrix[3][1] Used with indirect operands: effective address is known at runtime
Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55 Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55
lea eax, array[esi] ; mov eax, esi CPU computes the effective
; add eax, OFFSET array address of a memory operand
7
Default Segments Next . . .
When 32-bit register indirect addressing is used
Address in EAX, EBX, ECX, EDX, ESI, and EDI is relative to DS Operand Types
Address in EBP and ESP is relative to SS
Data Transfer Instructions
In flat-memory model, DS and SS are the same segment
Therefore, no need to worry about the default segment Addition and Subtraction
When 16-bit register indirect addressing is used Addressing Modes
Address in BX, SI, or DI is relative to the data segment DS Jump and Loop Instructions
Address in BP is relative to the stack segment SS
Copying a String
In real-address mode, DS and SS can be different segments
Summing an Array of Integers
We can override the default segment using segment
prefix
mov ax, ss:[bx] ; address in bx is relative to stack segment
mov ax, ds:[bp]
Basic Instructions & Addressing Modes ; address in bp is relative to data segment
slide /55
Computer Organization & Assembly Language Programming Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55
8
Next . . . Copying a String
The following code copies a string from source to target
Operand Types
.DATA
Data Transfer Instructions source BYTE "This is the source string",0
target BYTE SIZEOF source DUP(0)
Addition and Subtraction .CODE
main PROC Good use of SIZEOF
Addressing Modes mov esi,0 ; index register
mov ecx, SIZEOF source ; loop counter
Jump and Loop Instructions L1:
mov al,source[esi] ; get char from source
Copying a String mov target[esi],al ; store it in the target
Summing an Array of Integers inc esi ; increment index
loop L1 ESI is used to ; loop for entire string
exit index source &
main ENDP target strings
END main
Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55 Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55
.DATA .DATA
intarray WORD 100h,200h,300h,400h,500h,600h intarray DWORD 10000h,20000h,30000h,40000h,50000h,60000h
.CODE .CODE
main PROC main PROC
mov esi, OFFSET intarray ; address of intarray mov esi, 0 ; index of intarray
mov ecx, LENGTHOF intarray ; loop counter mov ecx, LENGTHOF intarray ; loop counter
mov ax, 0 ; zero the accumulator mov eax, 0 ; zero the accumulator
L1: L1:
add ax, [esi] ; accumulate sum in ax add eax, intarray[esi*4] ; accumulate sum in eax
add esi, 2 ; point to next integer inc esi ; increment index
loop L1 ; repeat until ecx = 0 loop L1 ; repeat until ecx = 0
exit exit
main ENDP esi is used as a pointer main ENDP esi is used as a scaled index
END main contains the address of an array element END main
Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55 Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55
9
Summary
Data Transfer
MOV, MOVSX, MOVZX, and XCHG instructions
Arithmetic
ADD, SUB, INC, DEC, NEG, ADC, SBB, STC, and CLC
Carry, Overflow, Sign, Zero, Auxiliary and Parity flags
Addressing Modes
Register, immediate, direct, indirect, indexed, based-indexed
Load Effective Address (LEA) instruction
32-bit and 16-bit addressing
JMP and LOOP Instructions
Traversing and summing arrays, copying strings
PC-relative addressing
Basic Instructions & Addressing Modes Computer Organization & Assembly Language Programming slide /55
10