0% found this document useful (0 votes)
4 views

Lecture05-06

Uploaded by

amanmusicxx
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)
4 views

Lecture05-06

Uploaded by

amanmusicxx
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/ 31

CSC-430

Computer Organization &


Assembly Language

Prof. Dr. Imtiaz Ali Korejo


Email: [email protected]
Programming Examples: Displaying
a String
TITLE PGM4_2: PRINT STRING PROGRAM
.MODEL SMALL
.STACK 100H
.DATA
MSG DB 'HELLO!$'
.CODE
MAIN PROC
;initialize DS
MOV AX,@DATA
MOV DS,AX ;initialize DS
;display message
LEA DX,MSG ;get message
MOV AH,9 ;display string function
INT 21H ;display message
;return to DOS
MOV AH,4CH
INT 21H ;DOS exit
MAIN ENDP
END MAIN
Programming Example:Case
Conversion
TITLE PGM4_3: CASE CONVERSION PROGRAM
.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB 'ENTER A LOWER CASE LETTER: $'
MSG2 DB 0DH,0AH,'IN UPPER CASE IT IS: '
CHAR DB ?,'$'
.CODE
MAIN PROC
;initialize DS
MOV AX,@DATA ;get data segment
MOV DS,AX ;initialize DS
;print user prompt
LEA DX,MSG1 ;get first message
MOV AH,9 ;display string function
INT 21H ;display first message
;input a character and convert to upper case
MOV AH,1 ;read character function
INT 21H ;read a small letter into AL
SUB AL,20H ;convert it to upper case
MOV CHAR,AL ;and store it
;display on the next line
LEA DX,MSG2 ;get second message
MOV AH,9 ;display string function
INT 21H ;display message and upper
;DOS exit ;case letter in front
MOV AH,4CH
INT 21H ;DOS exit
MAIN ENDP
END MAIN
Addition of two number
2+4=6 DECIMAL VALUE ASC CHAR
48D [30H] 0
MOV AL, 2
49D [31H] 1
MOV BL, 4 50D [32H] 2
ADD BL,AL 51D [33H] 3
52D [34] 4
ADD BL,30H
MOV DL,BL
MOV AH,2
INT 21H
Addition of two numbers
Processor Status & Flags Register
Circuits in CPU make decisions based on (the values of these flags) the current
state of the processor
The processor state is implemented as 9 bits called FLAGS
 Flags are placed in Flags register
 Two types of Flags
Status Flags
– The ALU has a number of status flags that reflect the outcome of arithmetic (and bitwise)
operations. Located in bits: 0, 2, 4, 6, 7 and 11.
– Based on the contents of the destination operand.
Control Flags
– Enable or disable certain operations of the processor. Located in bits 8, 9 and 10.
Objectives of the Flag Registers
• Status indication: The Flag register indicate the status of the previous
instruction executed.
• Conditional branching: The Flag register is used to control conditional
branching instructions.
• Arithmetic and logic operations: The Flag register is updated after
arithmetic and logic operations.
• Interrupt Handling: The Flag register is used to control interrupt
handling. The interrupt flag(IF) is used to enable or disable interrupts.
Flags Register
Flags Register(cont’d)
Auxiliary Carry Flag (AF)
– 1 if there is a carry out from bit 3 on addition or a borrow from bit 3 on subtraction (used and
interpreted in BCD arithmetic operations). We DO NOT have to worry with this flag.
Zero Flag (ZF)
– 1 if result is a zero; 0 if result is not a zero
Sign Flag (SF)
– 1 if result is negative; 0 if result is positive
Overflow Flag (OF)
– 1 if signed overflow occurred; otherwise a zero (Only interpreted on signed arithmetic
operations)
Flags Register (cont’d)
Control Flags:
– TF, IF, DF
Trap Flag (TF)
– Used by interrupts
Interrupt Flag (IF)
– Used by interrupts
Direction Flag (DF)
– Used in string operations
• Note: Bits 15, 14, 13, 12 , 5, 3 and 1 in the FLAGS register have no significance. You DO NOT
have to memorize the specific bit locations of these bits in the FLAGS register!
Zero Flag (ZF)
• The Zero flag is set when the result of an operation produces zero in
the destination operand.
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

Remember...
• 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 generates 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
Overflow
Overflow occurs when the result cannot fit into the destination field after some
arithmetic operation
Types of Arithmetic Overflows
– No overflow
– Signed overflow only (OF = 1)
– Unsigned overflow only (CF = 1)
– Both signed and unsigned overflows (OF=1 and CF=1)
Programmers must interpret the results
– Use the appropriate FLAG to interpret the correct result
– For example, if a signed interpretation needs to be used, only the OF is of interest!
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=0100h SF=0 ZF=0 CF=0
sub ax,1 ; AX=00FFh SF=0 ZF=0 CF=0
add al,1 ; AL=00h SF=0 ZF=1 CF=1
mov bh,6Ch
add bh,95h ; BH=01 SF=0 ZF=0 CF=1

mov al,2
sub al,3 ; AL=FFh SF=1 ZF=0 CF=1
Parity Flag (PF)
 The parity flag is set when the least significant byte of the
destination has an even number of 1 bits.

; Example
mov al, 10001100b
add al, 00000010b ; AL = 10001110, PF = 1
sub al, 10000000b ; AL = 00001110, PF = 0
Auxiliary Carry Flag
The Auxiliary carry flag indicates a carry or borrow out of bit 3 in the
destination operand. It is primarily used in binary coded decimal
(BCD) arithmetic, but can be used in other contexts.
; Example
mov al, 0Fh
add al, 1 ; AF = 1
Here is the arithmetic:
00001111
+ 00000001
------------------------
00010000
More Examples from Text
• Determine how each instruction sets 5 of the STATUS FLAGS (We are not
interested in the AF, the Auxiliary Carry Flag)
• Example 1
AX: FFFF BX: FFFF
ADD AX,BX
AX:FFFE

FLAG Results: SF = 1 Result is negative


PF = 0 7 bits in low order byte (odd)
ZF = 0 Non Zero result
CF = 1 Carried out of MSB
OF = 0 Negative + Negative = Negative
More Examples (cont’d)
• Example 2

AL: 80 BL: 80

ADD AL,BL

AL:00

FLAG Results: SF = 0 Result is positive


PF = 1 Zero 1 bits in low order byte (even)
ZF = 1 Result is zero
CF = 1 Carried out of MSB
OF = 1 Negative + Negative = Positive
More Examples (cont’d)
• Example 3
AX: 8000 BX: 0001

SUB AX,BX

AX:7FFF

FLAG Results: SF = 0 Positive result


PF = 1 8 bits in low order byte
(even)
ZF = 0 Non Zero Result
CF = 0 Large # - Small #
OF = 1 Neg - Pos = Neg + Neg = Pos
More Examples (cont’d)
• Example 4
AL: FF
INC AL
AL: 00

FLAG Results: SF = 0 Zero in MSB


PF = 1 No 1 bits in low order byte (even)
ZF = 1 Zero Result
CF = unaffected by INC or DEC
OF = 0 Neg + Pos cannot overflow
• Example 5
MOV AX,-5
The MOV instruction does not affect ANY flags !!
More Examples (cont’d)
• Example 6
AX: 8000
NEG AX
AX:8000

FLAG Results: SF = 1 Neg Result


PF = 1 No 1 bits in low order byte (even)
ZF = 0 Non Zero Result
CF = 1 Result is not zero on a NEG
OF = 1 Special Case
Word = 8000
DEBUG Program
• Part of MS/DOS
• Is an environment for testing a program
• Can step through a program
• Display and change the contents of a register or memory locations
• Can enter assembly code directly - DEBUG converts it and stores it
in memory
• Steps
• First assemble and link your program
• Then key in DEBUG program name.EXE
• DEBUG's prompt is a -
DEBUG Program (cont’d)
• To view the register contents, type - R and press the ENTER
key
• The third line displayed gives you the address of the next
instruction to be executed in segment:offset form followed
by the machine language version of that instruction
followed by the assemble language version of that
instruction
• The first line and half of the second line displayed gives you
the contents of the 13 PC registers
• The last half of the second line gives you the current flag
settings in the FLAGS Register
DEBUG Program (cont’d)
The FLAGS are given to you in the following order:
OVERFLOW DIRECTION INTERRUPT SIGN ZERO AUX PARITY CARRY
(OF) (DF) (IF) (SF) (ZF) (AF) (PF) (CF)
OV DN EI NG ZR AC PE CY
NV UP DI PL NZ NA PO NC

Debug uses the following symbols:


STATUS FLAG IS SET (1) FLAG IS CLEAR (0)
FLAG
OF OV (overflow) NV (No overflow)
DF DN (down) UP (up)
IF EI(enable interrupt) DI(disable interrupt)
SF NG (negative) PL (plus)
ZF ZR (zero) NZ (non zero)
AF AC (auxiliary carry) NA (no auxiliary)
PF PE (even parity) PO (odd parity)
CF CY (carry) NC (no carry)
DEBUG Program (cont’d)

• To step through (trace through) the program, key in -T


• To complete the execution of a program, key in -G
• To enter machine code or data, key in E followed by the segment
register and offset address and the code or data you want to
enter.
• Examples:
E CS:0100 B8 00 02 A1 00 02 03
E DS:0200 23 01 25 00 00 00
• To dump a section of code or a section of data, key in D followed
by the segment register and offset address of the code or data
you want to dump.
• Examples:
• D CS:100,10A
• D DS:200,208
DEBUG Program (cont’d)
• To execute all instructions up to but not including a certain
instruction use the G command
• Example:
If you’re looking in the code segment and the next instruction to be
executed is an INT 21H instruction which is two bytes long located at
CS:0104
Address Machine Language ALC CODE
CS:0104CD 21 INT 21H

You could key in:


G CS:106 and DEBUG would execute only the INT 21H instruction.

• To exit DEBUG, key in Q


THE END.

You might also like