home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 3 / FREEWARE.BIN / ms_dos / dsort / asmutys.asm next >
Assembly Source File  |  1980-01-02  |  10KB  |  330 lines

  1.     page    96,132
  2. ;§∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞§
  3. ;§                                                                          §
  4. ;§              ディレクトリエントリ  ソート  ユーティリティ                §
  5. ;§                                                                          §
  6. ;§                                     DSORT.EXE  Ver1.11    §
  7. ;§                                                                          §
  8. ;§                   Copyright (C) by 福地 邦雄 1991. All rights reserved.  §
  9. ;§∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞§
  10.     .MODEL  SMALL,C
  11.     .data
  12. ;
  13.     public  errorno         ;プログラム終了時のステータスに指定する
  14. errorno     dw      ?           ;エラーコード
  15. ;
  16. printmsg    db  'print error',0dh,0ah   ;標準プリントのエラー表示のメッセージ
  17. prtmsgsiz   equ     $-printmsg
  18. ;
  19. allocmsg    db  'memory allocation error',0dh,0ah   ;メモリ獲得エラーの
  20. alcmsgsiz   equ     $-allocmsg              ;メッセージ
  21. ;
  22.     .code
  23. ;
  24. ;------------------------------------------------------------------------------
  25. ;
  26. ;   getargs
  27. ;       コマンドライン入力を分解してポインタのリストにする
  28. ;       空白,タブを区切り文字とする   プログラム名はサポートされない
  29. ;
  30. ;   TYPE    near call
  31. ;   IN  ds=psp
  32. ;       200h以上のスタックサイズ
  33. ;   OUT ss:bp にファーポインタリストの先頭アドレス
  34. ;       ax に文字列の個数       ゼロフラグに個数のテスト結果
  35. ;       ディレクションフラグ ダウン方向
  36. ;
  37. ;   保存レジスタ    ds のみ
  38. ;
  39. ;------------------------------------------------------------------------------
  40. ;
  41. stackwrk    equ 01c0h
  42. argcwrk     equ word ptr [bp]
  43. returnaddr  equ word ptr [bp+2]
  44. strings     equ word ptr [bp+4]
  45. cmdlen      equ ds:[80h]
  46. cmdtop      equ 81h
  47. SPC     equ 20h
  48. TAB     equ 09h
  49. ;
  50.     public  getargs
  51. getargs proc    near
  52. ;
  53.     sub     sp,stackwrk     ;文字列とポインタリスト領域の確保
  54.     mov     bp,sp
  55.     mov     ax,[bp+stackwrk]    ;リターンアドレスのセーブ
  56.     mov     returnaddr,ax
  57.     xor     ax,ax           ;ワーク領域&レジスタの初期化
  58.     mov     argcwrk,ax
  59.     mov     cx,ax
  60.     lea     di,strings
  61.     mov     cl,cmdlen       ;コマンドライン文字列長の取得
  62.     mov     si,cx
  63.     inc     cx
  64.     mov     bx,cmdtop
  65.     mov     byte ptr [si+bx],SPC    ;コマンドライン終端の改行コードを空白に
  66. ;
  67. spaceloop:              ;空白,タブをスキップする
  68.     cmp     byte ptr [bx],SPC
  69.     je      nextchar
  70.     cmp     byte ptr [bx],TAB
  71.     jne     spaceout
  72. nextchar:
  73.     inc     bx
  74.     loop    spaceloop
  75.     jmp     argend
  76. ;
  77. spaceout:               ;
  78.     mov     ss:[di],bx      ;文字列の先頭オフセットのセーブ
  79.     mov     word ptr ss:[di+2],0    ;文字列長さ初期化
  80.     lea     di,[di+4]
  81.     inc     argcwrk         ;文字列個数のカウントアップ
  82. nospcloop:
  83.     inc     word ptr ss:[di-2]  ;文字列長さのカウントアップ
  84.     dec     cx
  85.     jcxz    stringend
  86.     inc     bx
  87.     cmp     byte ptr [bx],SPC   ;空白,タブが現れるまでループ
  88.     je      stringend
  89.     cmp     byte ptr [bx],TAB
  90.     je      stringend
  91.     jmp     short nospcloop
  92. stringend:
  93.     mov     byte ptr [bx],0     ;文字列終端に 0 をセット
  94.     inc     bx
  95.     loop    spaceloop       ;空白,タブのスキップ
  96. ;
  97. argend:
  98.     mov     ax,argcwrk      ;文字列個数をチェック 0 なら終了
  99.     test    ax,ax
  100.     jnz     argsset
  101.     add     sp,stackwrk
  102.     xor     ax,ax
  103.     ret
  104. ;
  105. argsset:
  106.     mov     dx,di           ;文字列情報の最終アドレスをセーブ
  107.     mov     bx,di           ;
  108.     std             ;コピー方向=ダウンカウント
  109.     lea     di,[bp+stackwrk+1]  ;スタック最終アドレスを取得
  110. ;   @do until           ;文字列をスタックへコピー
  111. @d0001:
  112.         lea     bx,[bx-4]
  113.         mov     cx,ss:[bx+2]        ;文字列長取得
  114.         mov     ss:[bx+2],ss        ;コピー後セグメントセット
  115.         mov     si,ss:[bx]      ;文字列終端アドレス取得
  116.         add     si,cx
  117.         inc     cx          ;終端文字分をカウントアップ
  118.         mov     es,ss:[bx+2]
  119.       rep   movsb
  120.         mov     ss:[bx],di      ;スタック上の文字列オフセット設定
  121.         inc     word ptr ss:[bx]
  122.         dec     ax
  123. ;   @doend (zf,on)
  124.       jnz   @d0001
  125. ;                   ;ファーポインタリストのコピー
  126.     and     di,0fffeh       ;コピー先設定
  127.     lea     di,[di-2]
  128.     mov     cx,dx
  129.     mov     si,cx           ;コピー元設定
  130.     lea     si,[si-2]
  131.     sub     cx,bx
  132.     shr     cx,1            ;コピーカウント(ワード)設定
  133.     push    ds
  134.     mov     ds,ss:[si]      ;スタックセグメント取得
  135.   rep    movsw
  136.     pop     ds
  137. ;
  138.     mov     ax,argcwrk
  139.     mov     bx,returnaddr       ;リターンアドレス設定
  140.     mov     ss:[di],bx
  141.     lea     bp,[di+2]       ;ファーポインタリストのオフセット
  142.     mov     sp,di
  143.     test    ax,ax
  144.     ret
  145. ;
  146. getargs endp
  147. ;
  148. ;------------------------------------------------------------------------------
  149. ;
  150. ;   asctoint
  151. ;       アスキー10進数字列から、16ビットバイナリーへの変換
  152. ;       数字でない文字に出会った時点で終了する
  153. ;
  154. ;   TYPE    near call
  155. ;   IN  es:di アスキー10進数字列アドレス
  156. ;   OUT ax 変換した数値
  157. ;       es:di 変換時の数字でない文字のアドレス
  158. ;   保存レジスタ    bx,cx,dx,si,bp,ds,es
  159. ;
  160. ;------------------------------------------------------------------------------
  161. ;
  162.     public  asctoint
  163. asctoint    proc    uses bx cx dx
  164. ;
  165.     xor     ax,ax           ;変換中の数値&変換後通知データ
  166.     mov     cx,ax           ;獲得する(した)文字
  167.     mov     bx,10           ;10進変換のための乗数
  168. ;   @do repeat
  169. @d0002:
  170.         mov     cl,es:[di]      ;文字獲得
  171. ;       @if (cl,<,'0'),or,(cl,>,'9')
  172.           cmp   cl,'0'
  173.           jb    @i0001
  174.           cmp   cl,'9'
  175.           jbe   @i0002
  176. @i0001:
  177. ;           @doexit
  178.               jmp   @d0003
  179. ;       @ifend
  180. @i0002:
  181.         and     cl,0fh          ;文字を数値に変換
  182.         mul     bx          ;今までの変換データを10倍
  183.         add     ax,cx           ;獲得した数値を加算
  184.         inc     di          ;次の文字位置へ
  185. ;   @doend
  186.       jmp   @d0002
  187. @d0003:
  188.     test    ax,ax           ;ゼロフラグ設定
  189.     ret
  190. ;
  191. asctoint    endp
  192. ;
  193. ;------------------------------------------------------------------------------
  194. ;
  195. ;   inttoasc0
  196. ;       16ビットバイナリをアスキー10進数字列に変換する
  197. ;       5 桁固定で 頭の 0 も出力する
  198. ;
  199. ;   TYPE    near call
  200. ;   IN  ax 16ビットバイナリ
  201. ;       es:di 変換後のアスキー10進数字列を格納するアドレス
  202. ;   OUT es:di 変換したアスキー10進数字列の次のアドレス
  203. ;   保存レジスタ    bx,cx,dx,si,bp,ds,es
  204. ;
  205. ;------------------------------------------------------------------------------
  206. ;
  207.     public  inttoasc0
  208. inttoasc0   proc    uses cx dx
  209. ;
  210.     cld             ;変換後文字列出力方向 アップ方向
  211.     mov     cx,100          ;10進変換のための除数
  212.     xor     dx,dx           ;32/16ビット除算用の上位16ビットクリア
  213.     div     cx          ;100で割る DX=余り AX=商
  214.     div     cl          ;商を更に100で割る AH=余り AL=商
  215.     add     al,'0'          ;万の位を数字文字に変換して出力
  216.     stosb
  217.     mov     al,ah           ;千,百の位を数字文字に変換して出力
  218.     aam
  219.     add     ax,'00'
  220.     xchg    ah,al
  221.     stosw
  222.     mov     al,dl           ;十,一の位を数字文字に変換して出力
  223.     aam
  224.     add     ax,'00'
  225.     xchg    ah,al
  226.     stosw
  227.     ret
  228. ;
  229. inttoasc0   endp
  230. ;
  231. ;------------------------------------------------------------------------------
  232. ;
  233. ;   dosstdout
  234. ;       MS-DOSのハンドル2番 標準出力へデータを出力する
  235. ;       エラーが起こった時はアボートする
  236. ;
  237. ;   TYPE    near call
  238. ;   IN  cx 出力データ長
  239. ;       ds:dx 出力データの先頭アドレス
  240. ;   OUT なし
  241. ;   保存レジスタ    si,di,bp,ds,es
  242. ;
  243. ;------------------------------------------------------------------------------
  244. ;
  245.     public  dosstdout
  246. dosstdout   proc
  247. ;
  248.     mov     ah,40h
  249.     mov     bx,1
  250.     int     21h
  251. ;   @if (cf,on)
  252.       jnc   @i0003
  253.         mov     ah,4ch
  254.         int     21h
  255. ;   @ifend
  256. @i0003:
  257.     ret
  258. ;
  259. dosstdout   endp
  260. ;
  261. ;------------------------------------------------------------------------------
  262. ;
  263. ;   dosallocx
  264. ;       MS-DOSのメモリ獲得を呼び出す、獲得出来ない時は標準出力へエラー
  265. ;       メッセージを出力してアボートする
  266. ;
  267. ;   TYPE    near call
  268. ;   IN  dx:ax 獲得したいメモリのバイトサイズ
  269. ;   OUT ax 割り当てられたメモリのセグメントアドレス
  270. ;   保存レジスタ    cx,si,di,bp,ds,es
  271. ;
  272. ;------------------------------------------------------------------------------
  273. ;
  274.     public  dosallocx
  275. dosallocx    proc   uses bx cx
  276. ;
  277.     mov     cl,4
  278.     add     ax,15
  279.     adc     dx,0
  280.     shr     ax,cl
  281.     and     dx,0fh
  282.     ror     dx,cl
  283.     mov     bx,ax
  284.     or      bx,dx
  285.     mov     ah,48h
  286.     int     21h
  287. ;   @if (cf,on)
  288.       jnc   @i0004
  289.         mov     errorno,ax
  290.         mov     dx,offset allocmsg
  291.         mov     cx,alcmsgsiz
  292.         jmp     abort
  293. ;   @ifend
  294. @i0004:
  295.     ret
  296. ;
  297. dosallocx    endp
  298. ;
  299. ;------------------------------------------------------------------------------
  300. ;
  301. ;   abort
  302. ;       標準出力へメッセージを出力した後、プログラム終了する
  303. ;       終了時に終了コードを設定する
  304. ;
  305. ;   TYPE    near call
  306. ;   IN  cx 出力データ長
  307. ;       ds(data):dx 出力データの先頭アドレス
  308. ;       ds(data):errorno 終了コード
  309. ;   OUT なし
  310. ;   保存レジスタ    si,di,bp,ds,es
  311. ;
  312. ;------------------------------------------------------------------------------
  313. ;
  314.     public  abort
  315. abort       proc
  316. ;
  317.     mov     ax,seg  _data
  318.     mov     ds,ax
  319.     mov     ah,40h
  320.     mov     bx,1
  321.     int     21h
  322. ;
  323.     mov     ax,errorno
  324.     mov     ah,4ch
  325.     int     21h
  326. ;
  327. abort       endp
  328. ;
  329.     end
  330.