Jump to content
  • Advertisement
Sign in to follow this  

Assembly Language Problem

This topic is 2948 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

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

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

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

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

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

Share this post

Link to post
Share on other sites
ok try this snipet and study it for a while,
see where you were wrong...

format PE console
entry cmain
include 'win32ax.inc'

section '.code' code readable executable

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
stdcall PrintMessage,<'Write Only 1 Char',13,10,0>,19
invoke RtlZeroMemory,buffer,MAX_PATH
jmp @b

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

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

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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...

Share this post

Link to post
Share on other sites
I got it to work, here is my new code:

include "win32ax.inc"

InHandle DD ?
OutHandle DD ?
Buffer1 DD ?
Buffer2 DD ?
Buffer3 DD ?
Buffer4 DD ?
Buffer5 DD ?
FailString DB "Need more characters"
EndlineCharacter DB 31

invoke AllocConsole
invoke GetStdHandle, STD_INPUT_HANDLE
mov [InHandle], eax
invoke GetStdHandle, STD_OUTPUT_HANDLE
mov [OutHandle], eax

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
invoke WriteConsole,[OutHandle],EndlineCharacter,1,Buffer5,0
invoke WriteConsole,[OutHandle],FailString,20,Buffer5,0
jmp 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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!