You are on page 1of 6

Evaluate this . . . Evaluate this . . .

(cont)
.data
• We want to write a program that adds the following three bytes: myBytes BYTE 80h,66h,0A5h
.data
myBytes BYTE 80h,66h,0A5h
• How about the following code. Is anything missing?
• What is your evaluation of the following code?
movzx ax,myBytes
mov al,myBytes
mov bl,[myBytes+1]
add al,[myBytes+1]
add ax,bx
add al,[myBytes+2]
mov bl,[myBytes+2]
add ax,bx ; AX = sum
• What is your evaluation of the following code?
mov ax,myBytes
add ax,[myBytes+1]
add ax,[myBytes+2]
Yes: Move zero to BX before the MOVZX instruction.
• Any other possibilities?

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 16 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 17

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 18 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 19

Addition and Subtraction

• INC and DEC Instructions


• ADD and SUB Instructions
• NEG Instruction
• Implementing Arithmetic Expressions
• Flags Affected by Arithmetic
• Zero
• Sign
• Carry
• Overflow

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 20 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 21

1
INC and DEC Instructions INC and DEC Examples

.data
• Add 1, subtract 1 from destination operand myWord WORD 1000h
• operand may be register or memory myDword DWORD 10000000h
• INC destination .code
inc myWord ; 1001h
• Logic: destination  destination + 1 dec myWord ; 1000h
• DEC destination inc myDword ; 10000001h
• Logic: destination  destination – 1
mov ax,00FFh
inc ax ; AX = 0100h
mov ax,00FFh
inc al ; AX = 0000h

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 22 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 23

Write a short program demonstrating that the INC and DEC instructions do
Your turn... not affect the Carry flag.
.code
main PROC
Show the value of the destination operand after each of the
following instructions executes: ; INC does not affect the carry flag:
mov al,254
add al,1 ; AL=255, CF=0
.data
call DumpRegs
myByte BYTE 0FFh, 0
inc al ; AL=0, CF=0 (unchanged)
.code
call DumpRegs
mov al,myByte ; AL = FFh
mov ah,[myByte+1] ; AH = 00h
dec ah ; AH = FFh ; DEC does not affect the carry flag:
mov al,1
inc al ; AL = 00h
sub al,1 ; AL=0, CF=0
dec ax ; AX = FEFF
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, 2003. 24 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 25

ADD and SUB Instructions ADD and SUB Examples

• ADD destination, source


• Logic: destination  destination + source .data
• SUB destination, source
var1 DWORD 10000h
var2 DWORD 20000h
• Logic: destination  destination – source .code ; ---EAX---
mov eax,var1 ; 00010000h
• Same operand rules as for the MOV add eax,var2 ; 00030000h
instruction add ax,0FFFFh ; 0003FFFFh
add eax,1 ; 00040000h
sub ax,1 ; 0004FFFFh

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 26 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 27

2
INCLUDE Irvine32.inc
Description: Write a program that uses addition and .data
.code
subtraction to set and clear the Carry flag. After each main PROC
instruction, insert the call DumpRegs statement to
display the registers and flags. Using comments, explain ;adding 1 to 255 rolls AL over to zero:
mov al,255
how (and why) the Carry flag was affected by each add al,1 ; AL=0, CF=1 (unsigned overflow)
instruction. call DumpRegs

;subtracting larger number from smaller:


sub al,1 ; AL=255, CF=1
call DumpRegs

;subtracting 1 from 255


sub al,1 ; AL=254, CF=0
call DumpRegs

exit
main ENDP
28
END main 29
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.

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 NEG (negate) Instruction
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. Reverses the sign of an operand. Operand can be a register or
memory operand.
mov al,+127 ; AL=7Fh
add al,1 ; AL=80h, OF=1
call DumpRegs .data
valB BYTE -1
sub al,1 ; AL=7Fh, OF=1 valW WORD +32767
call DumpRegs .code
mov al,valB ; AL = -1
sub al,1 ; AL=7Eh, OF=0
call DumpRegs
neg al ; AL = +1
neg valW ; valW = -32767
mov al,-128
add al,-1
call DumpRegs ; AL=7Fh, OF=1
Suppose AX contains –32,768 and we apply NEG to it. Will
; Optional: the result be valid?
mov al,80h
add al,80h ; AL=0, CF=1, OF=1
call DumpRegs

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 30 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 31

NEG Instruction and the Flags Implementing Arithmetic Expressions


HLL compilers translate mathematical expressions into
The processor implements NEG using the following internal assembly language. You can do it also. For example:
operation:
Rval = -Xval + (Yval – Zval)
SUB 0,operand
Rval DWORD ?
Any nonzero operand causes the Carry flag to be set. Xval DWORD 26
Yval DWORD 30
Zval DWORD 40
.data
.code
valB BYTE 1,0 mov eax,Xval
valC SBYTE -128 neg eax ; EAX = -26
.code mov ebx,Yval
neg valB ; CF = 1, OF = 0 sub ebx,Zval ; EBX = -10
neg [valB + 1] ; CF = 0, OF = 0 add eax,ebx
neg valC ; CF = 1, OF = 1 mov Rval,eax ; -36

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 32 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 33

3
Your turn... Flags Affected by Arithmetic
Translate the following expression into assembly language. Do
not permit Xval, Yval, or Zval to be modified: • The ALU has a number of status flags that reflect the
Rval = Xval - (-Yval + Zval)
outcome of arithmetic (and bitwise) operations
• based on the contents of the destination operand
Assume that all values are signed doublewords. • Essential flags:
mov ebx,Yval • Zero flag – set when destination equals zero
neg ebx • Sign flag – set when destination is negative
add ebx,Zval
mov eax,Xval
• Carry flag – set when unsigned value is out of range
sub eax,ebx • Overflow flag – set when signed value is out of range
• The MOV instruction never affects the flags.
mov Rval,eax

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 34 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 35

Concept Map Zero Flag (ZF)


CPU
The Zero flag is set when the result of an operation produces
part of executes zero in the destination operand.

executes mov cx,1


ALU sub cx,1 ; CX = 0, ZF = 1
conditional jumps
mov ax,0FFFFh
arithmetic & bitwise inc ax ; AX = 0, ZF = 1
operations attached to used by inc ax ; AX = 1, ZF = 0
provide

affect
status flags
branching logic
Remember...
• A flag is set when it equals 1.
You can use diagrams such as these to express the relationships between assembly • A flag is clear when it equals 0.
language concepts.

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 36 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 37

Sign Flag (SF) Signed and Unsigned Integers


A Hardware Viewpoint
The Sign flag is set when the destination operand is negative.
The flag is clear when the destination is positive.
• All CPU instructions operate exactly the same on
mov cx,0 signed and unsigned integers
sub cx,1 ; CX = -1, SF = 1
add cx,2 ; CX = 1, SF = 0
• The CPU cannot distinguish between signed and
unsigned integers
The sign flag is a copy of the destination's highest bit:
mov al,0
sub al,1 ; AL = 11111111b, SF = 1
• YOU, the programmer, are solely responsible for
add al,2 ; AL = 00000001b, SF = 0 using the correct data type with each instruction

Added Slide. Gerald Cahill, Antelope Valley College


Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 38 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 39

4
Overflow and Carry Flags Carry Flag (CF)
A Hardware Viewpoint
The Carry flag is set when the result of an operation generates an
• How the ADD instruction modifies OF and CF: unsigned value that is out of range (too big or too small for the
• OF = (carry out of the MSB) XOR (carry into the MSB) destination operand).
• CF = (carry out of the MSB)
mov al,0FFh
add al,1 ; CF = 1, AL = 00
• How the SUB instruction modifies OF and CF:
; Try to go below zero:
• NEG the source and ADD it to the destination
• OF = (carry out of the MSB) XOR (carry into the MSB) mov al,0
sub al,1 ; CF = 1, AL = FF
• CF = INVERT (carry out of the MSB)

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, 2003. 40 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 41

Your turn . . . Overflow Flag (OF)


For each of the following marked entries, show the values of The Overflow flag is set when the signed result of an operation is
the destination operand and the Sign, Zero, and Carry flags: invalid or out of range.

mov ax,00FFh ; Example 1


add ax,1 ; AX= 0100h SF= 0 ZF= 0 CF= 0 mov al,+127
sub ax,1 ; AX= 00FFh SF= 0 ZF= 0 CF= 0 add al,1 ; OF = 1, AL = ??
add al,1 ; AL= 00h SF= 0 ZF= 1 CF= 1
mov bh,6Ch ; Example 2
add bh,95h ; BH= 01h SF= 0 ZF= 0 CF= 1 mov al,7Fh ; OF = 1, AL = 80h
add al,1
mov al,2
sub al,3 ; AL= FFh SF= 1 ZF= 0 CF= 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, 2003. 42 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 43

A Rule of Thumb Your turn . . .


• When adding two integers, remember that the What will be the values of the given flags after each operation?
Overflow flag is only set when . . .
• Two positive operands are added and their sum is mov al,-128
negative neg al ; CF = 1 OF = 1
• Two negative operands are added and their sum is mov ax,8000h
positive add ax,2 ; CF = 0 OF = 0

What will be the values of the Overflow flag? mov ax,0


sub ax,2 ; CF = 1 OF = 0
mov al,80h
add al,92h ; OF = 1
mov al,-5
mov al,-2 sub al,+125 ; OF = 1
add al,+127 ; OF = 0

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 44 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 45

5
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 46 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 47

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 48 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 49

You might also like