Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 3rd, 2008, 2:34 PM   #1
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 294
Rep Power: 4 Klarre is on a distinguished road
[MASM32] Working with arrays

Hi!

I am working on a system managing key presses in a MASM application. The problem is that I must be doing something wrong with the array that keeps the press status of each key. (bool g_keys[256] // Position 0x20 means VK_SPACE i e.)

The "input_set_key" function always returns a non-zero value like my memset hasn't done anything with the array.

What can be the problem? I can't see it...
Thanks alot for your helpfulness!

/Klarre

.data

g_keys dd 0	; Pointer to the array of keys

.code

; ------------------------------------------------------------------------------
input_create proc
	invoke crt_malloc, 256	; Allocate 256 Bytes for the key array
	mov g_keys, eax	; Store the key array pointer

	invoke crt_memset, g_keys, 0, 256	; Make sure no buttons are pressed

	invoke input_is_key_pressed, 27	; DEBUG TEST. Returns a non-zero value

	xor eax, eax

	ret
input_create endp

; ------------------------------------------------------------------------------
input_is_key_pressed proc key:dword
	lea eax, [g_keys]	; Get the key array address
	add eax, key	; Offset to the correct position in the array
	mov eax, [eax]		; Read the value on the position in the array

	ret
input_is_key_pressed endp

end
__________________
http://www.klarre.se

Last edited by Klarre; Mar 3rd, 2008 at 2:58 PM.
Klarre is offline   Reply With Quote
Old Mar 3rd, 2008, 3:21 PM   #2
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 294
Rep Power: 4 Klarre is on a distinguished road
Re: [MASM32] Working with arrays

After finding the "Memory" debug window in Visual Studio, it was pretty simply to solve the bug. The functions is now looking like this and seems to work fine.

; ------------------------------------------------------------------------------
input_create proc
	invoke crt_malloc, 256	; Allocate 256 Bytes for the key array
	mov g_keys, eax	; Store the key array pointer

	invoke crt_memset, g_keys, 0, 256	; Make sure no buttons are pressed

	ret
input_create endp

; ------------------------------------------------------------------------------
input_is_key_pressed proc key:dword
	mov eax, g_keys	; Get the key array address
	add eax, key	; Offset to the correct position in the array
	mov ah, [eax]	; Read the value on the position in the array

	.if(ah)
		mov eax, 1
	.else
		mov eax, 0
	.endif

	ret
input_is_key_pressed endp
__________________
http://www.klarre.se
Klarre is offline   Reply With Quote
Old Mar 9th, 2008, 7:24 AM   #3
Irwin
Newbie
 
Join Date: Mar 2008
Posts: 9
Rep Power: 0 Irwin is on a distinguished road
Re: [MASM32] Working with arrays

IsKeyPressed proc Key:DWORD
    mov   ecx, [dwKey]
    movzx eax, byte [gKeys+ecx]
    test  eax, eax
    sete  al
    ret
IsKeyPressed endp

InputCreate  proc
    push  256d
    push  GPTR ; memory is filled with 0's
    call  GlobalAlloc
    push  27d
    push  IsKeyPressed
    ret
InputCreate endp

Also, a word to the wise. Don't use MSVCRT in ASM, it completely destroys the purpose of using ASM. The converting is fairly easy too:

malloc -> GlobalAlloc
memset -> stosb/stosw/stosd/stosq
Irwin is offline   Reply With Quote
Old Mar 9th, 2008, 1:55 PM   #4
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 294
Rep Power: 4 Klarre is on a distinguished road
Re: [MASM32] Working with arrays

Thanks for your input Irwin. I removed the msvcrt dependency from my project and changed the malloc function call to HeapAlloc since I read on MSDN that GlobalAlloc wasn't recommended.

Another question you may be able to answear on: Is it possible to link a program using the Visual Studio linker without linking with msvcrt.lib? I have set the entry point to main, but still got two linker errors I can't get rid of. So I guess it is impossible, right?
Error	1	error LNK2001: unresolved external symbol __RTC_Shutdown	main.obj
Error	2	error LNK2001: unresolved external symbol __RTC_InitBase	main.obj
__________________
http://www.klarre.se
Klarre is offline   Reply With Quote
Old Mar 10th, 2008, 2:30 AM   #5
Irwin
Newbie
 
Join Date: Mar 2008
Posts: 9
Rep Power: 0 Irwin is on a distinguished road
Re: [MASM32] Working with arrays

Quote:
Originally Posted by Klarre View Post
Thanks for your input Irwin. I removed the msvcrt dependency from my project and changed the malloc function call to HeapAlloc since I read on MSDN that GlobalAlloc wasn't recommended.

Another question you may be able to answear on: Is it possible to link a program using the Visual Studio linker without linking with msvcrt.lib? I have set the entry point to main, but still got two linker errors I can't get rid of. So I guess it is impossible, right?
Error	1	error LNK2001: unresolved external symbol __RTC_Shutdown	main.obj
Error	2	error LNK2001: unresolved external symbol __RTC_InitBase	main.obj
HeapAlloc is only recommended for small buffers, so for anything from 64-1024 bytes I use GlobalAlloc (then use VirtualAlloc for anything larger). Anyway, for a function like GetKeyboardState (which I assume you're using), I would use GlobalAlloc.

Also, you can completely remove any CRT dependency from an application by adding the /NODEFAULTLIB linker flag, then coupling it with the /entry:main (or whatever your entry function is called).
Irwin is offline   Reply With Quote
Old Mar 10th, 2008, 5:02 AM   #6
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 294
Rep Power: 4 Klarre is on a distinguished road
Re: [MASM32] Working with arrays

Ah, I see. Going to read up a little bit about the alloc functions.

I am using the /NODEFAULTLIB linker flag and have set the entry point. But this will not get rid of the linker errors...
__________________
http://www.klarre.se
Klarre is offline   Reply With Quote
Old Mar 10th, 2008, 6:51 AM   #7
Irwin
Newbie
 
Join Date: Mar 2008
Posts: 9
Rep Power: 0 Irwin is on a distinguished road
Re: [MASM32] Working with arrays

Quote:
Originally Posted by Klarre View Post
Ah, I see. Going to read up a little bit about the alloc functions.

I am using the /NODEFAULTLIB linker flag and have set the entry point. But this will not get rid of the linker errors...
Post your complete source and assembler/link flags and I'll find your problem.
Irwin is offline   Reply With Quote
Old Mar 10th, 2008, 7:23 AM   #8
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 294
Rep Power: 4 Klarre is on a distinguished road
Re: [MASM32] Working with arrays

The source file:
int main() { return 0; }

The linker command line (debug configuration):
/OUT:"G:\workspace\linktest\linktest\Debug\linktest.exe" /INCREMENTAL /NOLOGO /MANIFEST /MANIFESTFILE:"Debug\linktest.exe.intermediate.manifest" /NODEFAULTLIB /SUBSYSTEM:CONSOLE /ENTRY:"main" /MACHINE:X86 /ERRORREPORT:PROMPT
Causes these errors:
1>Linking...
1>main.obj : error LNK2001: unresolved external symbol __RTC_Shutdown
1>main.obj : error LNK2001: unresolved external symbol __RTC_InitBase
1>G:\workspace\linktest\linktest\Debug\linktest.exe : fatal error LNK1120: 2 unresolved externals

The linker command line (release configuration):
/OUT:"G:\workspace\linktest\linktest\Release\linktest.exe" /INCREMENTAL:NO /NOLOGO /MANIFEST /MANIFESTFILE:"Release\linktest.exe.intermediate.manifest" /NODEFAULTLIB /SUBSYSTEM:CONSOLE /OPT:REF /OPT:ICF /LTCG /ENTRY:"main" /MACHINE:X86 /ERRORREPORT:PROMPT
Causes these errors:
1>Linking...
1>main.obj : error LNK2001: unresolved external symbol @__security_check_cookie@4
1>G:\workspace\linktest\linktest\Release\linktest.exe : fatal error LNK1120: 1 unresolved externals
__________________
http://www.klarre.se
Klarre is offline   Reply With Quote
Old Mar 10th, 2008, 10:40 AM   #9
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 294
Rep Power: 4 Klarre is on a distinguished road
Re: [MASM32] Working with arrays

Oh, I meant in a C++ project, which I forgot to mention in the earlier posts.
__________________
http://www.klarre.se
Klarre is offline   Reply With Quote
Old Mar 10th, 2008, 10:41 AM   #10
Irwin
Newbie
 
Join Date: Mar 2008
Posts: 9
Rep Power: 0 Irwin is on a distinguished road
Re: [MASM32] Working with arrays

Oh, the problems aren't in your linking flags but rather your compiler flags. Just add /GS- to your compiler flags.

Good luck

Also, it's fine; I also code in C(++)
Irwin is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
joining associative arrays MiKuS PHP 2 Sep 6th, 2007 2:20 AM
Confusion With Pointers and Arrays JawaKing00 C 10 Sep 14th, 2006 7:33 AM
Arrays or Vectors? can342man C++ 2 Apr 20th, 2006 3:57 PM
Arrays in PHP MrMan9879 PHP 6 Jan 12th, 2006 9:18 PM
Arrays Gunman C++ 17 Oct 20th, 2005 6:11 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:43 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC