Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Assembly (http://www.programmingforums.org/forum20.html)
-   -   [MASM32] Working with arrays (http://www.programmingforums.org/showthread.php?t=15307)

Klarre Mar 3rd, 2008 2:34 PM

[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


Klarre Mar 3rd, 2008 3:21 PM

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


Irwin Mar 9th, 2008 7:24 AM

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

Klarre Mar 9th, 2008 1:55 PM

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


Irwin Mar 10th, 2008 2:30 AM

Re: [MASM32] Working with arrays
 
Quote:

Originally Posted by Klarre (Post 142259)
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).

Klarre Mar 10th, 2008 5:02 AM

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...

Irwin Mar 10th, 2008 6:51 AM

Re: [MASM32] Working with arrays
 
Quote:

Originally Posted by Klarre (Post 142285)
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.

Klarre Mar 10th, 2008 7:23 AM

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


Klarre Mar 10th, 2008 10:40 AM

Re: [MASM32] Working with arrays
 
Oh, I meant in a C++ project, which I forgot to mention in the earlier posts.

Irwin Mar 10th, 2008 10:41 AM

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(++) :P


All times are GMT -5. The time now is 1:06 AM.

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