Advertisement
Guest User

Untitled

a guest
May 24th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.97 KB | None | 0 0
  1.  
  2. data segment
  3.  
  4. newLine db 10,13,'$' ; "ENTER"
  5. firstMsg db "Wellcome to the online base convertor",10,13,"Please enter your base to convertor from",10,13,"<'H = Hex, 'D' = Dec, 'O' = Oct, 'B' = Bin>:",10,13,'$'; The entery msg
  6. failMsg db "Invalid input",10,13,"Please try again",10,13,'$' ; id input isn't ok
  7. strNum db "Please enter a number",10,13,'$'
  8. strHex db 5,?,5 dup(?)
  9. strBin db 17,?,17 dup(?)
  10. strOct db 6,?,6 dup(?)
  11. strDec db 7,?,7 dup(?)
  12. choose db "it is 68h",10,13,'$'
  13. HexResult db 10 dup(?),'$'
  14.  
  15. DecResult db 13 dup(?)
  16. OctResult db 13 dup (?),'$'
  17. DecBin db 16 dup(?),'$'
  18. strJunk db "liora",10,13,'$'
  19.  
  20. whatWedo db ?
  21. BinResult db 16 dup(?),'$'
  22. HowManyBins db ?
  23.  
  24. data ends
  25.  
  26. sseg segment stack
  27. db 100h dup (?)
  28. sseg ends
  29.  
  30. code segment
  31. assume cs:code, ds:data, ss:sseg
  32.  
  33. start: mov ax,data
  34. mov ds,ax
  35.  
  36. firstInput: lea dx, firstMsg ; Print the first msg
  37. mov ah,9
  38. int 21h
  39.  
  40. call CheckInput
  41.  
  42.  
  43. mov whatWedo,al
  44.  
  45. cmp al, 68h
  46. je callHextoBin
  47. cmp al,48h
  48. je callHextoBin
  49.  
  50. cmp al, 'D' ; Check if the input is 'D'
  51. je callDecToBIn
  52.  
  53. cmp al, 'd' ; Check if the input is 'd'
  54. je callDecToBIn
  55.  
  56. cmp al,'o'
  57. je callOctToBin
  58.  
  59. cmp al,'O'
  60. je callOctToBin
  61.  
  62. cmp al,'b'
  63. je callBinToAll
  64.  
  65. cmp al,'B'
  66. je callBinToAll
  67.  
  68.  
  69. jmp exit
  70.  
  71. callHextoBin: call HexToBin
  72. jmp exit
  73.  
  74. callOctToBin: call OctToBin
  75. jmp exit
  76.  
  77. callBinToAll: call BinToAll
  78. jmp exit
  79.  
  80. callDecToBIn: call DecToBin
  81.  
  82.  
  83.  
  84. exit: mov ah,4ch
  85. int 21h
  86.  
  87. proc BinToAll
  88.  
  89. xor bx,bx
  90. xor ax,ax
  91. mov al,16
  92. mov bl,strBin[1] ; How many in strBin
  93. sub al,bl ; How many '0' we need to add
  94. mov HowManyBins, bl
  95. mov cl,al ; Put it in cl for loop
  96. mov di,2
  97. mov si,0
  98. cmp cl,16
  99. jb FillEmpty
  100.  
  101. FillEmpty: mov BinResult[si],'0'
  102. inc si
  103. loop FillEmpty
  104.  
  105. mov cl, HowManyBins ; We need ? time to complete in cl for loop
  106.  
  107. remain: mov dl,strBin[di]
  108. mov BinResult[si],dl
  109. inc di
  110. inc si
  111. loop remain
  112.  
  113.  
  114. call BinToDec
  115. lea dx, newLine ; Print new line
  116. mov ah,9
  117. int 21h
  118. call BinToHex
  119. lea dx, newLine ; Print new line
  120. mov ah,9
  121. int 21h
  122. call BinToOct
  123.  
  124.  
  125. ret
  126.  
  127. endp BinToAll
  128.  
  129. proc OctToBin
  130.  
  131.  
  132.  
  133. mov cl,strOct[1] ; How many bytes to loop
  134. mov bx, 10 ; The multiplier
  135. xor dx,dx
  136. xor ax,ax
  137. xor dx,dx
  138. mov dx,10
  139. mov si,2
  140.  
  141. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  142.  
  143. putOcts: add al,strOct[si]
  144. sub al,'0'
  145. cmp cl,1 ; If it is the last cell so don't mul
  146. je noMulOcts
  147. mul dx
  148. mov dx,10
  149. noMulOcts: inc si
  150. loop putOcts
  151. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  152. mov bx,2
  153. mov si, 15
  154.  
  155. axToBinOcts: cmp ax, 0
  156. je exitOtB ; If ax == 0 so this exit
  157. xor dx,dx ; We have to clear dx because the divide is between dx:ax to bx
  158. div bx
  159. cmp dl,1
  160. je putOneOtB
  161.  
  162. mov BinResult[si],'0'
  163. dec si
  164. jmp axToBinOcts
  165.  
  166. putOneOtB: mov BinResult[si],'1'
  167. dec si
  168. jmp axToBinOcts
  169.  
  170.  
  171. exitOtB:
  172. mov cx,si
  173. inc cx
  174. fillO: mov BinResult[si],'0' ; Fill the rest str with '0'
  175. dec si
  176. loop fillO
  177.  
  178.  
  179. lea dx,BinResult ; Print the binary msg
  180. mov ah,9
  181. int 21h
  182.  
  183. lea dx,newLine ; Print the binary msg
  184. mov ah,9
  185. int 21h
  186.  
  187.  
  188. call BinToDec
  189.  
  190. call BinToHex
  191.  
  192.  
  193. ret
  194.  
  195.  
  196. endp OctToBin
  197.  
  198. proc DecToBin
  199.  
  200.  
  201. mov cl,strDec[1] ; How many bytes to loop
  202. mov bx, 10 ; The multiplier
  203. xor dx,dx
  204. xor ax,ax
  205. xor dx,dx
  206. mov dx,10
  207. mov si,2
  208.  
  209. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  210.  
  211. putDecs: add al,strDec[si]
  212. sub al,'0'
  213. cmp cl,1 ; If it is the last cell so don't mul
  214. je noMul
  215. mul dx
  216. mov dx,10
  217. noMul: inc si
  218. loop putDecs
  219. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  220. mov bx,2
  221. mov si, 15
  222.  
  223. axToBin: cmp ax, 0
  224. je exitDtB ; If ax == 0 so this exit
  225. xor dx,dx ; We have to clear dx because the divide is between dx:ax to bx
  226. div bx
  227. cmp dl,1
  228. je putOneDtB
  229.  
  230. mov BinResult[si],'0'
  231. dec si
  232. jmp axToBin
  233.  
  234. putOneDtB: mov BinResult[si],'1'
  235. dec si
  236. jmp axToBin
  237.  
  238.  
  239. exitDtB:
  240. mov cx,si
  241. inc cx
  242. fill: mov BinResult[si],'0' ; Fill the rest str with '0'
  243. dec si
  244. loop fill
  245.  
  246.  
  247. lea dx,BinResult ; Print the binary msg
  248. mov ah,9
  249. int 21h
  250.  
  251. lea dx,newLine ; Print the binary msg
  252. mov ah,9
  253. int 21h
  254.  
  255. call BinToOct
  256. call BinToHex
  257.  
  258.  
  259. ret
  260.  
  261.  
  262. endp DecToBin
  263.  
  264. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  265. proc BinToHex
  266.  
  267. mov si,16 ; Init the BINstr result index
  268. mov cl, 4 ; Init the external loop
  269. mov di,4 ; Because we went to put form the end to the start
  270.  
  271. externalBtH:push cx ; Push for keep the the external loop index value
  272. xor ax,ax
  273. xor bx,bx
  274. mov dx,1 ; Init the accumolator counter
  275. mov cx, 4 ; Init the inner loop
  276.  
  277. innerBtH: dec si
  278. mov bl,BinResult[si] ; Mov from the BinResult beginning to the end
  279. sub bl,'0' ; Convert the ascii to real decimal number
  280.  
  281. ;shl bl,4 ; Shl 4 times to move the bits to the upper level
  282.  
  283. loopBtH: shr bl,1 ; Shr 1 time to check the carry
  284. jc addToBtH ; If carry is 1 so add to the sum the DL VALUE
  285. shl dx,1 ; If not so shl 1 time
  286. loop innerBtH
  287. pop cx ; When finish thr inner loop so take the last external loop index value
  288. mov HexResult[di],al ; Put the the summerize value at the Result str
  289. add HexResult[di],'0' ; Add '0' to convert from number to the right char
  290. dec di
  291. loop externalBtH
  292. jmp exitBtH
  293. addToBtH: add ax,dx ; Add the Dl to summerize the bits value
  294. shl dx,1 ; Multiple by 2
  295. loop innerBtH
  296. pop cx
  297. cmp al,10
  298. jge putDigit
  299. mov HexResult[di],al
  300. add HexResult[di],'0'
  301. dec di
  302. loop externalBtH
  303. jmp exitBtH
  304. putDigit: mov HexResult[di],al
  305. add HexResult[di],37h
  306. dec di
  307. loop externalBtH
  308.  
  309. exitBtH:
  310. mov HexResult[5],'<'
  311. mov HexResult[6],'H'
  312. mov HexResult[7],'E'
  313. mov HexResult[8],'X'
  314. mov HexResult[9],'>'
  315. mov HexResult[10],'$'
  316. lea dx, HexResult ; Print the binary msg
  317. mov ah,9
  318. int 21h
  319.  
  320.  
  321. ret
  322.  
  323.  
  324. endp BinToHex
  325.  
  326. proc HexToBin
  327.  
  328. lea dx, strHex[2]; Print the first msg
  329. mov ah,9
  330. int 21h
  331. xor cx,cx
  332. mov cl,strHex[1]
  333. mov bl,1
  334. mov si,0
  335.  
  336. againHTB: inc bl
  337. push cx
  338. mov cx,4
  339. mov al, strHex[bx]
  340.  
  341.  
  342. startHTB: cmp al, 'A'
  343. jge digit
  344. sub al, 30h
  345. jmp shlToStart
  346.  
  347. digit: sub al,37h
  348.  
  349. shlToStart: shl al,4
  350.  
  351. againSL:
  352. shl al,1
  353. jc putOne
  354. mov BinResult[si],'0'
  355. inc si
  356. loop againSL
  357. pop cx
  358. clc
  359. loop againHTB
  360. jmp exitHexToBin
  361.  
  362.  
  363. putOne: mov BinResult[si],'1'
  364. inc si
  365. loop againSL
  366. pop cx
  367. clc
  368. loop againHTB
  369.  
  370. exitHexToBin: mov BinResult[si],10
  371. mov BinResult[si+1],13
  372. mov BinResult[si+2],'$'
  373. xor si,si
  374.  
  375. lea dx, BinResult ; Print the binary msg
  376. mov ah,9
  377. int 21h
  378.  
  379.  
  380.  
  381. cmp whatWedo,'h'
  382. je itIsHex
  383. cmp whatWedo,'H'
  384. je itIsHex
  385. jmp e
  386.  
  387. itIsHex :
  388. call BinToDec
  389. call BinToOct
  390.  
  391.  
  392.  
  393. e: ret
  394.  
  395.  
  396. endp HexToBin
  397.  
  398.  
  399.  
  400. proc BinToOct
  401.  
  402. mov cx, 5
  403. mov si,15
  404. mov di,4
  405. againBtO: xor ax,ax
  406. xor bx,bx
  407. mov bl,1
  408. push cx
  409. mov cx,3
  410.  
  411. loopBtO: cmp BinResult[si], '1'
  412. je addOct
  413. shl bx,1
  414. dec si
  415. loop loopBtO
  416. mov OctResult[di], al
  417. add OctResult[di],'0'
  418. dec di
  419. jmp finishInnerOct
  420.  
  421. addOct:add al,bl
  422. shl bx,1
  423. dec si
  424. loop loopBtO
  425. mov OctResult[di], al
  426. add OctResult[di],'0'
  427. dec di
  428. finishInnerOct: pop cx
  429. loop againBtO
  430.  
  431.  
  432. exitBinToOct: mov OctResult[5],'<'
  433. mov OctResult[6],'O'
  434. mov OctResult[7],'C'
  435. mov OctResult[8],'T'
  436. mov OctResult[9],'>'
  437. mov OctResult[10],10
  438. mov OctResult[11],13
  439. mov OctResult[12],'$'
  440. lea dx, OctResult ; Print the first msg
  441. mov ah,9
  442. int 21h
  443. ret
  444.  
  445.  
  446. endp BinToOct
  447.  
  448.  
  449. proc BinToDec
  450.  
  451. mov cx,16
  452. mov si, 15
  453. xor bx,bx
  454. xor ax,ax
  455. mov bl, 1
  456.  
  457.  
  458.  
  459. againBtD:
  460. cmp BinResult[si],'1'
  461. je addBtD
  462. shl bx,1
  463. dec si
  464. loop againBtD
  465. jmp startMakeDec
  466.  
  467. addBtD: add ax,bx
  468. shl bx,1
  469. dec si
  470. loop againBtD
  471.  
  472. startMakeDec:
  473. xor bx,bx
  474. xor dx,dx
  475. xor si,si
  476. mov si,4
  477. mov bx,10
  478. mov cx,100
  479.  
  480. makeDec: div bx
  481.  
  482. mov DecResult[si],dl
  483. add DecResult[si],'0'
  484. Dec si
  485. xor dx,dx
  486. cmp al,0
  487. je putZero
  488. loop makeDec
  489.  
  490. putZero: cmp si, 0
  491. je exitBinToDec
  492. mov DecResult[si],'0'
  493. Dec si
  494. loop putZero
  495.  
  496.  
  497.  
  498. exitBinToDec: mov DecResult[5],'<'
  499. mov DecResult[6],'D'
  500. mov DecResult[7],'E'
  501. mov DecResult[8],'C'
  502. mov DecResult[9],'>'
  503. mov DecResult[10],10
  504. mov DecResult[11],13
  505. mov DecResult[12],'$'
  506.  
  507.  
  508. lea dx, DecResult
  509. mov ah,9
  510. int 21h
  511. ret
  512.  
  513. endp BinToDec
  514.  
  515.  
  516.  
  517. proc CheckInput
  518.  
  519.  
  520.  
  521. again: mov ah, 1
  522. int 21h ;ENTER a char
  523.  
  524. lea dx, newLine ; Newline
  525. mov ah,9
  526. int 21h
  527.  
  528. cmp al, 'h'
  529. je jmpH
  530. cmp al, 'H'
  531. je jmpH
  532. cmp al, 'd'
  533. je jmpD
  534. cmp al, 'D'
  535. je jmpD
  536. cmp al, 'o'
  537. je jmpO
  538. cmp al, 'O'
  539. je jmpO
  540. cmp al, 'b'
  541. je jmpB
  542. cmp al, 'B'
  543. je jmpB
  544.  
  545. mov dx, offset failMsg ; invalid input
  546. mov ah,9
  547. int 21h
  548.  
  549. jmp again
  550.  
  551. okH: mov dx, offset strNum ; "Please enter a number"
  552. mov ah,9
  553. int 21h
  554.  
  555. mov dx, offset strHex ; Input a number
  556. mov ah, 0Ah
  557. int 21h
  558.  
  559.  
  560. mov cl, strHex[1]
  561. mov bx,2
  562. JMP againH
  563.  
  564.  
  565. jmpB: jmp okB
  566. jmpH: jmp okH
  567. jmpO: jmp okO
  568. jmpD: jmp okD
  569.  
  570.  
  571. againH: cmp strHex[bx], '0'
  572. jb failH
  573. cmp strHex[bx], '9'
  574. jbe jmpNextH
  575.  
  576. cmp strHex[bx],'A'
  577. JB failH
  578.  
  579. cmp strHex[bx], 'F'
  580. jbe jmpNextH
  581.  
  582. cmp strHex[bx],'a'
  583. jb failH
  584.  
  585. cmp strHex[bx], 'f'
  586. jbe jmpNextH
  587.  
  588.  
  589. failH: mov dx, offset failMsg ; invalid input
  590. mov ah,9
  591. int 21h
  592. jmp okH
  593.  
  594. okB: mov dx, offset strNum ; Please enter a number...
  595. mov ah,9
  596. int 21h
  597.  
  598. mov dx, offset strBin ; Input a number
  599. mov ah, 0Ah
  600. int 21h
  601.  
  602. ; mov dl,strBin[1]
  603. mov cl, strBin[1]
  604. mov bx,2
  605.  
  606. againB: cmp strBin[bx],'0'
  607. je jmpNextB
  608. cmp strBin[bx],'1'
  609. je jmpNextB
  610.  
  611. failB: mov dx, offset failMsg ; invalid input
  612. mov ah,9
  613. int 21h
  614. jmp okB
  615.  
  616. jmpNextH: jmp nextH
  617. jmpNextB: jmp nextB
  618. jmpAgainH: jmp againH
  619. jmpAgainB: jmp againB
  620. jmpAgaino: jmp againO
  621.  
  622. okO: mov dx, offset strNum ; Please enter a number...
  623. mov ah,9
  624. int 21h
  625.  
  626. mov dx, offset strOct ; Input a number
  627. mov ah, 0Ah
  628. int 21h
  629.  
  630. mov cl, strOct[1]
  631. mov bx, 2
  632.  
  633. againO: cmp strOct[bx], '0'
  634. jb failO
  635. cmp strOct[bx],'9'
  636. jbe nextO
  637.  
  638.  
  639. failO: mov dx, offset failMsg ; invalid input
  640. mov ah,9
  641. int 21h
  642. jmp okO
  643.  
  644.  
  645.  
  646. okD: mov dx, offset strNum ; Please input
  647. mov ah,9
  648. int 21h
  649.  
  650. mov dx, offset strDec ; Input a number
  651. mov ah, 0Ah
  652. int 21h
  653.  
  654. mov cl, strDec[1]
  655. mov bx, 2
  656.  
  657. againD: cmp strDec[bx],'0'
  658. jb failD
  659. cmp strDec[bx], '9'
  660. jbe nextD
  661.  
  662.  
  663. failD: mov dx, offset failMsg ; invalid input
  664. mov ah,9
  665. int 21h
  666. jmp okD
  667.  
  668.  
  669.  
  670.  
  671. nextH: inc bx
  672. loop jmpAgainH
  673. mov strHex[bx],10
  674. mov strHex[bx+1],13
  675. mov strHex[bx+2],'$'
  676. jmp exitF
  677.  
  678. nextB: inc bx
  679. loop jmpAgainB
  680. mov strBin[bx],'$'
  681. jmp exitF
  682.  
  683. nextO: inc bx
  684. loop jmpAgaino
  685. mov strOct[bx],'$'
  686. jmp exitF
  687.  
  688. nextD: inc bx
  689. loop againD
  690. mov strDec[bx],10
  691. mov strDec[bx+1],13
  692. mov strDec[bx+2],'$'
  693. jmp exitF
  694. exitF:
  695. ret
  696.  
  697.  
  698.  
  699. endp CheckInput
  700.  
  701. code ends
  702. end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement