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

  1. ; Turbo Assembler    Copyright (c) 1988, 1991 By Borland International, Inc.
  2.  
  3. ; REVERSE.ASM - Prints a string in reverse
  4.  
  5. ; From the Turbo Assembler Users Guide - Getting started
  6.  
  7.    DOSSEG
  8.    .MODEL SMALL
  9.    .STACK 100h
  10.    .DATA
  11. MAXIMUM_STRING_LENGTH  EQU  1000
  12. StringToReverse  DB  MAXIMUM_STRING_LENGTH DUP(?)
  13. ReverseString    DB  MAXIMUM_STRING_LENGTH DUP(?)
  14.    .CODE
  15.    mov  ax,@data
  16.    mov  ds,ax                       ;set DS to point to the data segment
  17.    mov  ah,3fh                      ;DOS read from handle function #
  18.    mov  bx,0                        ;standard input handle
  19.    mov  cx,MAXIMUM_STRING_LENGTH    ;read up to maximum number of characters
  20.    mov  dx,OFFSET StringToReverse   ;store the string here
  21.    int  21h                         ;get the string
  22.    and  ax,ax                       ;were any characters read?
  23.    jz   Done                        ;no, so you're done
  24.    mov  cx,ax                       ;put string length in CX, where
  25.                                     ; you can use it as a counter
  26.    push cx                          ;save the string length
  27.    mov  bx,OFFSET StringToReverse
  28.    mov  si,OFFSET ReverseString
  29.    add  si,cx
  30.    dec  si                          ;point to the end of the
  31.                                     ; reverse string buffer
  32. ReverseLoop:
  33.    mov  al,[bx]                     ;get the next character
  34.    mov  [si],al                     ;store the characters in reverse order
  35.    inc  bx                          ;point to next character
  36.    dec  si                          ;point to previous location in reverse buffer
  37.    loop ReverseLoop                 ;move next character, if any
  38.    pop  cx                          ;get back the string length
  39.    mov  ah,40h                      ;DOS write from handle function #
  40.    mov  bx,1                        ;standard output handle
  41.    mov  dx,OFFSET ReverseString     ;print this string
  42.    int  21h                         ;print the reversed string
  43. Done:
  44.    mov  ah,4ch                      ;DOS terminate program function #
  45.    int  21h                         ;terminate the program
  46.    END
  47.