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

  1. ; Turbo Assembler    Copyright (c) 1988, 1991 By Borland International, Inc.
  2. ;
  3. ; MULT_C&D.ASM
  4. ;
  5. ; Program to demonstrate use of multiple code and data segments.
  6. ;
  7. ; Reads a string from the console, stores it in one data
  8. ; segment, copies the string to another data segment, converting
  9. ; it to lowercase in the process, then prints the string to the
  10. ; console. Uses functions in another code segment to read,
  11. ; print, and copy the string.
  12. ;
  13. StackSeg     SEGMENT   PARA STACK 'STACK'
  14.      DB   512 DUP (?)
  15. StackSeg     ENDS
  16.  
  17. MAX_STRING_LENGTH   EQU  1000
  18.  
  19. SourceDataSeg  SEGMENT   PARA PRIVATE 'DATA'
  20. InputBuffer    DB   MAX_STRING_LENGTH DUP (?)
  21. SourceDataSeg  ENDS
  22.  
  23. DestDataSeg    SEGMENT   PARA PRIVATE 'DATA'
  24. OutputBuffer   DB   MAX_STRING_LENGTH DUP (?)
  25. DestDataSeg    ENDS
  26.  
  27. SubCode   SEGMENT   PARA PRIVATE 'CODE'
  28.      ASSUME    CS:SubCode
  29. ;
  30. ; Subroutine to read a string from the console. String end is
  31. ; marked by a carriage-return, which is converted to a
  32. ; carriage-return/linefeed pair so it will advance to the next
  33. ; line when printed. A 0 is added to terminate the string.
  34. ;
  35. ; Input:
  36. ;    ES:DI - location to store string at
  37. ;
  38. ; Output: None
  39. ;
  40. ; Registers destroyed: AX,DI
  41. ;
  42. GetString PROC FAR
  43. GetStringLoop:
  44.      mov  ah,1
  45.      int  21h                             ;get the next character
  46.      stosb                                ;save it
  47.      cmp  al,13                           ;is it a carriage-return?
  48.      jnz  GetStringLoop ;no-not done yet
  49.      mov  BYTE PTR es:[di],10
  50.      mov  BYTE PTR es:[di+1],0            ;end the string with a linefeed
  51.                                           ; and a 0
  52.      ret
  53. GetString ENDP
  54. ;
  55. ; Subroutine to copy a string, converting it to lowercase.
  56. ;
  57. ; Input:
  58. ;    DS:SI - string to copy
  59. ;    ES:DI - place to put string
  60. ;
  61. ; Output: None
  62. ;
  63. ; Registers destroyed: AL, SI, DI
  64. ;
  65. CopyLowercase  PROC FAR
  66. CopyLoop:
  67.      lodsb
  68.      cmp  al,'A'
  69.      jb   NotUpper
  70.      cmp  al,'Z'
  71.      ja   NotUpper
  72.      add  al,20h                        ;convert to lowercase if it's uppercase
  73. NotUpper:
  74.      stosb
  75.      and  al,al                         ;was that the 0 that ends the string?
  76.      jnz  CopyLoop                      ;no, copy another character
  77.      ret
  78. CopyLowercase  ENDP
  79. ;
  80. ; Subroutine to display a string to the console.
  81. ;
  82. ; Input:
  83. ;    DS:SI - string to display
  84. ;
  85. ; Output: None
  86. ;
  87. ; Registers destroyed: AH,DL,SI
  88. ;
  89. DisplayString  PROC FAR
  90. DisplayStringLoop:
  91.      mov  dl,[si]                       ;get the next character
  92.      and  dl,dl                         ;is this the 0 that ends the string?
  93.      jz   DisplayStringDone             ;yes, we're done
  94.      inc  si                            ;point to the following character
  95.      mov  ah,2
  96.      int  21h                           ;display the character
  97.      jmp  DisplayStringLoop
  98. DisplayStringDone:
  99.      ret
  100. DisplayString  ENDP
  101. SubCode   ENDS
  102.  
  103. Code SEGMENT   PARA PRIVATE 'CODE'
  104.      ASSUME    CS:Code,DS:NOTHING,ES:NOTHING,SS:StackSeg
  105. ProgramStart:
  106.      cld                                ;make string instructions increment
  107.                                         ; their pointer registers
  108. ;
  109. ; Read a string from the console into InputBuffer.
  110. ;
  111.      mov  ax,SourceDataSeg
  112.      mov  es,ax
  113.      ASSUME    ES:SourceDataSeg
  114.      mov  di,OFFSET InputBuffer
  115.      call GetString                     ;read string from the console and
  116.                                         ; store it at ES:DI
  117. ;
  118. ; Print a linefeed to advance to the next line.
  119. ;
  120.      mov  ah,2
  121.      mov  dl,10
  122.      int  21h
  123. ;
  124. ; Copy the string from InputBuffer to OutputBuffer, converting
  125. ; it to lowercase in the process.
  126. ;
  127.      push es
  128.      pop  ds
  129.      ASSUME    DS:SourceDataSeg
  130.      mov  ax,DestDataSeg
  131.      mov  es,ax
  132.      ASSUME    ES:DestDataSeg
  133.      mov  si,OFFSET InputBuffer         ;copy from DS:SI...
  134.      mov  di,OFFSET OutputBuffer        ;...to ES:DI...
  135.      call CopyLowercase                 ;...making it lowercase
  136. ;
  137. ; Display the lowercase string.
  138. ;
  139.      push es
  140.      pop  ds
  141.      ASSUME    DS:DestDataSeg
  142.      mov  si,OFFSET OutputBuffer
  143.      call DisplayString                 ;display string at DS:SI to the console
  144. ;
  145. ; Done.
  146. ;
  147.      mov  ah,4ch
  148.      int  21h
  149. Code ENDS
  150.      END  ProgramStart
  151.