home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 181.img / TASM-101.ZIP / CHAPXMPL.ARC / XCHANGE.ASM < prev    next >
Assembly Source File  |  1989-05-02  |  2KB  |  40 lines

  1. CODE      SEGMENT
  2.           ASSUME cs:CODE,ds:NOTHING
  3.  
  4. ; Parameters (note that offset are +2 because of push bp)
  5.  
  6. var1      equ     DWORD PTR ss:[bp+12]
  7. var2      equ     DWORD PTR ss:[bp+8]
  8. count     equ     WORD PTR ss:[bp+6]
  9.  
  10.  
  11. Exchange  PROC FAR
  12.           PUBLIC Exchange
  13.           cld                        ;exchange goes upward
  14.           mov     dx,ds              ;save DS
  15.           push    bp
  16.           mov     bp,sp              ;get stack base
  17.           lds     si,var1            ;get first address
  18.           les     di,var2            ;get second address
  19.           mov     cx,count           ;get number of bytes to move
  20.           shr     cx,1               ;get word count (low bit -> carry)
  21.           jnc     ExchangeWords      ;if no odd byte, enter loop
  22.           mov     al,es:[di]         ;read odd byte from var2
  23.           movsb                      ;move a byte from var1 to var2
  24.           mov     [si-1],al          ;write var2 byte to var1
  25.           jz      Finis              ;done if only one byte to exchange
  26. ExchangeWords:
  27.           mov     bx,-2              ;BX is a handy place to keep -2
  28. ExchangeLoop:
  29.           mov     ax,es:[di]         ;read a word from var2
  30.           movsw                      ;do a move from var1 to var2
  31.           mov     [bx][si],ax        ;write var2 word to var1
  32.           loop    ExchangeLoop       ;repeat "count div 2" times
  33. Finis:
  34.           mov     ds,dx              ;get back Turbo's DS
  35.           pop     bp
  36.           ret     10
  37. Exchange  ENDP
  38. CODE      ENDS
  39.           END
  40.