home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1990 / 03 / paterson.lst < prev    next >
File List  |  1990-02-13  |  4KB  |  224 lines

  1. ASSEMBLY LANGUAGE TRICKS OF THE TRADE
  2. by Tim Paterson
  3.  
  4.  
  5. #1   Binary-to-ASCII Conversion
  6.  
  7.     add    al,"0"    ;Handle 0 - 9
  8.     cmp    al,"9"    ;Did it work?
  9.     jbe    HaveAscii
  10.     add    al,"A" - ("9" + 1)    ;Apply correction for 0AH - 0FH
  11. HaveAscii:
  12.  
  13. -------------
  14.  
  15.  
  16.     add    al,90H    ;90H - 9FH
  17.     daa        ;90H - 99H, 00H - 05H +CY
  18.     adc    al,40H    ;0D0H - 0D9H +CY, 41H - 46H
  19.     daa        ;30H - 39H, 41H - 46H = "0"-"9", "A"-"F"
  20.  
  21.  
  22. #2  Absolute Value
  23.  
  24.     or    ax,ax    ;Set flags
  25.     jns    AxPositive    ;Already the right answer if positive
  26.     neg    ax    ;It was negative, so flip sign
  27. AxPositive:
  28.  
  29. -------------
  30.     cwd        ;Extend sign through dx
  31.     xor    ax,dx    ;Complement ax if negative
  32.     sub    ax,dx    ;Increment ax if it was negative
  33.  
  34.  
  35. #3 Smaller of Two Values (``MIN'')
  36.  
  37.     cmp    ax,bx
  38.     jl    AxSmaller
  39.     xchg    ax,bx    ;Swap smaller into ax
  40. AxSmaller:
  41.  
  42. -------------
  43.     sub    ax,bx    ;Could overflow if signs are different!!
  44.     cwd        ;dx = 0 if ax >= bx, dx = 0FFFFH if ax < bx
  45.     and    ax,dx    ;ax = 0 if ax >= bx, ax = ax - bx if ax < bx
  46.     add    ax,bx    ;ax = bx if ax >=bx, ax = ax if ax < bx
  47.  
  48.  
  49. #4 Convert to Uppercase
  50.  
  51.     cmp    al,"a"
  52.     jb    CaseOk
  53.     cmp    al,"z"
  54.     ja    CaseOk
  55.     sub    al,"a"-"A"    ;In range "a" - "z", apply correction
  56. CaseOk:
  57.  
  58. -------------
  59.  
  60.     sub    al,"a"    ;Lower case now 0 - 25
  61.     cmp    al,"z" - "a" +1    ;Set CY flag if lower case
  62.     sbb    ah,ah    ;ah = 0FFH if lower case, else 0
  63.     and    ah,"a" - "A"    ;ah = correction or zero
  64.     sub    al,ah    ;Apply correction, lower to upper
  65.     add    al,"a"    ;Restore base
  66.  
  67.  
  68. #5 Fast String Move
  69.  
  70.     shr    cx,1    ;Convert to word count
  71. rep    movsw        ;Move words
  72.     jnc    AllMoved    ;CY clear if no odd byte
  73.     movsb        ;Copy that last odd byte
  74. AllMoved:
  75.  
  76. -------------
  77.  
  78.     shr    cx,1    ;Convert to word count
  79. rep    movsw        ;Move words
  80.     adc    cx,cx    ;Move carry back into cx
  81. rep    movsb        ;Move one more if odd count
  82.  
  83.  
  84. #6 Binary/Decimal Conversion
  85.  
  86.     aam        ;al = ones, ah = tens & hundreds
  87.     mov    cl,al    ;Save ones in cl
  88.     mov    al,ah    ;Set up to do it again
  89.     aam        ;ah = hundreds, al = tens, cl = ones
  90.  
  91. -------------
  92.  
  93. ;ah = hundreds, al = tens, cl = ones
  94.     aad        ;Combine hundreds and tens
  95.     mov    ah,al
  96.     mov    al,cl    ;Move ones to al
  97.     aad        ;Binary result in ax, mod 256
  98.  
  99.  
  100. #7  Multiple Bit Testing
  101.  
  102.     mov    al,[Flag]
  103.     test    al,Bit1
  104.     jnz    Bit1Set
  105.     test    al,Bit2
  106.     jz    BothZero
  107. Bit2Only:
  108.     ...
  109.  
  110. Bit1Set:
  111.     test    al,Bit2
  112.     jnz    BothOne
  113. Bit1Only:
  114.  
  115. -------------
  116.  
  117.  
  118.     test    [Flag],Bit1 + Bit2
  119.     jz    BothZero
  120.     jpe    BothOne    ;Bits are equal, but not both zero
  121. ;One (and only one) bit is set
  122. .erre    Bit1 EQ 80H    ;Verify Bit1 is the sign bit
  123.     js    Bit1Only
  124. Bit2Only:
  125.  
  126.  
  127. #8  Function Dispatcher
  128.  
  129. ;Function number in cx
  130.     jcxz    Function0
  131.     dec    cx
  132.     jz    Function1
  133.     dec    cx
  134.     jz    Function2
  135.     ...
  136.  
  137. -------------
  138.  
  139. ;Function number in bx
  140.     shl    bx,1
  141.     jmp    tDispatch[bx]
  142.  
  143. -------------
  144.  
  145. ;Function number in cx
  146.     jcxz    Function0
  147.     loop    NotFunc1
  148. Function1:
  149.     ...
  150.  
  151. NotFunc1:
  152.     loop    NotFunc2
  153. Function2:
  154.     ...
  155.  
  156. NotFunc2:
  157.     loop    NotFunc3
  158. Function3:
  159.     ...
  160.  
  161.  
  162. #9 Skipping Instructions
  163.  
  164. Entry1:
  165.     mov    al,0
  166.     jmp    Body
  167.  
  168. Entry2:
  169.     mov    al,1
  170.     jmp    Body
  171.  
  172. Entry3:
  173.     mov    al,-1
  174. Body:
  175.  
  176. -------------
  177.  
  178. SKIP2F    MACRO
  179.     db    3DH    ;Opcode byte for CMP AX,<immed>
  180.     ENDM
  181.  
  182. Entry1:
  183.     mov    al,0
  184.     SKIP2F        ;Next 2 bytes are immediate data
  185. Entry2:
  186.     mov    al,1
  187.     SKIP2F        ;Next 2 bytes are immediate data
  188. Entry3:
  189.     mov    al,-1
  190. Body:
  191.  
  192. The effect of this when entered at Entry1 is:
  193.  
  194. Entry1:
  195.     mov    al,0
  196.     cmp    ax,01B0H    ;Data is MOV AL,1
  197.     cmp    ax,0FFB0H    ;Data is MOV AL,-1
  198. Body:
  199.  
  200. -------------
  201.  
  202. SKIP2    MACRO    ModReg
  203. IFIDNI    <ModReg>,<f>    ;Modify flags?
  204.     db    3DH    ;Opcode byte for CMP AX,<immed>
  205. ELSE
  206. ?_i    =    0
  207.     IRP    Reg,<ax,cx,dx,bx,sp,bp,si,di>
  208. IFIDNI    <ModReg>,<Reg>    ;Find the register in list yet?
  209.     db    0B8H + ?_i
  210.     EXITM
  211. ELSE
  212. ?_i    =    ?_i + 1
  213. ENDIF    ;IF ModReg = Reg
  214.     ENDM    ;IRP
  215. .errnz    ?_i EQ 8    ;Flag an error if no match
  216. ENDIF    ;IF ModReg = F
  217.     ENDM    ;SKIP2
  218.  
  219. ;Examples
  220.     SKIP2    f    ;Modify flags only
  221.     SKIP2    ax    ;Destroy ax, flags preserved
  222.  
  223.  
  224.