CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
String_manipulations.pdf
1. STRING HANDLING INSTRUCTIONS
●
8086 has special instructions to handle strings
●
By string we mean an array of bytes/words
●
Need pointers to point to each byte/each word in the array
●
SI is the default pointer to the source string
●
DI is the default pointer to the destination string
●
The processor assumes that source string is in DS
●
The processor assumes that destination string is in ES
2. STRING INSTRUCTIONS
●
The processor needs to increment or decrement the pointer to operate the
consecutive bytes/words in a string
●
String instructions automatically update (increment/decrement) the pointers
●
DF (Direction Flag) decides whether the pointers are incremented or
decremented by a string instruction
●
The programmer should set/clear the DF using instructions:
●
CLD – Clear Direction Flag (DF =0), the string instruction will automatically
increment the pointer(s)
●
STD – Set Direction Flag (DF =1), the string instruction will automatically
decrement the pointer(s)
3. MOVSB/MOVSW
●
MOVSB/MOVSW (move string byte/move string word)
●
Transfers a byte/word from a memory location pointed DS:SI pair to the
memory location pointed by ES:DI pair.
●
MOVSB/MOVSW also automatically updates SI and DI to point to the next
element in the source and destination string.
4. REP Prefix
●
The instruction to which the REP prefix is attached, is executed repeatedly.
●
REP assumes that CX holds the the number of times the instruction should be
repeated
●
Each time an instruction with REP prefix is executed, the basic string
operation is performed, pointers are updated, and then , CX is decremented
by 1, if CX is not zero then the operation is repeated.
●
The repeat count must be loaded into CX prior to executing a string
instruction with REP prefix
●
REP prefix is used with MOVSB/MOVSW to repeat the data transfer in order
to completely transfer the string from one location to another
6. LODSB/LODSW
●
Load string byte /load string word
●
LODSB loads the content of memory location pointed by the DS:SI in to
AL, and updates SI (depending on DF). If DF = 0, SI is incremented by 1. If
DF = 1, SI is decremented by 1
●
LODSW loads the content of memory locations pointed by the DS:SI and
DS:(SI+1) in to AX, and updates SI (depending on DF). If DF = 0, SI is
incremented by 2. If DF = 1, SI is decremented by 2
●
LODSB/LODSW is never used with REP prefix
7. STOSB/STOSW
●
Store string byte /store string word
●
STOSB stores the byte in AL into the memory location pointed by ES: DI
and updates DI (depending on DF ). If DF = 0, DI is incremented by 1. If DF
= 1, DI is decremented by 1
●
STOSW stores the word in AX to the memory locations pointed by ES: DI
and ES:(DI+1) and updates DI (depending on DF ). If DF = 0, DI is
incremented by 2. If DF = 1, DI is decremented by 2
●
REP prefix can be used with STOSB/STOSW
8. .DATA
MEM_BLOCK DB 100 DUP(?)
Msg DB 'bad memory', '$'
.CODE
; - - -
MOV AX, @DATA
MOV DS, AX
MOV ES, AX
CLD
MOV CX, 50
MOV DI, OFFSET MEM_BLOCK
MOV AX, 0AAAAH
REP STOSW
MOV SI, OFFSET MEM_BLOCK
MOV CX, 100
AGAIN: LODSB
XOR AL, AH
JNZ OVER
LOOP AGAIN
JMP EXIT
OVER: MOV AH,09H
MOV DX, OFFSET Msg
INT 21H
EXIT: MOV AH,4CH
INT 21H
; - - -
Store byte AAH into 100 memory locations, test the content of each location to see if AAH is there.
If test fails display the msg “bad memory”
9. CMPSB/CMPSW
●
Compare string byte / compare string word
●
CMPS allows comparison of two arrays pointed by SI and DI
●
CMPS subtracts the source byte/word from dest. byte/word
●
Affects the flags, but does not alter either operand
●
Automatically updates SI and DI (depending on DF)
●
We can test for the equality/ inequality of the string by the use of REPE or
REPNE prefixes
10. REPE and REPNE Prefixes
●
Used with CMPS and SCAS instructions
●
REPE will repeat the string operation as long as the source
and destination operands are equal (ZF = 1) or until CX
becomes zero
●
REPNE will repeat the string operation as long as the
source and destination operands are not equal (ZF = 0 ) or
until CX becomes zero
13. SCASB/SCASW
●
Scan String byte/Scan String Word
●
SCASB/SCASW allows us to scan a string for a particular byte/word
●
SCASB/SCASW instruction compares each byte/word of an array
pointed by ES:DI with the content of AL/AX and also updates DI
●
SCASB does (AL - ES:DI ), update the flags, update DI
●
SCASW does (AX- [ES:DI+1,ES:DI]), update the flags, update DI
●
SCASB/SCASW can be prefixed with REPE/REPNE
15. Scan a string for a specific letter and replace it with another letter
16. Signed Number representation in computers
D7 = 0, operand is positive
D7 = 1, operand is negative
The range of positive numbers that can
be represented by the above format is 0
to +127
17. Signed byte operands
For negative numbers, D7 =1, and the magnitude is represented in 2's
complement form
Example: representaion of
-5 = FBH
-34H = CCH
-128 = 80H (This is not negative zero)
21. Overflow problem in signed number operations
In 8-bit signed number operations, OF is set to 1 if either of the following
conditions occurs:
1) there is a carry from D6 to D7 but no carry out of D7 (CF = 0)
2) there is a carry from D7 out (CF =1) but no carry from D6 to D7
This means if there is a carry both from D6 to D7 and from D7 out, OF = 0
24. Overflow problem in signed number operations
In 16-bit signed number operations, OF is set to 1 if either of the following
conditions occurs:
1) there is a carry from D14 to D15 but no carry out of D15 (CF = 0)
2) there is a carry from D15 out (CF =1) but no carry from D14 to D15
This means if there is a carry both from D14 to D15 and from D15 out, OF = 0