News:

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

Help on lstrcat

Started by sjums, January 14, 2011, 06:31:07 PM

Previous topic - Next topic

sjums

Hey everybody, I started to read about MASM ASM (or how you descripe this branch) yesterday, and've so far written this "game" :)

The purpose of the game is to guess a number, if you guess too high it tells you to go lover and vica versa. And finally when you hit the right number, it prints a congratz and SHOULD print how many tries you used, but doesn't entirely work as it should :)

as you can see i have two strings which contains the first part of the "count" sentence, and another that contains the rest. The problem is just that only the first string and the number is printed.. then the rest is "gone". I guess it has something to do with the null terminator? but i can't figure it out.

an alternative (simple) solutions is also okay, but I don't want something too advanced (I'm still a noob :P)

Thanks in advance, Sjums


.386
.model flat, stdcall
option casemap :none

include \masm32\include\windows.inc
include \masm32\include\masm32.inc
include \masm32\include\kernel32.inc

includelib \masm32\lib\masm32.lib
includelib \masm32\lib\kernel32.lib

include \masm32\macros\macros.asm

.data
corrTxt db "Correct answer",10,0
higherTxt db "You need to go higher..",10,0
lowerTxt db "You have to go lower..",10,0
triesTxt1 db "You used ",0
triesTxt2 db "  tries",10,0

.data?
guess dd ?
numG dd ?

.code
start:
mov ebx,0
_rtry:
mov edx, sval(input("Enter your guess: "))
push edx
pop guess
mov eax,78
inc ebx
cmp guess, eax
jz _corr
jg _low
jl _high

_low:
invoke StdOut, addr lowerTxt
jmp _rtry
_high:
invoke StdOut, addr higherTxt
jmp _rtry
_corr:
invoke StdOut, addr corrTxt
push ebx
pop numG
invoke lstrcat, addr triesTxt1, str$(numG)
invoke lstrcat, addr triesTxt1, addr triesTxt2
invoke StdOut, addr triesTxt1

invoke ExitProcess,0
end start


Example Run
C:\Users\sjums\Desktop\asm>guessGame.exe
Enter your guess: 70
You need to go higher..
Enter your guess: 80
You have to go lower..
Enter your guess: 78
Correct answer
You used 3
C:\Users\sjums\Desktop\asm>


dedndave

i wrote a similar program called "PullMyFinger"   :lol
        print   addr triesTxt1
        print   str$(numG)
        print   addr triesTxt2



sjums

works like a charm ^^

what's the diffrence bestween invoking a StdOut or use print ?

what's better? :)

dedndave

well - print is a masm32 library macro
it eventually calls stdout, i think
but - it allows for a variety of input parameter forms
notice that i didn't use lstrcat - lol
you can do it that way, too - but no need to in this case

here's a little program that'll keep you busy for hours on end....

dedndave

i am disappointed that noone likes my little program   :red

disintx

Personally, I'd rather try to find out _why_ something I'm doing isn't working.. =)

1. lstrcat definition straight from the Win32 API reference
LPTSTR lstrcat(
    LPTSTR  lpString1, // address of buffer for concatenated strings
    LPCTSTR  lpString2 // address of string to add to string1
   );
Parameters
lpString1 - Points to a null-terminated string. The buffer must be large enough to contain both strings.

lpString2 - Points to the null-terminated string to be appended to the string specified in the lpString1 parameter.

Return Value
If the function succeeds, the return value is a pointer to the buffer.
If the function fails, the return value is NULL. To get extended error information, call GetLastError.


*edit* After re-reading what I said the first time I realize I wasn't very clear on what I meant.
There must be a sizeable enough palce to put the string. From uses I've seen it isn't really meant to do what you are doing.
It's funny because this is like my second post when I first joined the forum. =)

2. As far as I know Win32 expects you to preserve EBX; in other words, avoid using it if you can, stick with EAX/ECX/EDX. If you must use EBX try to ensure that you save it beforehand.

3. I saw your code has "pop guess" to get 78dec into it for the cmp instruction..you can compare against a constant so just place cmp reg, 78d instead =)

(p.s. I don't mean to come off as a douche in any of this, just makin a list and checking it twice)


-------- after edit -------------
for reference, I'm posting my code.

Output:
X:\src\lstrcat>lstrcat.exe
Enter your guess: 3
You need to go higher..
Enter your guess: 3
You need to go higher..
Enter your guess: 3
You need to go higher..
Enter your guess: 78
Correct answer
You used 4 tries


and the code
.386
.model flat, stdcall
option casemap :none

include \masm32\include\windows.inc
include \masm32\include\masm32.inc
include \masm32\include\kernel32.inc

includelib \masm32\lib\masm32.lib
includelib \masm32\lib\kernel32.lib

include \masm32\macros\macros.asm



.data
corrTxt db "Correct answer",10,0
higherTxt db "You need to go higher..",10,0
lowerTxt db "You have to go lower..",10,0
triesTxt1 db "You used ",0
triesTxt2 db " tries",10,0
Output db 50 dup(0)

.data?
guess dd ?
numG dd ?

.code
start:
;mov ebx,0
xor ecx,ecx
_rtry:
push ecx
mov edx, sval(input("Enter your guess: "))
;push edx
;pop guess
mov guess, edx
;mov eax,78
pop ecx
inc ecx
cmp guess, 78d
jz _corr
jg _low
jl _high

_low:
push ecx
invoke StdOut, addr lowerTxt
pop ecx
jmp _rtry
_high:
push ecx
invoke StdOut, addr higherTxt
pop ecx
jmp _rtry
_corr:
push ecx
invoke StdOut, addr corrTxt
pop numG
;invoke lstrcat, addr triesTxt1, str$(numG)
invoke lstrcat, addr Output, addr triesTxt1
invoke lstrcat, addr Output, str$(numG)
invoke lstrcat, addr Output, addr triesTxt2
;invoke lstrcat, addr triesTxt1, addr triesTxt2
invoke StdOut, addr Output

invoke ExitProcess,0
end start


In other words it's easier to do what dedndave said: no reason to concatenate these strings when you can just print all three separate, otherwise you end up with 3 lstrcats and then an stdout.


P.p.s. I decided to use ECX instead and lo and behold something (somewhere in StdOut change ECX so I had to preserve via push/pop and I didn't feel like cleaning, sorry)