0% found this document useful (0 votes)
28 views23 pages

Data Transfer, Addressing and Arithmetic: Microprocessor and Assembly Languages

Uploaded by

Hafza Ghafoor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views23 pages

Data Transfer, Addressing and Arithmetic: Microprocessor and Assembly Languages

Uploaded by

Hafza Ghafoor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Data Transfer, Addressing and

Arithmetic
Microprocessor and Assembly Languages

with slides by Kip Irvine & Yung-Yu Chuang


Disclaimer Statement

In preparation of these slides, materials have been taken f


rom different online sources in the shape of books, websit
es, research papers and presentations etc. However, the a
uthor does not have any intention to take any benefit of t
hese in her/his own name. This lecture (audio, video, slid
es etc) is prepared and delivered only for educational pur
poses and is not intended to infringe upon the copyrighted
material. Sources have been acknowledged where applica
ble. The views expressed are presenter’s alone and do not
necessarily represent actual author(s) or the institution.
Chapter overview
• Data Transfer Instructions
• Addition and Subtraction
• Data-Related Operators and Directives
• Indirect Addressing
• JMP and LOOP Instructions
Addition and Subtraction

• INC and DEC Instructions


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

• Add 1, subtract 1 from destination operan


d
– operand may be register or memory
• INC destination
• Logic: destination  destination + 1
• DEC destination
• Logic: destination  destination – 1
• INC and DEC affect all status flags except th
e Carry flag
INC and DEC Examples

.data
myWord dw 1000h
myDword DWORD 10000000h
.code
inc myWord ; 1001h
dec myWord ; 1000h
inc myDword ; 10000001h
mov ax,00FFh
inc ax ; AX = 0100h
mov ax,00FFh
inc al ; AX = 0000h
Your turn...
Show the value of the destination operand after each of t
he following instructions executes:

.data
myByte db 0FFh, 0
.code
mov al,myByte ; AL =
mov ah,[myByte+1] ; AH = FFh
dec ah ; AH = 00h
inc al ; AL = FFh
dec ax ; AX = 00h
FFFF
ADD and SUB Instructions
• ADD destination, source
• Logic: destination  destination + source
• SUB destination, source
• Logic: destination  destination – source
• Same operand rules as for the MOV
instruction
• Source remains unchanged
• Both operands must be of the same size
• Cannot be both mem operands at the same
time
• ADD and SUB affect all status flags
ADD and SUB Examples

.data
var1 DWORD 10000h
var2 DWORD 20000h
.code ; ---EAX---
mov eax,var1 ; 00010000h
add eax,var2 ; 00030000h
add ax,0FFFFh ; 0003FFFFh
add eax,1 ; 00040000h
sub ax,1 ; 0004FFFFh
NEG (negate) Instruction
Reverses the sign of an operand. Operand can be a register or
memory operand.

.data
valB db -1
valW dw +32767
.code
mov al,valB ; AL = -1
neg al ; AL = +1
neg valW ; valW = -32767

Suppose AX contains –32,768 and we apply NEG to it. Will


the result be valid?

NEG affects all status flags


Implementing Arithmetic Expressions
HLL compilers translate mathematical expressions into
assembly language. You can do it also. For example:
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 ; EBX = 30
sub ebx,Zval ; EBX = -10
add eax,ebx
mov Rval,eax ; -36
Your turn...
Translate the following expression into assembly language. Do
not permit Xval, Yval, or Zval to be modified:
Rval = Xval - (-Yval + Zval)

Assume that all values are signed doublewords.

mov ebx,Yval
neg ebx
add ebx,Zval
mov eax,Xval
sub eax,ebx
mov Rval,eax
Flags Affected by Arithmetic
• The ALU has a number of status flags that reflect the outcome of a
rithmetic (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 (unsigned
overflow)
– Overflow flag – set when signed value is out of range (signed o
verflow)
• The MOV instruction never affects the flags.
Concept Map
CPU

part of executes

executes
ALU
conditional jumps
arithmetic & bitwise
operations attached to used by provide

affect
status flags
branching logic

You can use diagrams such as these to express the relationships between assembly
language concepts.
Zero Flag (ZF)
Whenever the destination operand equals Zero, the Zero flag is
set.

mov cx,1
sub cx,1 ; CX = 0, ZF = 1
mov ax,0FFFFh
inc ax ; AX = 0, ZF = 1
inc ax ; AX = 1, ZF = 0

A flag is set when it equals 1.


A flag is clear when it equals 0.
Sign Flag (SF)
The Sign flag is set when the destination operand is negative.
The flag is clear when the destination is positive.
mov cx,0
sub cx,1 ; CX = -1, SF = 1
add cx,2 ; CX = 1, SF = 0

The sign flag is a copy of the destination's highest bit:


mov al,0
sub al,1 ; AL = 11111111b, SF = 1
add al,2 ; AL = 00000001b, SF = 0
Carry Flag (CF)
The Carry flag is set when the result of an operation gener
ates an unsigned value that is out of range (too big or too
small for the destination operand).
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

In the second example, we tried to generate a negative


value. Unsigned values cannot be negative, so the Carry flag
signaled an error condition.
Your turn . . .
For each of the following marked entries, show the values of
the destination operand and the Sign, Zero, and Carry flags:

mov ax,00FFh
add ax,1 ; AX= SF= ZF= CF=
sub ax,1 ; AX= 0100h SF= 0 ZF= 0 CF= 0
add al,1 ; AL= 00FFh SF= 0 ZF= 0 CF= 0
mov bh,6Ch 00h 0 1 1
add bh,95h ; BH= SF= ZF= CF=
01h 0 0 1
mov al,2
sub al,3 ; AL= SF= ZF= CF=
FFh 1 0 1
Overflow Flag (OF)
The Overflow flag is set when the signed result of an oper
ation is invalid or out of range.
; Example 1
mov al,+127
add al,1 ; OF = 1, AL = ??
; Example 2
mov al,7Fh ; OF = 1, AL = 80h
add 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.
A 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 ne
gative
– Two negative operands are added and their sum is p
ositive
– Then we have signed overflow

What will be the values of the Overflow flag?


mov al,80h
add al,92h ; OF = 1
mov al,-2
add al,+127 ; OF = 0
Your turn . . .
What will be the values of the Carry and Overflow flags after
each operation?

mov al,-128
neg al ; CF = 0 OF = 1
mov ax,8000h
add ax,2 ; CF = OF =
0 0
mov ax,0
sub ax,2 ; CF = OF =
1 0
mov al,-5
sub al,+125 ; CF = OF =
0 1

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy