0% found this document useful (0 votes)
20 views26 pages

String Operatons

Uploaded by

ashokkumarazk
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)
20 views26 pages

String Operatons

Uploaded by

ashokkumarazk
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/ 26

Stack

 Temporary storage of data and address.


 Used to save the contents of the registers for
the calling program while a procedure executes.
 one dimensional data structure.
 Items are added and removed from one end of
the structure that is it is processed in Last In
First Out (LIFO) manner.
 The most recent addition to the stack is called
the top of the stack.
Initializing stack
• Initialize the stack segment before using it.
• Syntax

STACKSEGMENT
DW size DUP(‘ ‘)
STACKENDS
 The STACK SEGMENT and STACK ENDS directive
  Stack memory size is left undefined the entire
64KB will be used for stack.
 Hold the upper 16 bits of the starting address.
 Stack Pointer (SP ) register is used to hold the
offset of the last word written on the stack.
 The physical address is produced by adding the SS
and SP.
 SP register is automatically decremented by 2
before a word is written to stack.
TITLE TO FIND THE FACTORIAL OF NUMBERS BETWEEN 1 TO 8
;Define stack segment
STACK SEGMENT
DW 100 DUP(0) ;Set aside 100 words for stack
STACK_TOP LABEL WORD ;Assign name to word above stack top
STACK ENDS
 
;Define Data segment
DATA SEGMENT
NUM EQU 05 ;Number to find the factorial
OPT DB '5!=' ;Output the result
RESULT DB 6 DUP(0)
DATA ENDS
 
CODE SEGMENT
ASSUME CS:CODE,DS:DATA,SS:STACK
MAIN:
;Intialize DS
MOV AX,DATA
MOV DS,AX
;Initialize SS
MOV AX,STACK
MOV SS,AX
 
MOV SP,OFFSET STACK_TOP;Initialize stack pointer
SUB SP,0004H ;Make place in the stack for the result
PUSH and PUSHF instruction
• To add a new word to the stack we PUSH
instruction. PUSH source

• Where source is a 16-bit register or memory


word.
Example :PUSH AX
 Execution of PUSH causes the following to
happen SP is decreased by 2
 A copy of the source content is moved to the
address specified by SS:SP. The source is
unchanged.
 The instruction PUSHF, which has no operands,
pushes the contents of the FLAGS register onto
stack.
 
PUSH
POP and POPF

• To remove the top item from the stack, the


POP instruction is used
POP destination
• Where destination is a 16-bit register or
memory word.
Example

 POP AX ;POP the item on top of the stack and


store it in AX
 Execution of POP causes the following to happen
 The content of SS: SP is moved to the destination.
 SP is increased by 2
 The instruction POPF, which has no operands,
copies the contents top of the stack into the FLAGS
register.
POP
 
CALL instruction

 To invoke a procedure, the CALL instruction is


used.
 Two kinds of procedure calls, direct and
indirect call.
• Syntax for Direct call
CALL procedure-name

• Syntax for Indirect call

CALL address-expression
Address expression specifies the register or memory
location containing the address of the procedure
 Actions are taken when a CALL instruction is
executed.
 The return address to the calling program is
saved on the stack, which is the offset of the
next instruction after the CALL statement.
 The segment: offset of this instruction is in CS:IP
at the time the call is executed.
 IP gets the address of the first instruction of the
procedure.
RET instruction

 The RET (return) instruction causes control to


transfer back to the calling procedure.
 Every procedure (except the main procedure)
should have a RET; usually it's the last
statement in the procedure.
RET [pop-value]
 pop-value is optional.
 For a NEAR procedure, execution of RET causes
the stack to be popped into IP.
 If a pop value N is specified, it is added to SP, and
thus has the effect of removing N additional
bytes from the stack.
 CS:IP now contains the segment: offset of the
return ad­dress, and control returns to the calling
program.
String Instructions
 A nice feature of the 8086 is its ability to handle
strings.
 A string is collection of bytes ,words , words or
long-words that can upto 64KB in length.
 Group of characters that resides in consecutive
memory locations.
 Some of the various string operations
MOVE,COMPARE,SCAN,LOAD,STORE,REPEAT,
AUTO Indexing
MOVE Operation
• Moving a string of data from one memory
location to elsewhere in the memory.
• MOVE instructions are used.
(a) MOVS Destination-string , Source-String
Points to Remember
 For the Destination-string, the segment: Offset
registers are ES:DI. For the source –string , the
segment: Offset registers are the DS:SI
 Before Execution of the instruction initialize DI and SI
registers.
 Direction flag is cleared ,SI and DI will auto-increment
otherwise if it is set , SI and DI will auto-decrement.
 The direction flag can be cleared with the CLD
instruction and set with STD instruction.
 CX must be initialized to the proper count
MOVSB instruction
 Moves a single byte from source string to
destination string.
 It copies the contents of byte addressed by
DS:SI to the byte addressed by ES:DI.
 The contents of the source byte are unchanged.
 Both SI and DI are automatically incremented if
DF=0, or decremented if DF=1.
Syntax
MOVSB
  
MOVSW instruction

 Moves a word from source string to destination


string.
 It copies the contents of word addressed by DS:SI to
the word addressed by ES:DI.
 Contents of the source byte are unchanged.
 Both SI and DI are automatically incremented by 2 if
DF=0, or decremented by 2 if DF=1.
Syntax
MOVSW
 
CMPSB instruction
 Subtracts the byte with address ES:DI from
the byte with address DS:SI, and sets the flags.
 The result is not stored. Afterward, both SI
and DI are incre­mented if DF = 0, or
decremented if DF = 1.

CMPSB

The CMPSB instruction affects all the flags.


CMPSW instruction

 Instruction subtracts the word with address


ES:DI from the word with address DS:SI, and
sets the flags.
 The result is not stored.
 Afterward, both SI and DI are incre­mented by
2 if DF = 0, or decremented by 2 if DF = 1.
• Syntax
– CMPSW

The CMPSW instruction affects all the flags.

You might also like