home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c081_11 / 9.ddi / CHAPXMPL.ZIP / XCHANGE.ASM < prev    next >
Encoding:
Assembly Source File  |  1991-02-13  |  1.8 KB  |  47 lines

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