Upload
vannga
View
216
Download
0
Embed Size (px)
Citation preview
STRING OPERATIONS
SORTING OF ‘N’ NUMBERS
ABSTRACT: Assembly language program to do sorting of numbers in a given series
APPARATUS: Personal computer with TASM software
ALGORITHM:
Step1: StartStep2: Initialize data segment Step3: Load CX register with countStep4: Copy the contents from CX to DX Step5: Load SI with offset listStep6: Copy the contents from DX to CXStep7: Move the contents from memory location SI to AL Step8: Increment SIStep9: Compare AL contents with [SI] Step10: Jump to step15 if carryStep11: Exchange the contents of AL with [SI] Step12: Decrement SIStep13: Move the contents from AL to memory location SI Step14: Increment SIStep15: Decrement CX and jump to step7 if no zero Step16: Decrement DX and jump to step5 if no zeroStep17: Stop
PROGRAM:
ASSUME CS: CODE, DS: DATADATA SEGMENTLIST DB 56H, 12H, 72H,32HCOUNT EQU 0003HDATA ENDSCODE SEGMENTORG 1000HSTART: MOV AX, DATA
MOV DS, AXMOV CX, COUNTMOV DX, CX
AGAIN: MOV SI, OFFSET LISTMOV CX, DX
BACK: MOV AL, [SI]INC SICMP AL, [SI]JC NEXTXCHG [SI], ALDEC SIMOV [SI], ALINC SI
NEXT: LOOP BACKDEC DXJNZ AGAININT 03
CODE ENDSEND START
THEORY:
Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order. This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2) where n is the number of items.
Bubble sorting method:
We take an unsorted array for our example. Bubble sort takes Ο(n2) time so we're keeping it
short and precise. 14 33 27 35 10
Bubble sort starts with very first two elements, comparing them to check which one is
greater. 14 33 27 35 10
In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we
compare 33 with 27. 14 33 27 35 10
We find that 27 is smaller than 33 and these two values must be swapped. 14 33 27 35 10
The new array should look like this − 14 27 33 35 10
Next we compare 33 and 35. We find that both are in already sorted positions.
14 27 33 35 10Then we move to the next two values, 35 and 10.
14 27 33 35 10We know then that 10 is smaller 35. Hence they are not sorted.
14 27 33 10 35
We swap these values. We find that we have reached the end of the array. After one iteration, the array should look like this −
To be precise, we are now showing how an array should look like after each iteration. After the second iteration, it should look like this −
14 27 10 33 35Notice that after each iteration, at least one value moves at the end.
14 10 27 33 35And when there's no swap required, bubble sorts learns that an array is completely sorted.
Cmp:Compare two operands Syntax: cmp op1, op2 op1: register or memory op2: register, memory, or immediate Action: Perform op1-op2, discarding the result but setting the flags. Flags Affected: OF, SF, ZF, AF, PF, CF Notes: Usually used before a conditional jump instruction.
XCHG REG,memory memory,REG REG,REG
Exchange values of two operands.
Algorithm:
operand1 < - > operand2
PROCEDURE:
1. Open command prompt.
2. Change into TASM software Directory.
3. Type the program using DOS editor.
4. Save the program as ASM file.
5. Convert the ASM file into OBJ file using TASM.
6. Convert OBJ file into EXE file using TLINK.
7. Debug the EXE file using DOS Debugger or TD.
8. Run the program using F7(single step) or F9(at a time).
9. Observe the results in the appropriate segment.
10. Compare the result with theoretical calculations
CODE TABLE:Physical Address
Hex Code Label Mnemonic operand CommentsSegment OffsetAddress Address
ASSUME CS: CODE, DS: DATA
DATA SEGMENT
LIST DB 56H, 12H, 72H,32H
COUNT EQU 0003H
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV CX, COUNT
MOV DX, CX
AGAIN: MOV SI, OFFSET LIST
MOV CX, DX
BACK:MOV AL, [SI]
INC SI
CMP AL, [SI]
JC NEXT
XCHG [SI], AL
DEC SI
MOV [SI], AL
INC SI
NEXT:LOOP BACK
DEC DX
JNZ AGAIN
INT 03
CODE ENDS
END START
RESULT:
MOVE BLOCK OF DATA
ABSTRACT: Assembly language program to transfer a block of data
APPARATUS: Personal computer with TASM software.
ALGORITHM:Step1: Start
Step2: Initialize data segment & extra segment
Step3: Define the String
Step4: Load CX register with length of the String
Step5: Initialize DI with memory location
Step6: Load SI with offset list
Step7: Repeat the process of moving string byte from SI to DI until CX equals to zero
Step8: Stop
PROGRAM:
ASSUME CS: CODE, DS: DATA, ES: EXTRADATA SEGMENTLIST DB ‘narasaraopet’COUNT EQU 06HDATA ENDSEXTRA SEGMENTDEST DB ?EXTRA ENDSCODE SEGMENTSTART: MOV AX, DATA
MOV DS, AXMOV AX,EXTRAMOV ES, AXMOV CX, COUNTLEA DI, DESTLEA SI, LISTCLDREP MOVSBINT 03
CODE ENDSEND START
THEORY:
Lea:Load effective address offset Syntax: lea reg16, memref reg16: 16-bit register memref: An effective memory address (e.g., [bx+2]) Action: reg16 = address offset of memref Flags Affected: None Notes: This instruction is used to easily calculate the address of data in memory. It does not actually access memory.
CLD: Clear Direction flag.
SI and DI will be incremented by chain instructions: CMPSB, CMPSW, LODSB, LODSW, MOVSB, MOVSW, STOSB, STOSW.
Algorithm:
DF = 0
REPE chain instructionRepeat following CMPSB, CMPSW, SCASB, SCASW instructions while ZF = 1 (result is Equal), maximum CX times.
Algorithm:
check_cx:
if CX > 0 thendo following chain instructionCX = CX - 1if ZF = 1 then:
go back to check_cxelse
exit from REPE cycleelse
exit from REPE cycle
MOVSB No operands
Copy byte at DS:[SI] to ES:[DI]. Update SI and DI.
Algorithm:
ES:[DI] = DS:[SI]if DF = 0 then
SI = SI + 1DI = DI + 1
elseSI = SI - 1DI = DI - 1
PROCEDURE:1. Open command prompt.
2. Change into TASM software Directory.
3. Type the program using DOS editor.
4. Save the program as ASM file.
5. Convert the ASM file into OBJ file using TASM.
6. Convert OBJ file into EXE file using TLINK.
7. Debug the EXE file using DOS Debugger or TD.
8. Run the program using F7(single step) or F9(at a time).
9. Observe the results in the appropriate segment.
10. Compare the result with theoretical calculations
CODE TABLE:Physical Address
Hex Code Label Mnemonic operand CommentsSegment OffsetAddress Address
ASSUMECS: CODE, DS:DATA, ES: EXTRA
DATA SEGMENT
LIST DB ‘ELECTRONICS’
COUNT EQU 0BH
DATA ENDS
EXTRA SEGMENT
DEST DB ?
EXTRA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV AX,EXTRA
MOV ES, AX
MOV CX, COUNT
MOV DI, DEST
LEA SI, LIST
CLD
REP MOVSB
MOV AH, 4CH
INT 21H
CODE ENDS
END START
RESULT:
LENGTH OF THE GIVEN STRING
ABSTRACT: Assembly language program to find the Length of a string
APPARATUS: Personal computer with TASM software.
ALGORITHM:
Step1: Start
Step2: Initialize data segment & extra segment
Step3: Load AL with ‘$’
Step4: Load SI with offset list
Step5: Initialize DX with 0000
Step6: Scan string byte from DI memory location until AL =ES: DI
Step7: Jump to step10 if equal
Step8: Increment DX
Step9: Jump to step6
Step10: Store the result to the memory location
Step11: Stop
PROGRAM:
ASSUMCS: CODE, ES: EXTRAEXTRA SEGMENTLIST DB ‘MICROPROCESSOR’LEN DW ?EXTRA ENDSCODE SEGMENTORG 1000HSTART: MOV AX, EXTRA
MOV ES, AXMOV AL,’$’LEA DI, LISTMOV DX, 0000HCLD
BACK: SCASBJE EXITINC DXJMP BACK
EXIT: MOV LEN, DXMOV AH, 4CHINT 21H
CODE ENDSEND START
THEORY:
Lea:Load effective address offset Syntax: lea reg16, memref reg16: 16-bit register memref: An effective memory address (e.g., [bx+2]) Action: reg16 = address offset of memref Flags Affected: None Notes: This instruction is used to easily calculate the address of data in memory. It does not actually access memory.
CLD: Clear Direction flag.
SI and DI will be incremented by chain instructions: CMPSB, CMPSW, LODSB, LODSW,MOVSB,MOVSW,STOSB,STOSW.
Algorithm:
DF = 0
SCASB:Scan string byte wise.
SCASB No operands
Compare bytes: AL from ES:[DI].
Algorithm:
ES:[DI] - ALset flags according to result:OF, SF, ZF, AF, PF, CFif DF = 0 then
DI = DI + 1else
DI = DI - 1
PROCEDURE:1. Open command prompt.2. Change into TASM software Directory.3. Type the program using DOS editor.4. Save the program as ASM file.5. Convert the ASM file into OBJ file using TASM.6. Convert OBJ file into EXE file using TLINK.7. Debug the EXE file using DOS Debugger or TD.8. Run the program using F7(single step) or F9(at a time).9. Observe the results in the appropriate segment.10. Compare the result with theoretical calculations
CODE TABLE:Physical Address
Hex Code Label Mnemonic operand CommentsSegment OffsetAddress Address
ASSUMCS: CODE, ES: EXTRA
EXTRA SEGMENT
LIST DB ‘ECENECNRT’
LEN DW ?
EXTRA ENDS
CODE SEGMENT
START: MOV AX, EXTRA
MOV ES, AX
MOV AL,’$’
LEA DI, LIST
MOV DX, 0000H
CLD
BACK: SCASB
JE EXIT
INC DX
JMP BACK
EXIT: MOV LEN, DX
MOV AH, 4CH
INT 21H
CODE ENDS
END START
RESULT:
COMPARISON OF TWO STRINGS
ABSTRACT: Assembly language program to compare two strings.
APPARATUS: Personal computer with TASM software.
ALGORITHM:
Step1: StartStep2: Initialize data segment & extra segmentStep3: Load AX with length of String 1Step4: Load BX with length of String 2Step5: Compare AX with BXStep6: Jump step14 if not equalStep7: Copy the contents from AX to CXStep8: Load SI with first location of string 1Step9: Load DI with first location of string 2Step10: Repeat comparing string byte until count equals to zeroStep11: jump to step 14 if not equalStep12: Store the result to the data memoryStep13: Jump to step 15Step14: Store another result to the data memoryStep15: Stop
PROGRAM:
ASSUME CS: CODE, DS: DATA, ES: EXTRADATA SEGMENTORG 1000HLIST1 DB ‘NARASARAOPET’LEN1 EQU ($-LIST1)RESULT DW ?DATA ENDSEXTRA SEGMENTORG 5000HLIST2 DB ‘microprocessor’LEN2 EQU ($-LIST2)EXTRA ENDSCODE SEGMENTORG 1000HSTART: MOV AX, DATA
MOV DS, AXMOV AX,EXTRAMOV ES, AXMOV AX.LEN1MOV BX, LEN2CMP AX, BXJNE EXITMOV CX, AXMOV SI, OFFSET LIST1MOV DI, OFFSET LIST2CLDREP CMPSBJNE EXITMOV RESULT,0FFFFHJMP NEXT
EXIT: MOV RESULT, 0000HNEXT:MOV AH, 4CH
INT 21HCODE ENDSEND START
THEORY:
CLD: Clear Direction flag.
SI and DI will be incremented by chain instructions: CMPSB, CMPSW, LODSB, LODSW, MOVSB, MOVSW, STOSB, STOSW.
Algorithm:
DF = 0
REPE chain instructionRepeat following CMPSB, CMPSW, SCASB, SCASW instructions while ZF = 1 (result is Equal), maximum CX times.
Algorithm:
check_cx:
if CX > 0 thendo following chain instructionCX = CX - 1if ZF = 1 then:
go back to check_cxelse
exit from REPE cycleelse
exit from REPE cycle
CMPSB: compare strings byte wiseCompare bytes: ES:[DI] from DS:[SI].
Algorithm:
DS:[SI] - ES:[DI]set flags according to result:OF, SF, ZF, AF, PF, CFif DF = 0 then
SI = SI + 1DI = DI + 1
elseSI = SI - 1DI = DI - 1
PROCEDURE:1. Open command prompt.2. Change into TASM software Directory.3. Type the program using DOS editor.4. Save the program as ASM file.5. Convert the ASM file into OBJ file using TASM.6. Convert OBJ file into EXE file using TLINK.7. Debug the EXE file using DOS Debugger or TD.8. Run the program using F7(single step) or F9(at a time).9. Observe the results in the appropriate segment.10. Compare the result with theoretical calculations
Physical Address
Hex Code Label Mnemonic operand CommentsSegment OffsetAddress Address
ASSUME CS: CODE, DS: DATA, ES: EXTRA
DATA SEGMENT
LIST1 DB ‘ECENECNRT’
LEN1 EQU ($-LIST1)
RESULT DW ?
DATA ENDS
EXTRA SEGMENT
LIST2 DB ‘NECECE’
LEN2 EQU ($-LIST2)
EXTRA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV AX,EXTRA
MOV ES, AX
MOV AX.LEN1
MOV BX, LEN2
CMP AX, BX
JNE EXIT
MOV CX, AX
MOV SI, OFFSET LIST1
MOV DI, OFFSET LIST2
CLD
REP CMPSB
JNE EXIT
MOV RESULT, 0FFFFH
JMP NEXT
EXIT:MOV RESULT, 0000H
NEXT:INT 03H
CODE ENDS;END START
RESULT:
REVERSAL OF GIVEN STRINGABSTRACT: Assembly language program to reverse a given string
APPARATUS: Personal computer with TASM software.
ALGORITHM:
Step1: StartStep2: Initialize data segment & code segmentStep3: Load starting address of the string in SIStep4: Obtain end address in DI.Step5: Get 1st character of the string into ALStep6: Exchange it with final character.Step7: Increment SI and decrement DIStep8: Repeat steps 6 & 7 until SI less than DI.Step9: Verify Result.Step10: Stop
PROGRAM:
ASSUME CS:CODE,DS:DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV SI, OFFSET STRING
MOV DI,SI
ADD DI,LEN-1
L1: MOV AL,[SI]
XCHG AL,[DI]
MOV [SI],AL
INC SI
DEC DI
CMP SI,DI
JL L1
INT 03H
CODE ENDS
DATA SEGMENT
STRING DB 'ABCDEFGH123456789'
LEN EQU $-STRING
DATA ENDS
END
THEORY:
Cmp:Compare two operands Syntax: cmp op1, op2 op1: register or memory op2: register, memory, or immediate Action: Perform op1-op2, discarding the result but setting the flags. Flags Affected: OF, SF, ZF, AF, PF, CF Notes: Usually used before a conditional jump instruction.
XCHG REG,memory memory,REG REG,REG
Exchange values of two operands.
Algorithm:
operand1 < - > operand2
PROCEDURE:1. Open command prompt.
2. Change into TASM software Directory.
3. Type the program using DOS editor.
4. Save the program as ASM file.
5. Convert the ASM file into OBJ file using TASM.
6. Convert OBJ file into EXE file using TLINK.
7. Debug the EXE file using DOS Debugger or TD.
8. Run the program using F7(single step) or F9(at a time).
9. Observe the results in the appropriate segment.
10. Compare the result with theoretical calculations
CODE TABLE:
Physical Address
Hex Code Label Mnemonic operand CommentsSegment OffsetAddress Address
ASSUME CS:CODE,DS:DATA
CODE SEGMENT
MOV AX,DATA
MOV DS,AX
MOV SI,OFFSET STRING
MOV DI,SI
ADD DI,LEN-1
L1: MOV AL,[SI]
XCHG AL,[DI]
MOV [SI],AL
INC SI
DEC DI
CMP SI,DI
JL L1
INT 03H
CODE ENDS
DATA SEGMENT
STRING DB 'ABCDEFGH123456789'
LEN EQU $-STRING
DATA ENDS
END
RESULT:
CORRECT MISSPELT WORD
AIM: To Implement Assembly language program for correction(insertion and deletion)of a
word.
APPARATUS: Personal computer with TASM software.
ALGORITHM:
Step1: StartStep2: Initialize code segment and data segmentStep3: Load right word address in SI.Step4: Load wrong word address in DI.Step5: Compare two words until mismatch occurs.Step6: Call procedure when mismatch.Step7: Substitute right character in place of wrong character.Step8: Move rest of the string one position right.Step9: Resume comparing wordsStep10: Repeat steps 5,6,7,8 &9 upto final character.Step11: Verify the resultStep12: Stop
PROGRAM:
ASSUME CS:CODE,DS:DATA
CODE SEGMENT
START:MOV AX,DATA
MOV DS,AX
LEA SI,TRUE
MOV CX,LEN
LEA DI,WRONG
CLD
LOOP2:REPZ
CMPSB
JCXZ LOOP1
CALL PROCEED
JMP LOOP2
LOOP1:DEC SI
DEC DI
MOV AL,[SI]
XCHG[DI],AL
INT 03H
PROCEED PROC NEAR
PUSH DI
PUSH CX
PUSH SI
DEC SI
DEC DI
MOV AL,[SI]
LOOP3:XCHG [DI],AL
INC DI
DEC CX
JNZ LOOP3
MOV [DI],AL
POP SI
POP CX
POP DI
RET
ENDP
CODE ENDS
DATA SEGMENT
TRUE DB 'ELECTRONICS'
LEN EQU $-TRUE
DB 10 DUP(0)
WRONG DB 'ELCTRNIC'
DATA ENDS
END STAR
THEORY:
CLD: Clear Direction flag. SI and DI will be incremented by chain instructions: CMPSB, CMPSW, LODSB, LODSW, MOVSB, MOVSW, STOSB, STOSW. Algorithm:
DF = 0
REPE chain instructionRepeat following CMPSB, CMPSW, SCASB, SCASW instructions while ZF = 1 (result is Equal), maximum CX times.
Algorithm:
check_cx:
if CX > 0 thendo following chain instructionCX = CX - 1if ZF = 1 then:
go back to check_cxelse
exit from REPE cycleelse
exit from REPE cycle
CMPSB: compare strings byte wiseCompare bytes: ES:[DI] from DS:[SI]. Algorithm:
DS:[SI] - ES:[DI]set flags according to result:OF, SF, ZF, AF, PF, CFif DF = 0 then
SI = SI + 1DI = DI + 1
elseSI = SI - 1DI = DI - 1
PROCEDURE:
1. Open command prompt.
2. Change into TASM software Directory.
3. Type the program using DOS editor.
4. Save the program as ASM file.
5. Convert the ASM file into OBJ file using TASM.
6. Convert OBJ file into EXE file using TLINK.
7. Debug the EXE file using DOS Debugger or TD.
8. Run the program using F7(single step) or F9(at a time).
9. Observe the results in the appropriate segment.
10. Compare the result with theoretical calculations
CODE TABLE:Physical Address
Hex Code Label Mnemonic operand CommentsSegment OffsetAddress Address
ASSUME CS:CODE,DS:DATA
CODE SEGMENT
START:MOV AX,DATA
MOV DS,AX
LEA SI,TRUE
MOV CX,LEN
LEA DI,WRONG
CLD
LOOP2:REPZ
CMPSB
JCXZ LOOP1
CALL PROCEED
JMP LOOP2
LOOP1:DEC SI
DEC DI
MOV AL,[SI]
XCHG[DI],AL
INT 03H
PROCEED PROC NEAR
PUSH DI
PUSH CX
PUSH SI
DEC SI
DEC DI
MOV AL,[SI]
LOOP3:XCHG [DI],AL
INC DI
DEC CX
JNZ LOOP3
MOV [DI],AL
POP SI
POP CX
POP DI
RET
ENDP
CODE ENDS
DATA SEGMENT
TRUE DB 'ELECTRONICS'
LEN EQU $-TRUE
DB 10 DUP(0)
WRONG DB 'ELCTRNIC'
DATA ENDS
END START
RESULT: