Hi!
im having some trouble with, what to me apears as a strange error. the purpose of the code is to catenate some strings into a buffer and then display it (by setdlgitemtext, the strings beeing put together are parts of computation to get the momentum of an object). theres also a check wich control if the result of the computation should be added, and heres the weird thing: if the last part (the result) is to be added to the string i overwrites the whole string!
ok, so i sat down i started to debug the code (using OllyDbg). I sat a 'on memory access' breakpoint on the buffer, F9-ing my way through the code. but now it worked! the string in the buffer didnt get overwritten. so i tried to run the prg again, without the bp, but no luck - the buffer had been overwritten by the last string. so i tried debugging it again, setting the same bp and it worked again... after doing this several times im confused!
heres the "active"code (crash course in swedish =): l?ngd=length, tid=time, hastighet=speed massa=mass):


.data
...
visaP BOOL FALSE ; this ones changed elsewhere.

tmpdb db 18 dup(0)
resTxt db 140 dup(?)
resTTxt db 1150 dup(?)

resMn db 'M?tning %i:',0dh,0ah,0
resT db 't: ',0
resTE db ' s',0dh,0ah,0
resV db 'v: ',0
resVE db ' m/s',0dh,0ah,0
resP db 'P: ',0
resPE db ' Kgm/s2',0dh,0ah,0
resDelare db '----------------------------------------------------------',0dh,0ah,0
resKlar db 'Labb Klar',0
...
.code
...
GoLabbGo:
; doing some stuff to calculate the speed.
...
invoke FpuFLtoA,0,9,addr tmpdb,SRC1_FPU or SRC2_DIMM
invoke Strcat,offset resTxt,offset resT ; "t: "
invoke Strcat,offset resTxt,offset tmpdb ; <FpuFLtoA>
invoke Strcat,offset resTxt,offset resTE ; " s",0dh,0ah

invoke GetDlgItemText,hWnd,IDC_l1,addr l1,12h
invoke FpuAtoFL,addr l1,0,DEST_FPU ; st(0)=l?ngd, st(1)=tid
fdiv st(0),st(1) ; st(0)=l?ngd/tid=hastighet, st(1)=tid
invoke FpuFLtoA,0,9,addr tmpdb,SRC1_FPU or SRC2_DIMM
invoke Strcat,offset resTxt,offset resV ; "v: "
invoke Strcat,offset resTxt,offset tmpdb ; <FpuFLtoA>
invoke Strcat,offset resTxt,offset resVE ; " m/s",0dh,0ah

cmp visaP,FALSE
je labb_fardig
invoke GetDlgItemText,hWnd,IDC_m1,addr m1,12h
invoke FpuAtoFL,addr m1,0,DEST_FPU ; st(0)=massa, st(1)=hastighet, st(2)=tid
fmulp st(1),st(0) ; st(0)=hastighet*massa=P
invoke FpuFLtoA,0,9,addr tmpdb,SRC1_FPU or SRC2_DIMM
invoke Strcat,offset resTxt,offset resP ; "P: "
invoke Strcat,offset resTxt,offset tmpdb ; <FpuFLtoA>
invoke Strcat,offset resTxt,offset resPE ; " Kgm/s2",0dh,0ah

labb_fardig:
invoke Strcat,offset resTxt,offset resDelare
invoke Strcat,offset resTTxt,offset resTxt
dec antalChkd
jnz GoLabbGo

invoke Strcat,offset resTTxt,offset resKlar
invoke SetDlgItemText,hWnd,IDC_res1,addr resTTxt

...

; wanted to learn more about string handling so i wrote my own strcat and strlen.

Strcat proc USES eax ecx esi edi TillStrang:DWORD,FranStrang:DWORD
mov esi,FranStrang
mov edi,TillStrang
invoke Strlen,TillStrang
inc eax
add edi,eax
invoke Strlen,FranStrang
inc eax
mov ecx,eax
cld
rep movsb
mov byte ptr[edi],0h ; NULL-ending.
ret
Strcat endp
Strlen proc Sstrang:LPSTR
mov eax,[Sstrang]
rakna:
cmp byte ptr[eax],0h
lea eax,[eax+1]
jne rakna
sub eax,[Sstrang]
dec eax
ret
Strlen endp
...

the result looks something like this, when visaP is false:

M?tning 1:
t: xx.xxxxxxxxx s
v: xxx.xxxxxxxxx m/s
----------------------------------------------------------
M?tning 2:
t: xx.xxxxxxxxx s
v: xxx.xxxxxxxxx m/s
----------------------------------------------------------
Labb Klar

which it does. and this is how it should look if it is true:

M?tning 1:
t: xx.xxxxxxxxx s
v: xxx.xxxxxxxxx m/s
P: xxx.xxxxxxxxx Kgm/s2
----------------------------------------------------------
M?tning 2:
t: xx.xxxxxxxxx s
v: xxx.xxxxxxxxx m/s
P: xxx.xxxxxxxxx Kgm/s2
----------------------------------------------------------
Labb Klar

but it doesent, it looks like this (with an extra linefeed before the text):


M?tning 2:
t: xx.xxxxxxxxx s
v: xxx.xxxxxxxxx m/s
P: xxx.xxxxxxxxx Kgm/s2
----------------------------------------------------------
MLabb Klar

Unless theres a bp on the buffer memory-access...?!
ill attach the full project aswell (RadASM projectZipper :alright: ), so youll get to see the full code... if youd like =)!
Posted on 2004-04-10 19:02:19 by sluggo
I think your resTxt buffer may be too small. Based on your given theorital display, I counted 139 characters (including all the CRLF and terminating 0) and your buffer size is 140.

However, as shown, each data line starts with a heading such as t: showing only 1 space between the ":" and the output number. That 1 space is part of the header string (db 't: ',0).

The FpuFLtoA function would leave a blank space in front of the result if it is positive (as it is in your case). This would add another 3 characters to your string which would then start overwriting the resTTxt buffer with at least a terminating "0" and possibly a LF.

I'm almost certain that increasing the size of the resTxt buffer will clear the problem.

Raymond
Posted on 2004-04-10 21:49:57 by Raymond
The irony - Ive counted all the chars i dunno how many times to make shure the resTTxt buffer was big enough :rolleyes:.
But once again your right, thank you Raymond!:alright:
just out of curiosity, any ideas on why it (kinda) worked (some of the results got messed up but still the same amounts of chars) when i put a bp on access of the resTTxt-buf?
Posted on 2004-04-11 05:14:31 by sluggo
The only thing I can think of is that the bp may have temporarily overwritten the guilty terminating 0.

Raymond
Posted on 2004-04-11 11:11:08 by Raymond