Transcript Chapter 1
CS2422 Assembly Language and System Programming
Week 9
Data Transfers, Addressing, and
Arithmetic
0
What's Next
Addition and Subtraction
INC and DEC Instructions
ADD and SUB Instructions
NEG Instruction
Implementing Arithmetic Expressions
Flags Affected by Arithmetic
‒ Zero, Sign, Carry, Overflow
1
INC and DEC Instructions
Add 1/subtract 1 from destination operand
operand may be register or memory
.data
myWord WORD 1000h
myDword DWORD 10000000h
.code
inc myWord
; 1001h
dec myWord
; 1000h
inc myDword
; 10000001h
mov ax,myDword
inc ax
; AX = 10000001h
2
ADD and SUB Instructions
ADD destination, source
Logic: destination destination + source
SUB destination, source
Logic: destination destination – source
Same operand rules as for MOV instruction
.data
var1 DWORD 10000h
var2 DWORD 20000h
.code
mov eax,var1
add eax,var2
add ax,0FFFFh
add eax,1
sub ax,1
;
;
;
;
;
;
---EAX--00010000h
00030000h
0003FFFFh
00040000h
0004FFFFh
3
NEG (negate) Instruction
Reverses the sign of an operand. Operand can
be a register or memory operand
.data
valB BYTE -1
valW WORD +32767
.code
mov al,valB
neg al
neg valW
; AL = -1
; AL = +1
; valW = -32767
4
Arith. Expression in Assembly
HLL mathematical expressions are translated
into assembly language by compiler, e.g.
Rval = -Xval + (Yval – Zval)
Rval DWORD ?
Xval DWORD 26
Yval DWORD 30
Zval DWORD 40
.code
mov eax,Xval
neg eax
; EAX = -26
mov ebx,Yval
sub ebx,Zval
; EBX = -10
add eax,ebx
mov Rval,eax
; -36
5
Flags Affected by Arithmetic
ALU has a number of status flags that reflect the
outcome of arithmetic (and bitwise) operations
Essential flags:
based on the contents of the destination operand
Zero: set when destination equals zero
Sign: set when destination is negative
Carry: set when unsigned value is out of range
Overflow: set when signed value is out of range
The MOV instruction never affects the flags
6
Zero Flag (ZF)
Zero Flag is set when the result of an operation
produces zero in the destination operand
mov
sub
mov
inc
inc
cx,1
cx,1
ax,0FFFFh
ax
ax
; CX = 0, ZF = 1
; AX = 0, ZF = 1
; AX = 1, ZF = 0
Remember...
• A flag is set when it equals 1.
• A flag is clear when it equals 0.
7
Sign Flag (SF)
Sign Flag is set when the destination operand is
negative and clear when destination is positive
mov cx,0
sub cx,1
add cx,2
; CX = -1, SF = 1
; CX = 1, SF = 0
Sign flag is a copy of the destination's highest bit:
mov al,0
sub al,1
add al,2
; AL = 11111111b, SF = 1
; AL = 00000001b, SF = 0
8
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) carry or borrow
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
9
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
; Example 2
mov al,7Fh
add al,1
; OF = 1,
AL = ??
; OF = 1,
AL = 80h
The two examples are identical at binary level because
7Fh equals +127. To determine the value of destination
operand, it is often easier to calculate in hexadecimal.
10
Exercise
Run Example AddSub3
Output
11
CS2422 Assembly Language and System Programming
Week 10
Data Transfers, Addressing, and
Arithmetic
12
What's Next
Data Transfer Instructions
Operand Types
MOV Instruction
Direct Memory Operands
Direct-Offset Operands
Zero and Sign Extension
XCHG Instruction
13
Operand Types
Three basic types of operands:
Immediate – a constant integer (8, 16, or 32 bits)
‒ value is encoded within the instruction
Register – the name of a register
‒ register name is converted to a number and
encoded within the instruction
Memory – reference to a location in memory
‒ memory address is encoded within the instruction,
or a register holds the address of a memory
location
14
Instruction Operand Notation
15
Data Transfer Instructions
MOV is for moving data between:
Memory
Register
Immediate (constant)
Almost all combinations, except:
Memory to Memory!
16
MOV Instruction
• Move from source to destination. Syntax:
MOV destination, source
• Source and destination are of same size
• No more than one memory operand permitted
• CS, EIP, and IP cannot be the destination
• No immediate to segment register moves
.data
count BYTE 100
wVal WORD 2
.code
mov bl,count
mov ax,wVal
mov count,al
mov al,wVal
mov ax,count
mov eax,count
; error
; error
; error
17
Your turn . . .
Explain why each of the following MOV statements are invalid:
.data
bVal BYTE
100
bVal2 BYTE
?
wVal WORD
2
dVal DWORD 5
.code
mov ds,45
mov esi,wVal
mov eip,dVal
mov 25,bVal
mov bVal2,bVal
;immediate move to DS not permitted
;size mismatch
;EIP cannot be the destination
;immediate value cannot be destination
;memory-to-memory move not permitted
18
Memory to Memory?
Must go through a register…
.data
Var1 WORD 100h
Var2 WORD ?
.code
MOV ax, var1
MOV var2, ax
19
Zero or Sign Extension
What happens to ECX if –1 is moved to CX?
.data
signedVal SWORD -16
.code
mov ecx,0
mov cx,signedVal
Are the higher 16 bits of ECX all 0?
What number does ECX represent now?
The solution: MOVZX and MOVSX
MOVZX always fills higher bits with 0.
MOVSX fills higher bits by “sign extension”.
Just extend the left-most bit!
20
Zero Extension
When copy a smaller value into a larger
destination, MOVZX instruction fills (extends) the
upper half of the destination with zeros
0
10001111
Source
(bl)
00000000
10001111
Destination
(ax)
mov bl,10001111b
movzx ax,bl
; zero-extension
The destination must be a register
21
Sign Extension
MOVSX fills the upper half of the destination with
a copy of the source operand's sign bit
10001111
Source
10001111
Destination (ax)
Does it affect
the value?
11111111
(bl)
mov bl,10001111b
movsx ax,bl
; sign extension
The destination must be a register
22
LAHF/SAHF and XCHG
LAHF to load flags (EFLAGS) into AH
Loads Sign, Zero, Auxiliary Carry, Parity, Carry
SAHF to store contents of AH to EFLAGS
XCHG for exchanging data between:
Register, register
Register, memory
Memory, register
(again, no memory to memory)
23
XCHG Instruction
XCHG exchanges the values of two operands.
At least one operand must be a register.
No immediate operands are permitted
.data
var1 WORD 1000h
var2 WORD 2000h
.code
xchg ax,bx
xchg ah,al
xchg var1,bx
xchg eax,ebx
;
;
;
;
xchg var1,var2
;error: two memory operands
exchange
exchange
exchange
exchange
16-bit regs
8-bit regs
mem, reg
32-bit regs
24