home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / cexpress / strings / padleft.asm < prev    next >
Encoding:
Assembly Source File  |  1989-05-03  |  1.7 KB  |  69 lines

  1. ;void  pad_left(strg,ch,length);
  2. ;  unsigned char  *strg,ch;
  3. ;  unsigned short  length;
  4.  
  5.     EXTRN  _memory_model:byte
  6.     EXTRN  _error_code:byte
  7.  
  8. _TEXT    SEGMENT BYTE PUBLIC 'CODE'
  9.     ASSUME CS:_TEXT
  10.     PUBLIC _pad_left
  11. _pad_left proc near
  12.     push bp            ;
  13.     mov  bp,sp        ;set up stack frame
  14.     push di            ;
  15.     push si            ;
  16.     push ds            ;save DS
  17.     mov  _error_code,1    ;1 = error
  18.     cmp  _memory_model,0    ;near or far?
  19.     jle  begin        ;jump if near
  20.     inc  bp            ;else add 2 to BP
  21.     inc  bp            ;
  22. begin:    cmp  _memory_model,2    ;data near or far?
  23.     jb   L0            ;jump if near
  24.     les  di,dword ptr[bp+4] ;get string address
  25.     mov  ax,es        ;DS = ES
  26.     mov  ds,ax        ;
  27.     inc  bp            ;add 2 to bp since dword ptr
  28.     inc  bp            ;
  29.     jmp  short L1        ;jump ahead
  30. L0:    mov  di,[bp+4]        ;near case
  31.     mov  ax,ds        ;
  32.     mov  es,ax        ;
  33. L1:    sub  cx,cx        ;must find string length
  34.     mov  si,di        ;copy string ptr
  35. L2:    cmp  byte ptr[si],0     ;end of string?
  36.     je   L3            ;
  37.     inc  si            ;
  38.     inc  cx            ;
  39.     jmp  short L2        ;loop till null
  40. L3:    sub  bx,bx        ;
  41.     mov  bl,[bp+8]        ;new string length in BL
  42.     cmp  cl,bl        ;compare the two lengths
  43.     jae  L5            ;quit if old len >= new
  44.     add  di,bx        ;pt DI to new string end
  45.     sub  bx,cx        ;number of spaces to pad
  46.     inc  cx            ;move extra char for null
  47.     std            ;set direction flag
  48.     rep  movsb        ;move old string right
  49.     mov  cx,bx        ;spaces to pad in CX
  50.     mov  al,[bp+6]        ;get pad character
  51. L4:    mov  es:[di],al        ;insert pad char
  52.     dec  di            ;move to next position
  53.     loop L4            ;go do next char
  54.     pop  ds            ;restore DS
  55.     dec  _error_code    ;0 = no error
  56.     jmp  short L6        ;jump ahead and quit
  57. L5:    pop  ds            ;exit with error
  58. L6:    pop  si            ;
  59.     pop  di            ;
  60.     pop  bp            ;
  61.     cld            ;
  62.     cmp  _memory_model,0    ;quit
  63.     jle  quit        ;
  64.     db   0CBh        ;RET far
  65. quit:    ret            ;RET near
  66. _pad_left ENDP
  67. _TEXT    ENDS
  68.     END
  69.