Skip to main content
GameDev.net gamedev.net
🔒 Locked

Assembly Language Problem

Started by Lith Oct 19, 2010 at 11:50 AM 3 replies 1.7k views
Original Post
Lith
Lith
I have been learning assembly and i quite like it, it's a nice way of programming. However, i have come across a problem. Here is my code:
include 'win32ax.inc'                                                                                                  ;The windows **** that i need.data        InputHandle DD ?                                                                                               ;This will store the input handle from windows        OutputHandle DD ?                                                                                              ;This will store the output handle from windows        NumberOfCharactersRead DD ?                                                                                    ;How many characters were read from the console        NumberOfCharactersWritten DD ?                                                                                 ;How many characters were written to the console        UserInput DB ?                                                                                                 ;What the user actully typed in        WrongNumberOfCharactersString DB 'You must only enter 1 character!'                                            ;The string that will be displayed when the user doesnt enter the right number of characters        EndlineCharacter DB 13                                                                                         ;This is the ASCII code for a new line.code        Main:                invoke AllocConsole                                                                                    ;Init the console                invoke GetStdHandle,STD_INPUT_HANDLE                                                                   ;Put the input handle into eax                mov [InputHandle],eax                                                                                  ;Store the input handle in a varible                invoke GetStdHandle,STD_OUTPUT_HANDLE                                                                  ;Put the output handle into eax                mov [OutputHandle],eax                                                                                 ;Store the output handle in a varible                jmp GetUserInput                                                                                       ;Go and get the user input        GetUserInput:                invoke ReadConsole,[InputHandle],UserInput,1,NumberOfCharactersRead,0                                  ;Get the input                mov eax,[NumberOfCharactersRead]                                                                       ;Move how many characters the user enterd into eax in preperation for the if statement                mov ebx,1                                                                                              ;Move how many characters the user was supposed to enter into edx for the if statement                cmp eax,ebx                                                                                            ;later                jg TooManyCharacters                                                                                   ;If the user enterd too many characters jump to a label                jmp ExitProgram                                                                                        ;If the user did everything right, exit the program        TooManyCharacters:                invoke WriteConsole,[OutputHandle],EndlineCharacter,1,NumberOfCharactersWritten,0                      ;Go to a new line                invoke WriteConsole,[OutputHandle],WrongNumberOfCharactersString,31,NumberOfCharactersWritten,0        ;Write the error message                invoke WriteConsole,[OutputHandle],EndlineCharacter,1,NumberOfCharactersWritten,0                      ;Go to a new line                jmp GetUserInput                                                                                       ;Try and get the user input again        ExitProgram:                invoke  ExitProcess,0                                                                                  ;Exit the program.end Main


It is supposed to ask the user to enter something, and if the user enterd 1 character, exit the program. If they dont it prints a message and asks for it again.

I think the problem lies in my if statements, but thats all i know.
I am using FASM and all it's windows macros if it helps
Dancho
Dancho
ok try this snipet and study it for a while,
see where you were wrong...
format PE consoleentry cmaininclude 'win32ax.inc'section '.code' code readable executablecmain:		invoke GetStdHandle,STD_INPUT_HANDLE	mov [inHandle],eax	invoke GetStdHandle,STD_OUTPUT_HANDLE	mov [outHandle],eax	;@@:	invoke ReadConsole,[inHandle],buffer,MAX_PATH,cRead,0	sub [cRead],2	.if	[cRead]=1		stdcall PrintMessage,<'Going Out Now',13,10,0>,15		jmp @f		.else			stdcall PrintMessage,<'Write Only 1 Char',13,10,0>,19			invoke RtlZeroMemory,buffer,MAX_PATH			jmp @b	.endif	@@:		invoke ExitProcess,0		proc PrintMessage uses esi edi ebx,text,len	local cWritten:DWORD				invoke WriteConsole,[outHandle],[text],[len],addr cWritten,0		xor eax,eax		ret	endpsection '.data' data readable writeable	inHandle	dd	0	outHandle	dd	0	cRead		dd	0	buffer		rb  MAX_PATH	section '.idata' import data readable	library kernel32,'kernel32.dll'		include 'api\kernel32.inc'
Lith
Lith
Hmmm, i noticed a afew things:

1) I dont understand your code much, i havnt seen most of the things you are useing. e.g "section", "@@".

2) I think i have to change "jg" to "ja". I did that and it still didnt work.

3) I realised that i am asking to read only 1 character, i set it to 10 and it "sort of worked". I was typing on the pervious line that was written and and it always says that i have to enter more characters.

I think i have to reset the "UserInput" varible for it to work, but i dont know how to do this.
Dancho
Dancho
ok,2 things...

1.) when you download latest fasm package you receive pdf document about assembly and fasm programming , study it and you will understand what is section and @@
and etc ...
2.) for windows programming APIs download some PSDK and check console func you are using here

This code is very simple and you should really understand it before going further...
Lith
Lith
I got it to work, here is my new code:
include "win32ax.inc".data        InHandle DD ?        OutHandle DD ?        Buffer1 DD ?        Buffer2 DD ?        Buffer3 DD ?        Buffer4 DD ?        Buffer5 DD ?        FailString DB "Need more characters"        EndlineCharacter DB 31.code        Main:                invoke AllocConsole                invoke GetStdHandle, STD_INPUT_HANDLE                mov [InHandle], eax                invoke GetStdHandle, STD_OUTPUT_HANDLE                mov [OutHandle], eax        GetInput:                invoke WriteConsole,[OutHandle],EndlineCharacter,1,Buffer4,0                invoke ReadConsole,[InHandle],Buffer1,10,Buffer2,0                sub [Buffer2],2                .if [Buffer2]=1                    ;The user has enterd the right ammount of characters(1)                    invoke  ExitProcess,0                .else                    invoke WriteConsole,[OutHandle],EndlineCharacter,1,Buffer5,0                    invoke WriteConsole,[OutHandle],FailString,20,Buffer5,0                    jmp Reset                .endif        Reset:                mov [Buffer1],0                mov [Buffer2],0                mov [Buffer3],0                mov [Buffer4],0                mov [Buffer5],0                jmp GetInput.end Main



I know it isnt the best way to do it(5 buffers), but it is functional :)
Also, i found out why the text was writing over itself. It was the "NewlineCharacter", i typed it wrong and made it 31 and it worked! I changed it back to 13 and the text was writing on itself.
Thanks for all your help

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.