News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

calc string

Started by ragdog, November 20, 2007, 03:20:38 PM

Previous topic - Next topic

ragdog

hi guys

i have a problem with :

how to calculate a string right (if length max.24) ... length of string - 36 = count of spaces
how to fix the endsign on same place..without moving it..

example:

<      word1:%s                                 >


szFormat db "  <                word1:%s                                 >",13,10,0


mov edi,0
    lea esi, hString
      INVOKE lstrlen, ADDR hString
      mov ebx, eax
      sub ebx, 36
   @@:
      inc edi
      mov DWORD PTR [esi], 20h
      cmp edi,ebx
      jne @B
     invoke wsprintf,addr lpBuffer,addr szFormat,ADDR hString
     invoke MessageBox,hWnd,addr lpBuffer,0,MB_OK



can your help me please

regards
ragdog

Mark Jones

Hi ragdog, so you want the function to spit out the same length string for any value of %s?

It might be easier to code a custom parsing algorithm. This is not too difficult. Try something like:


; assemble with "console assemble and link"
include masm32rt.inc                    ; masm32 v9.2 assemble-time libs

T MACRO _jump:VARARG                    ; Branch Hint: Taken
    DB 2Eh
    _jump
ENDM

NT MACRO _jump:VARARG                   ; Branch Hint: Not Taken
    DB 3Eh
    _jump
ENDM

.data?
    szWord      dd  ?                   ; pointer to entered word
    szBuffer    db  64 dup(?)           ; ascii string buffer   
.const
    szFormat    db  "< Word:*                                       >",13,10,0

.code
start:
    print chr$("szFormat: ",13,10)      ; display our formatting string
    print addr szFormat
go:
    mov eax,input("Enter word(s) to replace the asterisk: ")
    mov szWord,eax                      ; save location of entered word
    cmp byte ptr [eax+1],10             ; was enter pressed?
T   jnz ok                              ; if not, continue
    ret                                 ; else exit

ok: lea esi,szFormat                    ; load addresses of string locations
    lea edi,szBuffer                    ; into source and dest registers
    mov ecx,szWord                      ; szWord already a pointer, use as ecx
@@: movsb                               ; copy byte edi<--esi, inc both ++
    cmp byte ptr [esi],"*"              ; look for the asterisk
T   jnz @B                              ; keep copying until found
@@: mov al,byte ptr [ecx]               ; fetch a byte of the new word
    cmp al,0                            ; are we at end of new word?
NT  jz @F                               ; if so, break out
    inc ecx                             ; else point to next byte of new word
    mov byte ptr [edi],al               ; put byte in the destination buffer
    inc esi                             ; and increment the
    inc edi                             ; other two pointers
    jmp @B                              ; and loop back
@@: movsb                               ; finish copying rest of string
    cmp byte ptr [edi-1],00             ; wait for ending null to be written
T   jnz @B                              ; and loop

    print addr szBuffer                 ; display result
    jmp go                              ; and start over

end start


This returns:


szFormat:
< Word:*                                       >
Enter word(s) to replace the asterisk: hello
< Word:hello                                   >
Enter word(s) to replace the asterisk: hello world!
< Word:hello world!                            >
Enter word(s) to replace the asterisk: hi there world, this is assembly
< Word:hi there world, this is assembly        >
Enter word(s) to replace the asterisk:
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

ragdog

thanks Mark its Perfect for my app :U

regards
ragdog