home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / simtel / sigm / vols000 / vol075 / shellm.asm < prev    next >
Assembly Source File  |  1984-04-29  |  3KB  |  170 lines

  1. .TITLE    'IN MEMORY SHELL-metzner sort'
  2. .sbttl    'from KILOBAUD april 1981 p164'
  3. ;
  4. .remark    'For fixed length records stored in memory
  5.     Put no. of records in n1 and m1. The length
  6.     of each record is stored at k1, and the starting
  7.     address at j1. Start sort by calling location
  8.     "entry". To change to descending sort change the
  9.     instruction at neq: to DAH.'
  10. ;
  11. n1:    .word    0    ; number of records
  12. m1:    .word    0    ; ..same here
  13. k1:    .word    0    ; length of records
  14. j1:    .word    0    ; starting address of strings
  15. i1:    .word    0    ; ptr
  16. ml1:    .word    0    ; ptr
  17. dj1:    .word    0    ; ptr
  18. di1:    .word    0    ;ptr
  19. ;
  20. entry:    lhld    j1    ; get start address
  21.     push    h    ; ..save
  22.     lhld    k1    ; get length
  23.     push    h    ; ..it too
  24. div:    xra    a    ; m1=m1/2
  25.     lhld    m1
  26.     mov    a,h
  27.     rar
  28.     mov    h,a
  29.     mov    a,l
  30.     rar
  31.     mov    l,a
  32.     shld    m1    ; save new m1
  33. ;
  34.     ora    h    ; check if done
  35.     jnz    ndon
  36.     pop    b    ; finished
  37.     pop    d    ; ..so return
  38.     ret        ; ...now
  39. ;
  40. ;    set k1=n1-m1
  41. ;
  42. ndon:    xchg        ; m1 to de
  43.     lhld    n1
  44.     mov    a,l
  45.     sub    e
  46.     mov    l,a
  47.     mov    a,h
  48.     sbb    d
  49.     mov    h,a
  50.     shld    k1
  51.     lxi    h,1    ; set and save i=j=1
  52.     shld    j1
  53.     shld    i1
  54. ;
  55. ;    calc & save addr offset = m1*i1
  56. ;
  57.     dcr    l
  58.     pop    b    ; length of str=i1
  59.     push    b    ; ..put it back
  60. lp1:    dad    d
  61.     dcx    b
  62.     mov    a,b
  63.     ora    c
  64.     jnz    lp1
  65.     shld    ml1
  66. ;
  67.     xchg        ; calc & save d(j), d(i), d(i+m)
  68.     pop    b
  69.     pop    h
  70.     push    h
  71.     push    b
  72. lp2:    shld    dj1
  73.     shld    di1
  74.     xchg
  75.     dad    d
  76.     xchg        ; HL has d(i), DE has d(i+m)
  77. ;
  78. ;    compare strings and switch
  79. ;
  80. cp1:    pop    b    ; len of string=l1
  81.     push    b
  82. lp3:    ldax    d    ; compare each byte
  83.     sub    m
  84.     jnz    neq    ; not equal
  85.     inx    h    ; if =, then next byte
  86.     inx    d
  87.     dcx    b
  88.     mov    a,b
  89.     ora    c
  90.     jnz    lp3
  91.     jmp    nsw    ; if done, don't switch
  92. ;
  93. ;    change next instruction to jc for descending
  94. ;
  95. neq:    jnc    nsw    ; if d(i)<d(i+m) don't switch
  96. ;
  97. sw:    push    b    ; switch bytes not equal
  98.     mov    b,m
  99.     ldax    d
  100.     mov    m,a
  101.     mov    a,b
  102.     stax    d
  103.     inx    h
  104.     inx    d
  105.     pop    b
  106.     dcx    b
  107.     mov    a,b
  108.     ora    c
  109.     jnz    sw
  110. ;
  111. ;    strings switched, chk if i1-m1 < 1
  112. ;
  113.     lhld    m1
  114.     mov    a,h
  115.     cma
  116.     mov    d,a
  117.     mov    a,l
  118.     cma    
  119.     mov    e,a
  120.     lhld    i1
  121.     dad    d    ; if i1-m1<1 then jump to same as
  122.             ; ..no switch
  123.     jnc    nsw
  124. ;
  125. ;    calc new d(i), d(i+m)
  126. ;
  127.     inx    h    ; save    new i1=i1-m
  128.     shld    i1
  129.     lhld    di1    ; old d(i)=new d(i+m)
  130.     xchg
  131.     lhld    ml1    ; address offset
  132.     mov    a,e    ; new d(i)=old d(i)-offset
  133.     sub    l
  134.     mov    l,a
  135.     mov    a,d
  136.     sbb    h
  137.     mov    h,a
  138.     shld    di1    ; save new d(i)
  139.     jmp    cp1    ; goto compare strings
  140. ;
  141. ;    check for j>k
  142. ;
  143. nsw:    lhld    j1
  144.     inx    h    ; save new j=old j+1
  145.     shld    j1
  146.     shld    i1
  147.     xchg
  148.     lhld    k1
  149.     mov    a,l
  150.     sub    e
  151.     mov    a,h
  152.     sbb    d
  153.     jc    div    ; if j>k goto beginning and
  154.             ; ..divide m1
  155. ;
  156. ;    calc new d(j), d(i)
  157. ;
  158.     lhld    dj1
  159.     pop    d
  160.     push    d
  161.     dad    d    ; new d(j)=old d(j+1)
  162.     xchg
  163.     lhld    ml1
  164.     xchg
  165.     jmp    lp2
  166. ;
  167. ; that all folks
  168. ;
  169.     .end    entry
  170.