Advertisement
Guest User

Untitled

a guest
Jan 9th, 2013
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.58 KB | None | 0 0
  1. ; a program to demonstrate creating a file and then reading from
  2. ; it
  3.  
  4. .model small
  5. .stack
  6. EXTRA_SEG SEGMENT PARA 'EXTRA'
  7. StartOFile1 dw ?
  8. EXTRA_SEG ENDS
  9.  
  10. .data
  11. pkey db "press any key...$"
  12. errornotfound db "Ops! This Word, Isn't Here!!! Please Try Again.",0ah,0dh,"$"
  13.  
  14.  
  15. SAVE_CNT dw ?
  16. START_ADDR dw ?
  17. findword db "<>Dear User! What Word, Do You Want To count It? ","$"
  18. askDirectory DB "enter directory of file :",0ah,0dh,"$"
  19. str DB "=" , '$'
  20. Handle DW ? ; to store file handle
  21. fileDirectory db 400 dup(?)
  22. temp dw ?
  23. enter db "",0ah,0dh,"$"
  24. plus dw 0
  25. FileName DB "File.txt",0 ; file to be opened
  26. OpenError DB "An error has occured(opening)!$"
  27. ReadError DB "An error has occured(reading)!$"
  28. tst db "lllllllllllll",0ah,0dh,"$"
  29. Buffer DB 100 dup (?) ; buffer to store data
  30. linef db 13,10,"$"
  31. home db 13,"$"
  32. lengthword dw ?
  33.  
  34. word dw 100 dup(?)
  35. checkendsentence db ?
  36. checkendword db ?
  37. lengthremainsentence dw ?
  38. lengthremain dw ?
  39. remainsentence db 20 dup(?)
  40. lenghtsentence dw 0
  41. j dw 0
  42. succes db "found",0ah,0dh,"$"
  43. tedad db 0
  44.  
  45.  
  46.  
  47.  
  48.  
  49. i db 0
  50.  
  51. string dw 300 dup(?)
  52. .code
  53. main proc far
  54. mov ax,@data ; base address of data segment
  55. mov ds,ax ; put this in ds
  56.  
  57. call ask_directory
  58. lea dx,fileDirectory
  59.  
  60. mov al,2 ; access mode - read and write
  61. mov ah,3Dh ; function 3Dh -open a file
  62. int 21h ; call DOS service
  63.  
  64. mov Handle,ax ; save file handle for later
  65. jc ErrorOpening ; jump if carry flag set - error!
  66.  
  67. mov dx,offset Buffer ; address of buffer in dx
  68. mov bx,Handle ; handle in bx
  69. mov cx,100 ; amount of bytes to be read
  70. mov ah,3Fh ; function 3Fh - read from file
  71. int 21h ; call dos service
  72.  
  73. jc ErrorReading ; jump if carry flag set - error!
  74.  
  75. mov bx,Handle ; put file handle in bx
  76. mov ah,3Eh ; function 3Eh - close a file
  77. int 21h ; call DOS service
  78.  
  79. mov cx,100 ; length of string
  80. mov si,OFFSET Buffer ; DS:SI - address of string
  81. xor bh,bh ; video page - 0
  82. mov ah,0Eh ; function 0Eh - write character
  83. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  84. mov j,di
  85.  
  86. mov di,0;in inja dare kare onieki word ro kharab mikone
  87. NextChar:
  88. ;push di
  89.  
  90. lodsb ; AL = next character in string
  91. cmp al,00h ;ghablan '$' bod
  92. ;jz l9
  93. jz prc
  94. ;shayad inja store kardan mikhad
  95. mov ah,00h
  96. mov string[di],ax
  97. inc di
  98. inc lenghtsentence
  99.  
  100. mov dl,al
  101. mov AH , 02h
  102. int 21h
  103. ;inc di
  104.  
  105. loop NextChar
  106.  
  107. ;call bubble
  108.  
  109. prc:
  110. mov di,j
  111. mov ah,9
  112. mov dx, offset enter
  113. int 21h
  114. mov ah,9
  115. mov dx, offset enter
  116. int 21h
  117.  
  118. call ask_word
  119.  
  120.  
  121. ;;be enter berese dg nemikhoneee
  122. call find_word
  123. mov ah,9
  124. mov dx, offset pkey
  125. int 21h
  126. ; wait for any key....
  127.  
  128.  
  129.  
  130. mov ah, 1
  131. int 21h
  132.  
  133. mov ax, 4c00h ; exit to operating system.
  134. int 21h
  135.  
  136. ; li:
  137. ; mov ah,9
  138. ; mov dx, offset tst
  139. ; int 21h
  140. ; jmp li1
  141. ;
  142. ;
  143.  
  144. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  145. ErrorOpening:
  146.  
  147. mov dx,offset OpenError ; display an error
  148. mov ah,09h ; using function 09h
  149. int 21h ; call DOS service
  150. mov ax,4C01h ; end program with an errorlevel =1
  151. int 21h
  152.  
  153. ErrorReading:
  154. mov dx,offset ReadError ; display an error
  155. mov ah,09h ; using function 09h
  156. int 21h ; call DOS service
  157. l9:
  158. ;;;;;;;;;;;;;;;;;;;;;
  159. mov ah,9
  160. lea dx,linef
  161. int 21h
  162. ;;;;;;;;;;;;;;;;;;;;;;
  163. lea bx,Buffer
  164. mov di,0
  165. l1:
  166. cmp di,75
  167. jz l2
  168. mov si,0
  169. l3:
  170. cmp si,75
  171. jz l4
  172. mov ch,[bx+si+1]
  173. mov dh,[bx+si]
  174. cmp dh,ch
  175. ja l5
  176. mov [bx+si],ch
  177. mov [bx+si+1],dh
  178. l5:
  179. inc si
  180. jmp l3
  181. l4:
  182.  
  183. inc di
  184. jmp l1
  185. l2:
  186.  
  187. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  188. lea bx,Buffer
  189. mov si,0
  190. ln1:
  191. cmp si,75
  192. ;cmp [bx+si],"$"
  193. jz ln2
  194. mov AH , 02h
  195. mov dl ,[bx+si]
  196. int 21h
  197. inc si
  198. jmp ln1
  199. ln2:
  200.  
  201. ;;;;;;;;;;;;;;;;;;;;;;;;;;
  202.  
  203. mov ah,9
  204. lea dx,linef
  205. int 21h
  206. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  207. ;;;;;;;;;print enter;;;;;;
  208. lea bx,Buffer
  209. mov si,0
  210. mov i,49
  211. lk1:
  212. cmp si,75
  213. ja lk2
  214. mov ch,[bx+si+1]
  215. mov dh,[bx+si]
  216. pusha
  217.  
  218. cmp dh,"$"
  219. jz lk2
  220.  
  221. mov dl,dh
  222. mov AH , 02h
  223. int 21h
  224.  
  225. mov AH , 9
  226. lea DX , str
  227. int 21h
  228.  
  229. lw3:
  230. cmp i,";"
  231. jb lw2
  232.  
  233. mov ah,02h
  234. mov dl,"1"
  235. int 21h
  236. sub i,10
  237. jmp lw3
  238.  
  239. lw2:
  240.  
  241. mov ah,02h
  242. mov dl,i
  243. int 21h
  244.  
  245.  
  246.  
  247. mov ah,9
  248. lea dx,home
  249. int 21h
  250.  
  251. popa
  252. cmp dh,ch
  253. jz lk5
  254. pusha
  255. mov ah,9
  256. lea dx,linef
  257. int 21h
  258. popa
  259. mov i,48
  260. lk5:
  261. inc i
  262.  
  263. inc si
  264. jmp lk1
  265. lk2:
  266. ;;;;;;;;;;;;;;;;;;;;;;;;;;
  267.  
  268. mov ah,9
  269. lea dx,linef
  270. int 21h
  271.  
  272. mov ah,9
  273. lea dx,linef
  274. int 21h
  275. mov ah,8
  276. int 21h
  277. mov AX , 4c00h
  278. int 21h
  279.  
  280.  
  281.  
  282. main endp
  283.  
  284. ask_directory proc
  285.  
  286. mov ah,9
  287. lea dx, askDirectory ;jomle baraye gereftane vorodi
  288. int 21h
  289.  
  290.  
  291. mov si,0
  292.  
  293. sent:
  294. mov ah,1h
  295. int 21h
  296. cmp al,0dh ;comper al,enter
  297. je exit1
  298. mov fileDirectory[si],al ;string bod ghablan
  299. inc si
  300.  
  301. jmp sent
  302.  
  303. exit1:
  304. mov ah,9
  305. mov dx, offset enter
  306. int 21h
  307. mov ah,9
  308. mov dx, offset enter
  309. int 21h
  310. ret
  311. endp
  312. ;--------------------ask word proc------------
  313.  
  314. ask_word proc
  315. mov ah,9
  316. lea dx, findword
  317. int 21h
  318. again:
  319. mov ah,1h
  320. mov temp,di
  321. mov di,0
  322. mov di,temp
  323. int 21h
  324. mov ah,0
  325. cmp ax,0dh
  326.  
  327. je next2
  328. mov ah,0
  329. mov word[di],ax
  330. inc di
  331. jmp again
  332.  
  333. next2:
  334.  
  335. dec di
  336. mov lengthword,di
  337. mov ah,9
  338. mov dx, offset enter
  339. int 21h
  340. mov ah,9
  341. mov dx, offset enter
  342. int 21h
  343. ret
  344. endp
  345. ;---------------------find word proc---------
  346. find_word proc
  347. ;
  348. ; ; mov ax,StartOFile
  349. ; lea ax , string
  350. ; mov StartOFile1,ax
  351. ; mov ax,0000h
  352. ; ; cnt1:
  353. ;; add cnt,1
  354. ;
  355. ; mov ax, word
  356. ; LEA DI,StartOFile1
  357. ; cld
  358. ; SCASW
  359. ; ;je cnt1
  360. ;; jne
  361. ; ;testesh kardam, inja eshtebah kar mikone
  362. ; jz li
  363. ;
  364. ;
  365.  
  366.  
  367. ;
  368. ; li1:
  369. ;
  370.  
  371.  
  372.  
  373. mov si,0;baraye jomalate file ; bayad andazeye file ro bezaram
  374. mov di,0;baraye kalameye morede nazar
  375.  
  376. doagain:
  377. ; cmp al,b.word[di]; di bashe ya si???????
  378. ; jne next_word
  379. ; ; jmp this_word
  380.  
  381. mov ah,0
  382. mov ax,string[si]
  383. mov ah,0
  384. mov ax, word[di]
  385. ; mov ah,0
  386. cmp ax, string[si];al bod ghable string dw
  387. jne nextword
  388. jmp thisword
  389. nextword:
  390. inc si
  391. call check_end_sentence
  392. cmp checkendsentence,2ah;ascii code for *
  393. ;cmp checkendsentence,00h
  394. je exitthis
  395.  
  396. mov ax, string[si];kalameye badi dar file ro mikhone
  397. mov ah,00h
  398. cmp ax, 20h ;ascii code for blank(bebine kalame tamom shode ya na)
  399. jne nextword
  400. inc si
  401. call check_end_sentence
  402. cmp checkendsentence,2ah
  403. je exitthis
  404. ;inc si ;ino ezafe kardam ke vaghti be blank resid nare dar doagain blanko ba word moghayese kone
  405. jmp doagain
  406. thisword:
  407. inc si
  408. call check_end_sentence
  409. ; cmp checkendsentence,00h
  410. cmp checkendsentence,2ah
  411. je exitthis
  412. inc di
  413. call check_end_word
  414. cmp checkendword,2ah
  415. je found
  416. mov ah,0
  417. mov ax, word[di]
  418. cmp ax, string[si]
  419. jne againword
  420.  
  421. jmp thisword
  422.  
  423. againword:
  424. mov di,0
  425.  
  426. jmp nextword
  427.  
  428. found:
  429.  
  430. mov ah,9
  431. mov dx, offset enter
  432. int 21h
  433. mov ah,9
  434. mov dx, offset succes
  435. int 21h
  436. mov ah,9
  437. mov dx, offset enter
  438. int 21h
  439. inc tedad;badesh tedad ro mirizam to al va print al ro mikhonam:D
  440. mov di,0
  441. jmp doagain
  442.  
  443.  
  444.  
  445. exitthis:
  446.  
  447. mov ah,9
  448. mov dx, offset enter
  449. int 21h
  450.  
  451. mov ah,9
  452. mov dx, offset enter
  453. int 21h
  454. ret
  455. endp
  456.  
  457. ;------Check The Word Is End Or Not.------
  458.  
  459. check_end_word proc
  460.  
  461. cmp di,lengthword
  462. jne exit2
  463. inc si
  464. mov lengthremainsentence,si
  465. mov di,0
  466.  
  467. ;saveremain:
  468. ; mov ax,string[si]
  469. ; mov ah,0
  470. ; mov remainsentence[di],al
  471. ; cmp si,lenghtsentence
  472. ; je savepoint
  473. ; inc di
  474. ; inc si
  475. ; jmp saveremain
  476. ;
  477. ;savepoint:
  478. ; mov lengthremain,di
  479. ; mov si,lengthremainsentence
  480. ; mov di,lengthword
  481. ; inc di
  482. mov checkendword,2ah
  483.  
  484. ; decsi:
  485. ; dec si
  486. ; dec di
  487. ; cmp di,0
  488. ; jne decsi
  489.  
  490. exit2:
  491.  
  492. ret
  493. endp
  494.  
  495.  
  496. ;------Check The Sentence Is End Or Not.------
  497.  
  498. check_end_sentence proc
  499.  
  500. cmp si,lenghtsentence
  501. jne end
  502. mov ah,9
  503. lea dx,errornotfound
  504. int 21h
  505. mov checkendsentence,2ah
  506. jmp end
  507.  
  508. end:
  509. ret
  510.  
  511. endp
  512.  
  513. ;==========bubble sort=========
  514. bubble proc
  515. push ds
  516. push cx
  517. push ax
  518. push bx
  519. mov ax,@data
  520. mov ds,ax
  521. lea di,buffer; khodam
  522. mov START_ADDR,di
  523. mov cx,es:[di]
  524. ;inja bayad cx be andazeye lengthsentence bashe
  525. mov save_cnt,cx
  526.  
  527. INIT:
  528. mov bx ,1
  529. dec SAVE_CNT
  530. jz STORED
  531. mov cx,20
  532. ;mov cx,SAVE_CNT
  533. mov di,START_ADDR
  534. NEXT1: add di,2
  535. mov ax,es:[di]
  536. cmp es:[di+2],ax
  537. jae CONT
  538. xchg es:[di+2],ax
  539. mov es:[di],ax
  540. sub bx,bx
  541. CONT: loop NEXT1
  542. cmp bx,0
  543. je INIT
  544. STORED: mov di,START_ADDR
  545. pop bx
  546. pop ax
  547. pop cx
  548. pop ds
  549. ret
  550. endp
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561. ;======end of bubble sort========
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement