home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 181.img / TASM-101.ZIP / MULT_C&D.ASM < prev    next >
Assembly Source File  |  1988-10-31  |  4KB  |  148 lines

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