SlideShare a Scribd company logo
1 of 57
PROGRAMMING
CONCEPTS-III
CHAPTER 4
STRING INSTRUCTIONS
 The 8086 has a set of instructions for handling
blocks of data in the form of bytes or words.
 They are called ‘string’ instructions.
 A string is an array of data of the same type – for
example, a character string or a byte string
 The usefulness of string instructions can be
seen when in the memory data has to be moved,
searched or compared in blocks.
PRE-REQUISITES FOR USING STRING
INSTRUCTIONS
i. DS and ES are to be defined: Initializing & using
these segment makes DS as the source & ES as
the destination segment
ii. SI and DI acts as pointers: SI to point to DS & DI top
point to ES, means SI should contain the address (offset) of
the first location in the DS and DI should contain the
address of the first location in ES
iii. DF should be set/reset: control flag called as direction
flag, used exclusively for string operations. Purpose in
string operation is, if flag is set, the pointers get
automatically decremented, and if reset, the reverse
occurs. So, during string instructions are used, DF should
be set/reset depending on the direction.
iv. CX should act as a counter: counter CX should be
loaded with the count of number of operations required.
LIST OF INSTRUCTIONS/PREFIXES USED IN
STRING OPERATIONS
THE MOVS INSTRUCTION
 Can be used as MOVSB or MOVSW
 It moves data from the source segment (data
segment) to the destination segment (extra
segment)
 CX acts as the count register
 DF is to be set/reset
 SI and DI have to be intialiased to the source
address in the data segment ,and the destination
address in the extra segment
MOVING A STRING -USING THE FULL SEGMENT
MODEL
 Two segments DAT & EXTR are defined
 Two segment registers DS & ES are initialized
 SI & DI point to the source data & destination data
 CLD is used to clear the DF for auto incrementing the
pointer registers
 This causes the value of SI and DI to be incremented after
each move operation
 Here, SI & DI are incremented only by one each time
 String instruction is used here is MOVSB
 This causes the data in location pointed by SI to be moved
to the location pointed by DI
 After each such move operation, two actions occur
 One is that CX decrements by 1 and the other is that the
pointer registers are incremented
 This sequence continues until CX=0
USING THE SIMPLIFIED MODEL
THE CMPS INSTRUCTION
 This instruction is for string comparison, byte
by byte or word by word as the case may be.
The conditional flags are modified according
to the result of the comparison.
String comparison has to be accompanied by
the use of the conditional REP prefix.
Since string comparison checks only for
equality, the Zero flag is made use of
automatically.
 Example
 Compare two character strings (of length six) which
are saved in the data segment.
 One of two messages is to be displayed, depending
on whether the strings are equal or not.
THE SCAS INSTRUCTION
 The SCAS instruction scans a byte or word string to
ascertain the presence of a specific byte or word. This
specific data is loaded into AL or AX.
 The string which is to be scanned has to be in the
extra segment, and is to be pointed by DI. This is
mandatory and cannot be overridden.
 Write a program to search for a character in an
ASCII string .
 Express the result using appropriate messages
EXAMPLE 4.3 -SOLUTION
THE STOS AND LODS
INSTRUCTIONS
 i) STOS
 The STOS instruction is the mnemonic for ‘storing’ a
string in memory.
 As such, we need to define a memory area in which
‘storing’ is to be done. This memory area is defined to
be the extra segment, and it is addressed by DI as it
is the destination segment.
 The data to be stored is placed in the AL or AX register.
 An area in memory can be filled with the required data,
with this instruction.
 Write a program to fill an area of memory with a
specific word
EXAMPLE 4.4 -SOLUTION
THE LODS
INSTRUCTION
 This is an instruction for ‘loading’.
 Here, the source memory is the data
segment and the pointer to it is SI
 The data segment is the source segment
and the destination register is the AL or AX
register.
 There is no sense in using the REP prefix
for the LODS instruction as data can be
loaded to AL/AX only once.
PROCEDURES
When a main program calls a subsidiary
program ,the latter is called a procedure
 To avoid writing the sequence of instructions
in the program each time you need them,
you can write the sequence as a separate
subprogram called a procedure
 You use the CALL instruction to send the
8086 to the starting address of the
procedure in memory
CONTD..
 A RET instruction at the end of the procedure
returns execution to the next instruction in the main
line
 Procedures can even be nested
THE SEQUENCE OF CALLING PROCEDURES
 Near procedure –which resides in the same code
segment as the main (calling) program
 A procedure is called by a CALL instruction
 At the time the CALL instruction is being executed,
the IP (instruction pointer) will be pointing to the
next instruction in the main program
THE STEPS TAKEN BY THE PROCESSOR FOR
CALLING PROCEDURES
 It saves the current IP content on the stack (this is
the ‘return’ address for coming back to the main
program after executing the procedure).
 The CALL destination (specified in the CALL
instruction) will be the address of the procedure.
The IP is now loaded with this address and
execution proceeds from that location
 The procedure is executed until a RET (return)
instruction in the procedure is encountered.
 Then, the old value of the IP is retrieved from the
stack and control returns to the main program at the
return address.
CALL AND RETURN
25
MAINLINE OR CALLING
PROGRAM
NEXT MAINLINE
INSTRUCTIONS
PROCEDURE
INSTRUCTIONS
CALL
RET
Fig. Single Procedure Call
26
Main Line Instructions
RET
Lower level
Procedure
CALL
RET
Procedure
CALL
Next Main Line
Instructions
Fig. Nested Procedures Call
WRITING A PROCEDURE
 The procedure should begin with the
procedure name followed by the directive
PROC.
 We also use the directives NEAR or FAR
which specify the ‘type’ of the procedure.
 The procedure should end with the
procedure name and ENDP.
EXAMPLE 4.5
 Write a program, which enters 10 single digit
numbers through the keyboard, finds their squares
and stores the squares in memory.
 Only byte locations need be allocated in memory
for the squares as the square of the highest single
digit number is 81, which will fi t into a byte space
EXAMPLE 4.5-SOLUTION
THE CALL INSTRUCTION
 Intrasegment or ‘Near’ Call
 i) Direct CALL
Usage: CALL label
 The direct call is like a direct jump instruction, and is
three bytes long. It is relative and the destination can be
−32,768 bytes to +32,767 bytes from the address of the
instruction following
 the call (this will be the current content of the IP).
 the off set can be a 16-bit signed number.
 When this call is executed, the new value of IP = old IP
+ off set,
FIGURE 4.3 | FORMAT OF THE DIRECT NEAR
CALL INSTRUCTION
II) INDIRECT CALL
 Usage: CALL reg16, CALL [reg16]
In this case, the destination is specified in a 16-bit
register or in a memory location pointed by a
register.
 This is not a ‘relative’ call. The content of the
referred register or memory location is loaded into
IP for using the procedure.
 Examples
 CALL BX ;the procedure’s address is in BX
 CALL WORD PTR[BX] ;the address of the
procedure is in memory and is pointed by BX
EXAMPLE 4.6
INTERSEGMENT OR FAR CALL
 Direct Far Call A far call is an intersegment call,
which means that the destination address is in a
different code segment.
 This will be a 5-byte instruction, the first byte being
the opcode, the second and third bytes being the
new value of IP, and the fourth and fi fth, the new
values of CS.
 This is not a relative call.
 When the procedure is called, the IP and CS values
are replaced by the corresponding values in the call
instruction
FIGURE 4.4 | FORMAT OF THE FAR JUMP
INSTRUCTION
INDIRECT FAR CALL
• The destination address is not in the instruction –
rather, it is in a register or memory.
 For a far call, four bytes are needed to specify a
destination. a register cannot specify it. Hence the
four bytes needed to specify a destination are
stored in
memory and pointed by a register.
 Example of an indirect far call
 CALL DWORD PTR [SI] can be a far call instruction.
[SI] and [SI + 1] gives the new value of IP and [SI + 2]
and [SI + 3] gives the new value of CS.
 Far calls and procedures are specified using the ‘far’
directive when defining the procedure.
 In Chapter 5, we will see how the EXTRN directive is
used in this context.
THE RET INSTRUCTION
 i) Usage: RET
 The execution of RET causes the return address to
be popped from the stack to IP or IP and CS.
 (Whenever a procedure is defined, it is known
whether it is a far or near procedure. From this, it is
determined whether the stack has saved just the
old IP value or both IP and CS.)
RET N
 Usage: RET n
 This is another form of the RET instruction. This
adds the number ‘n’ to the stack pointer (SP) after
the return address has been popped off the stack,
on return from a procedure
THE USE OF THE STACK IN PROCEDURE
CALLS
 When both the main program and the procedures
need the same registers, the content of the
registers are pushed onto the stack when going to
a procedure ,and popped back on returning.
 MASM 6.x has a construct ‘USES’ which
automatically pushes the specified registers onto
the stack.
 The popping will be automatic on returning from the
procedure.
EXAMPLE 4.7A –THE USES DIRECTIVE
EXAMPLE 4.7B
PASSING PARAMETERS TO AND FROM
PROCEDURES
Can be done using
o Registers
o Memory and memory pointers
o Stack
EXAMPLE 4.8
 Write a program which calculates and places in
memory the Nth term of an arithmetic progression.
The formula for the Nth term of an A.P. is A + (N −
1) D, where A is the fi rst term and D is the common
difference.
 Pass the values of the operands using registers
EXAMPLE 4.8
PASSING PARAMETERS THROUGH
MEMORY
 Example 4.9 is a program for arranging a set of
numbers (stored in memory) in descending order.
 The values are passed to the program through
memory pointers.
 The method involves comparing numbers pair-wise
and repeating this N − 1 times, if there are N
numbers to be sorted.
EXAMPLE 4.9
PASSING PARAMETERS THROUGH THE
STACK
 There may be reasons where the stack, which is an
area in the memory, can be used to store data. The
procedure can take data from the stack for its
computation.
 Example 4.10 shows BP being used to access data in
the stack
 Here, four words which are in the data segment are
pushed on to the stack by the main program.
 The procedure accesses the data using the indexing
features of BP. Remember that BP is a register
associated (by default) with the stack segment, and
that BP cannot be used without an off set.
EXAMPLE 4.10
 Calculate (A + B) − (E + D) where A, B, E and D are
words stored in data memory. The result is also to
be saved in the data segment.
 Use BP for accessing the stack
 Note We do not use C as a label because ‘C’ is a
reserved word in MASM
FIGURE 4.5 | STACK OPERATION FOR THE
PUSH AND RET 8 INSTRUCTIONS OF EXAMPLE
4.10
STACK OVERFLOW AND
UNDERFLOW
 SP can have a maximum value of FFFFH. For each
PUSH operation, the SP value decrements by 2,
and in the limiting case, it can go to SP = 0000.
 Any PUSH operation beyond this will cause a
‘stack overflow’.
 This creates a condition when there is no space in
the stack for new data.
 Stack underflow is the other case when POP
operations cause SP to have values beyond the
defined stack.
MACROS
 What is a macro? It is like an opcode – when used,
it executes.
 A macro when called by name, executes the
instructions that are listed under that name.
 Thus essentially, a macro is a short hand notation
for a number of instruction lines.
 It makes assembly language coding more readable
and helps to avoid repetitive coding.
WRITING A MACRO –THE FORMAT
MACRO-NAME MACRO [parameter list]
Instructions (body of the macro)
ENDM
Macros Procedures
Accessed during assembly when
name given to macro is written as
an instruction in the assembly
program.
Accessed by CALL and RET
instructions during program
execution.
Machine code is generated for
instructions each time a macro is
called.
Machine code for instructions is put
only once in the memory.
This due to repeated generation of
machine code requires more
memory.
This as all machine code is defined
only once so less memory is
required.
Parameters are passed as a part of
the statement in which macro is
called.
Parameters can be passed in
register memory location or stack.

More Related Content

What's hot

system software 16 marks
system software 16 markssystem software 16 marks
system software 16 marksvvcetit
 
Instruction sets of 8086
Instruction sets of 8086Instruction sets of 8086
Instruction sets of 8086Mahalakshmiv11
 
Assembler directives and basic steps ALP of 8086
Assembler directives and basic steps ALP of 8086Assembler directives and basic steps ALP of 8086
Assembler directives and basic steps ALP of 8086Urvashi Singh
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 80869840596838
 
N_Asm Assembly arithmetic instructions (sol)
N_Asm Assembly arithmetic instructions (sol)N_Asm Assembly arithmetic instructions (sol)
N_Asm Assembly arithmetic instructions (sol)Selomon birhane
 
Assembly language 8086
Assembly language 8086Assembly language 8086
Assembly language 8086John Cutajar
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...Bilal Amjad
 
Assembly Language Basics
Assembly Language BasicsAssembly Language Basics
Assembly Language BasicsEducation Front
 
Unit 3 – assembly language programming
Unit 3 – assembly language programmingUnit 3 – assembly language programming
Unit 3 – assembly language programmingKartik Sharma
 
8086 assembly language
8086 assembly language8086 assembly language
8086 assembly languageMir Majid
 
Arrays and addressing modes
Arrays and addressing modesArrays and addressing modes
Arrays and addressing modesBilal Amjad
 
Assembly language 8086 intermediate
Assembly language 8086 intermediateAssembly language 8086 intermediate
Assembly language 8086 intermediateJohn Cutajar
 
Compiler Design File
Compiler Design FileCompiler Design File
Compiler Design FileArchita Misra
 
Lec 04 intro assembly
Lec 04 intro assemblyLec 04 intro assembly
Lec 04 intro assemblyAbdul Khan
 
Assemblers: Ch03
Assemblers: Ch03Assemblers: Ch03
Assemblers: Ch03desta_gebre
 
Computer architecture instruction formats
Computer architecture instruction formatsComputer architecture instruction formats
Computer architecture instruction formatsMazin Alwaaly
 

What's hot (20)

Wk1to4
Wk1to4Wk1to4
Wk1to4
 
system software 16 marks
system software 16 markssystem software 16 marks
system software 16 marks
 
Instruction sets of 8086
Instruction sets of 8086Instruction sets of 8086
Instruction sets of 8086
 
Assembler directives and basic steps ALP of 8086
Assembler directives and basic steps ALP of 8086Assembler directives and basic steps ALP of 8086
Assembler directives and basic steps ALP of 8086
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
 
Instruction formats-in-8086
Instruction formats-in-8086Instruction formats-in-8086
Instruction formats-in-8086
 
N_Asm Assembly arithmetic instructions (sol)
N_Asm Assembly arithmetic instructions (sol)N_Asm Assembly arithmetic instructions (sol)
N_Asm Assembly arithmetic instructions (sol)
 
Assembly language 8086
Assembly language 8086Assembly language 8086
Assembly language 8086
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
 
Assembly Language Basics
Assembly Language BasicsAssembly Language Basics
Assembly Language Basics
 
Unit 3 – assembly language programming
Unit 3 – assembly language programmingUnit 3 – assembly language programming
Unit 3 – assembly language programming
 
8086 assembly language
8086 assembly language8086 assembly language
8086 assembly language
 
Arrays and addressing modes
Arrays and addressing modesArrays and addressing modes
Arrays and addressing modes
 
Instruction set
Instruction setInstruction set
Instruction set
 
Assembly language 8086 intermediate
Assembly language 8086 intermediateAssembly language 8086 intermediate
Assembly language 8086 intermediate
 
Compiler Design File
Compiler Design FileCompiler Design File
Compiler Design File
 
Lec 04 intro assembly
Lec 04 intro assemblyLec 04 intro assembly
Lec 04 intro assembly
 
Assemblers: Ch03
Assemblers: Ch03Assemblers: Ch03
Assemblers: Ch03
 
ADDRESSING MODE
ADDRESSING MODEADDRESSING MODE
ADDRESSING MODE
 
Computer architecture instruction formats
Computer architecture instruction formatsComputer architecture instruction formats
Computer architecture instruction formats
 

Similar to Chapter 4 programming concepts III

Describe the three main addressing modes of the PIC16 architecture di.pdf
Describe the three main addressing modes of the PIC16 architecture di.pdfDescribe the three main addressing modes of the PIC16 architecture di.pdf
Describe the three main addressing modes of the PIC16 architecture di.pdfSALES97
 
address5ng modes.pptx IS A GOOD MATERIAL
address5ng  modes.pptx IS A GOOD MATERIALaddress5ng  modes.pptx IS A GOOD MATERIAL
address5ng modes.pptx IS A GOOD MATERIALDrkoteswararaoseelam
 
8086 instruction set (with simulator)
8086 instruction set (with simulator)8086 instruction set (with simulator)
8086 instruction set (with simulator)Aswini Dharmaraj
 
Compreport
CompreportCompreport
Compreportxdarlord
 
Assembly language.pptx
Assembly language.pptxAssembly language.pptx
Assembly language.pptxShaistaRiaz4
 
Mastering Assembly Language: Programming with 8086
Mastering Assembly Language: Programming with 8086Mastering Assembly Language: Programming with 8086
Mastering Assembly Language: Programming with 8086sravanithonta79
 
Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutineAshim Saha
 
Module 2 instruction set
Module 2 instruction set Module 2 instruction set
Module 2 instruction set Deepak John
 
Notes 8086 instruction format
Notes 8086 instruction formatNotes 8086 instruction format
Notes 8086 instruction formatHarshitParkar6677
 
Unit i ca- mips addressing
Unit i  ca- mips addressingUnit i  ca- mips addressing
Unit i ca- mips addressingPraba haran
 

Similar to Chapter 4 programming concepts III (20)

Co&al lecture-07
Co&al lecture-07Co&al lecture-07
Co&al lecture-07
 
Chapter 5 notes
Chapter 5 notesChapter 5 notes
Chapter 5 notes
 
Describe the three main addressing modes of the PIC16 architecture di.pdf
Describe the three main addressing modes of the PIC16 architecture di.pdfDescribe the three main addressing modes of the PIC16 architecture di.pdf
Describe the three main addressing modes of the PIC16 architecture di.pdf
 
address5ng modes.pptx IS A GOOD MATERIAL
address5ng  modes.pptx IS A GOOD MATERIALaddress5ng  modes.pptx IS A GOOD MATERIAL
address5ng modes.pptx IS A GOOD MATERIAL
 
8086 instruction set (with simulator)
8086 instruction set (with simulator)8086 instruction set (with simulator)
8086 instruction set (with simulator)
 
Buffer overflow attack
Buffer overflow attackBuffer overflow attack
Buffer overflow attack
 
Compreport
CompreportCompreport
Compreport
 
Assembly language.pptx
Assembly language.pptxAssembly language.pptx
Assembly language.pptx
 
MES_MODULE 2.pptx
MES_MODULE 2.pptxMES_MODULE 2.pptx
MES_MODULE 2.pptx
 
Mastering Assembly Language: Programming with 8086
Mastering Assembly Language: Programming with 8086Mastering Assembly Language: Programming with 8086
Mastering Assembly Language: Programming with 8086
 
Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutine
 
Lecture5
Lecture5Lecture5
Lecture5
 
Lecture5(1)
Lecture5(1)Lecture5(1)
Lecture5(1)
 
Module 2 instruction set
Module 2 instruction set Module 2 instruction set
Module 2 instruction set
 
Notes 8086 instruction format
Notes 8086 instruction formatNotes 8086 instruction format
Notes 8086 instruction format
 
8086 instruction set
8086 instruction set8086 instruction set
8086 instruction set
 
Chapter 6 notes
Chapter 6 notesChapter 6 notes
Chapter 6 notes
 
Instruction-Sets-2.pptx
Instruction-Sets-2.pptxInstruction-Sets-2.pptx
Instruction-Sets-2.pptx
 
Unit i ca- mips addressing
Unit i  ca- mips addressingUnit i  ca- mips addressing
Unit i ca- mips addressing
 
Chapter 5 notes new
Chapter 5 notes newChapter 5 notes new
Chapter 5 notes new
 

More from SHREEHARI WADAWADAGI

More from SHREEHARI WADAWADAGI (15)

Chapter 15 software product metrics
Chapter 15 software product metricsChapter 15 software product metrics
Chapter 15 software product metrics
 
Chapter 14 software testing techniques
Chapter 14 software testing techniquesChapter 14 software testing techniques
Chapter 14 software testing techniques
 
Chapter 13 software testing strategies
Chapter 13 software testing strategiesChapter 13 software testing strategies
Chapter 13 software testing strategies
 
Chapter 12 user interface design
Chapter 12 user interface designChapter 12 user interface design
Chapter 12 user interface design
 
Chapter 21 project management concepts
Chapter 21 project management conceptsChapter 21 project management concepts
Chapter 21 project management concepts
 
Ch 11-component-level-design
Ch 11-component-level-designCh 11-component-level-design
Ch 11-component-level-design
 
Ch 9-design-engineering
Ch 9-design-engineeringCh 9-design-engineering
Ch 9-design-engineering
 
An introduction to software engineering
An introduction to software engineeringAn introduction to software engineering
An introduction to software engineering
 
Architectural design
Architectural designArchitectural design
Architectural design
 
Chapter 1 archietecture of 8086
Chapter 1 archietecture of 8086Chapter 1 archietecture of 8086
Chapter 1 archietecture of 8086
 
Brief description of all the interupts
Brief description of all the interuptsBrief description of all the interupts
Brief description of all the interupts
 
Chapter 7 memory & i/o
Chapter 7  memory & i/oChapter 7  memory & i/o
Chapter 7 memory & i/o
 
Chapter 6 hardware structure of 8086
Chapter 6  hardware structure of 8086Chapter 6  hardware structure of 8086
Chapter 6 hardware structure of 8086
 
Arm processor
Arm processorArm processor
Arm processor
 
8086 complete guide
8086 complete guide 8086 complete guide
8086 complete guide
 

Recently uploaded

Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniquesugginaramesh
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 

Recently uploaded (20)

Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniques
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 

Chapter 4 programming concepts III

  • 2. STRING INSTRUCTIONS  The 8086 has a set of instructions for handling blocks of data in the form of bytes or words.  They are called ‘string’ instructions.  A string is an array of data of the same type – for example, a character string or a byte string  The usefulness of string instructions can be seen when in the memory data has to be moved, searched or compared in blocks.
  • 3. PRE-REQUISITES FOR USING STRING INSTRUCTIONS i. DS and ES are to be defined: Initializing & using these segment makes DS as the source & ES as the destination segment ii. SI and DI acts as pointers: SI to point to DS & DI top point to ES, means SI should contain the address (offset) of the first location in the DS and DI should contain the address of the first location in ES iii. DF should be set/reset: control flag called as direction flag, used exclusively for string operations. Purpose in string operation is, if flag is set, the pointers get automatically decremented, and if reset, the reverse occurs. So, during string instructions are used, DF should be set/reset depending on the direction. iv. CX should act as a counter: counter CX should be loaded with the count of number of operations required.
  • 4. LIST OF INSTRUCTIONS/PREFIXES USED IN STRING OPERATIONS
  • 5.
  • 6. THE MOVS INSTRUCTION  Can be used as MOVSB or MOVSW  It moves data from the source segment (data segment) to the destination segment (extra segment)  CX acts as the count register  DF is to be set/reset  SI and DI have to be intialiased to the source address in the data segment ,and the destination address in the extra segment
  • 7. MOVING A STRING -USING THE FULL SEGMENT MODEL
  • 8.  Two segments DAT & EXTR are defined  Two segment registers DS & ES are initialized  SI & DI point to the source data & destination data  CLD is used to clear the DF for auto incrementing the pointer registers  This causes the value of SI and DI to be incremented after each move operation  Here, SI & DI are incremented only by one each time  String instruction is used here is MOVSB  This causes the data in location pointed by SI to be moved to the location pointed by DI  After each such move operation, two actions occur  One is that CX decrements by 1 and the other is that the pointer registers are incremented  This sequence continues until CX=0
  • 10. THE CMPS INSTRUCTION  This instruction is for string comparison, byte by byte or word by word as the case may be. The conditional flags are modified according to the result of the comparison. String comparison has to be accompanied by the use of the conditional REP prefix. Since string comparison checks only for equality, the Zero flag is made use of automatically.
  • 11.  Example  Compare two character strings (of length six) which are saved in the data segment.  One of two messages is to be displayed, depending on whether the strings are equal or not.
  • 12.
  • 13. THE SCAS INSTRUCTION  The SCAS instruction scans a byte or word string to ascertain the presence of a specific byte or word. This specific data is loaded into AL or AX.  The string which is to be scanned has to be in the extra segment, and is to be pointed by DI. This is mandatory and cannot be overridden.
  • 14.  Write a program to search for a character in an ASCII string .  Express the result using appropriate messages
  • 16. THE STOS AND LODS INSTRUCTIONS  i) STOS  The STOS instruction is the mnemonic for ‘storing’ a string in memory.  As such, we need to define a memory area in which ‘storing’ is to be done. This memory area is defined to be the extra segment, and it is addressed by DI as it is the destination segment.  The data to be stored is placed in the AL or AX register.  An area in memory can be filled with the required data, with this instruction.
  • 17.  Write a program to fill an area of memory with a specific word
  • 19. THE LODS INSTRUCTION  This is an instruction for ‘loading’.  Here, the source memory is the data segment and the pointer to it is SI  The data segment is the source segment and the destination register is the AL or AX register.  There is no sense in using the REP prefix for the LODS instruction as data can be loaded to AL/AX only once.
  • 20. PROCEDURES When a main program calls a subsidiary program ,the latter is called a procedure  To avoid writing the sequence of instructions in the program each time you need them, you can write the sequence as a separate subprogram called a procedure  You use the CALL instruction to send the 8086 to the starting address of the procedure in memory
  • 21. CONTD..  A RET instruction at the end of the procedure returns execution to the next instruction in the main line  Procedures can even be nested
  • 22. THE SEQUENCE OF CALLING PROCEDURES  Near procedure –which resides in the same code segment as the main (calling) program  A procedure is called by a CALL instruction  At the time the CALL instruction is being executed, the IP (instruction pointer) will be pointing to the next instruction in the main program
  • 23. THE STEPS TAKEN BY THE PROCESSOR FOR CALLING PROCEDURES  It saves the current IP content on the stack (this is the ‘return’ address for coming back to the main program after executing the procedure).  The CALL destination (specified in the CALL instruction) will be the address of the procedure. The IP is now loaded with this address and execution proceeds from that location  The procedure is executed until a RET (return) instruction in the procedure is encountered.  Then, the old value of the IP is retrieved from the stack and control returns to the main program at the return address.
  • 25. 25 MAINLINE OR CALLING PROGRAM NEXT MAINLINE INSTRUCTIONS PROCEDURE INSTRUCTIONS CALL RET Fig. Single Procedure Call
  • 26. 26 Main Line Instructions RET Lower level Procedure CALL RET Procedure CALL Next Main Line Instructions Fig. Nested Procedures Call
  • 27. WRITING A PROCEDURE  The procedure should begin with the procedure name followed by the directive PROC.  We also use the directives NEAR or FAR which specify the ‘type’ of the procedure.  The procedure should end with the procedure name and ENDP.
  • 28. EXAMPLE 4.5  Write a program, which enters 10 single digit numbers through the keyboard, finds their squares and stores the squares in memory.  Only byte locations need be allocated in memory for the squares as the square of the highest single digit number is 81, which will fi t into a byte space
  • 30. THE CALL INSTRUCTION  Intrasegment or ‘Near’ Call  i) Direct CALL Usage: CALL label  The direct call is like a direct jump instruction, and is three bytes long. It is relative and the destination can be −32,768 bytes to +32,767 bytes from the address of the instruction following  the call (this will be the current content of the IP).  the off set can be a 16-bit signed number.  When this call is executed, the new value of IP = old IP + off set,
  • 31. FIGURE 4.3 | FORMAT OF THE DIRECT NEAR CALL INSTRUCTION
  • 32. II) INDIRECT CALL  Usage: CALL reg16, CALL [reg16] In this case, the destination is specified in a 16-bit register or in a memory location pointed by a register.  This is not a ‘relative’ call. The content of the referred register or memory location is loaded into IP for using the procedure.  Examples  CALL BX ;the procedure’s address is in BX  CALL WORD PTR[BX] ;the address of the procedure is in memory and is pointed by BX
  • 34. INTERSEGMENT OR FAR CALL  Direct Far Call A far call is an intersegment call, which means that the destination address is in a different code segment.  This will be a 5-byte instruction, the first byte being the opcode, the second and third bytes being the new value of IP, and the fourth and fi fth, the new values of CS.  This is not a relative call.  When the procedure is called, the IP and CS values are replaced by the corresponding values in the call instruction
  • 35. FIGURE 4.4 | FORMAT OF THE FAR JUMP INSTRUCTION
  • 36. INDIRECT FAR CALL • The destination address is not in the instruction – rather, it is in a register or memory.  For a far call, four bytes are needed to specify a destination. a register cannot specify it. Hence the four bytes needed to specify a destination are stored in memory and pointed by a register.
  • 37.  Example of an indirect far call  CALL DWORD PTR [SI] can be a far call instruction. [SI] and [SI + 1] gives the new value of IP and [SI + 2] and [SI + 3] gives the new value of CS.  Far calls and procedures are specified using the ‘far’ directive when defining the procedure.  In Chapter 5, we will see how the EXTRN directive is used in this context.
  • 38. THE RET INSTRUCTION  i) Usage: RET  The execution of RET causes the return address to be popped from the stack to IP or IP and CS.  (Whenever a procedure is defined, it is known whether it is a far or near procedure. From this, it is determined whether the stack has saved just the old IP value or both IP and CS.)
  • 39. RET N  Usage: RET n  This is another form of the RET instruction. This adds the number ‘n’ to the stack pointer (SP) after the return address has been popped off the stack, on return from a procedure
  • 40. THE USE OF THE STACK IN PROCEDURE CALLS  When both the main program and the procedures need the same registers, the content of the registers are pushed onto the stack when going to a procedure ,and popped back on returning.  MASM 6.x has a construct ‘USES’ which automatically pushes the specified registers onto the stack.  The popping will be automatic on returning from the procedure.
  • 41. EXAMPLE 4.7A –THE USES DIRECTIVE
  • 43. PASSING PARAMETERS TO AND FROM PROCEDURES Can be done using o Registers o Memory and memory pointers o Stack
  • 44. EXAMPLE 4.8  Write a program which calculates and places in memory the Nth term of an arithmetic progression. The formula for the Nth term of an A.P. is A + (N − 1) D, where A is the fi rst term and D is the common difference.  Pass the values of the operands using registers
  • 46. PASSING PARAMETERS THROUGH MEMORY  Example 4.9 is a program for arranging a set of numbers (stored in memory) in descending order.  The values are passed to the program through memory pointers.  The method involves comparing numbers pair-wise and repeating this N − 1 times, if there are N numbers to be sorted.
  • 48. PASSING PARAMETERS THROUGH THE STACK  There may be reasons where the stack, which is an area in the memory, can be used to store data. The procedure can take data from the stack for its computation.  Example 4.10 shows BP being used to access data in the stack  Here, four words which are in the data segment are pushed on to the stack by the main program.  The procedure accesses the data using the indexing features of BP. Remember that BP is a register associated (by default) with the stack segment, and that BP cannot be used without an off set.
  • 49. EXAMPLE 4.10  Calculate (A + B) − (E + D) where A, B, E and D are words stored in data memory. The result is also to be saved in the data segment.  Use BP for accessing the stack  Note We do not use C as a label because ‘C’ is a reserved word in MASM
  • 50.
  • 51. FIGURE 4.5 | STACK OPERATION FOR THE PUSH AND RET 8 INSTRUCTIONS OF EXAMPLE 4.10
  • 52. STACK OVERFLOW AND UNDERFLOW  SP can have a maximum value of FFFFH. For each PUSH operation, the SP value decrements by 2, and in the limiting case, it can go to SP = 0000.  Any PUSH operation beyond this will cause a ‘stack overflow’.  This creates a condition when there is no space in the stack for new data.  Stack underflow is the other case when POP operations cause SP to have values beyond the defined stack.
  • 53. MACROS  What is a macro? It is like an opcode – when used, it executes.  A macro when called by name, executes the instructions that are listed under that name.  Thus essentially, a macro is a short hand notation for a number of instruction lines.  It makes assembly language coding more readable and helps to avoid repetitive coding.
  • 54. WRITING A MACRO –THE FORMAT MACRO-NAME MACRO [parameter list] Instructions (body of the macro) ENDM
  • 55.
  • 56.
  • 57. Macros Procedures Accessed during assembly when name given to macro is written as an instruction in the assembly program. Accessed by CALL and RET instructions during program execution. Machine code is generated for instructions each time a macro is called. Machine code for instructions is put only once in the memory. This due to repeated generation of machine code requires more memory. This as all machine code is defined only once so less memory is required. Parameters are passed as a part of the statement in which macro is called. Parameters can be passed in register memory location or stack.