News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

MASM example code needed

Started by jayanthd, March 15, 2013, 08:13:46 PM

Previous topic - Next topic

jayanthd

Please see this link http://www.winasm.net/forum/index.php?showtopic=4139


I am doing a project using WinASM and ml.exe assembler. I now this forum is for MASM32. Registration at MASM forum is disabled, so I am asking the question here. I need a MASM code for the application (GUI) shown in the above link. The code should take string input like "12345" from the first textbox and then convert it to integer like 12345 and then do two operations like number + 12 and number * 12 and convert the results to strings and display it in the other two textboxes. This should happen when Generate button is clicked. When Exit button is clicked the program should exit to windows. Also mention how to change the caption of a label through code.

sinsi

QuoteThe code should take string input like "12345" from the first textbox and then convert it to integer like 12345
Use something like atodw and store the value
Quoteand then do two operations like number + 12 and number * 12
value+12, store it, value*12, store it
Quoteand convert the results to strings and display it in the other two textboxes.
Use dwtoa on the two stored values, use SetWindowText to display them
QuoteThis should happen when Generate button is clicked.
Handle WM_COMMAND in your wndproc
QuoteWhen Exit button is clicked the program should exit to windows.
Handle WM_COMMAND and send WM_CLOSE
QuoteAlso mention how to change the caption of a label through code.
SetWindowText

Now knock some code together for us to critique  :biggrin: no homework handouts here.

dedndave

Quote from: jayanthd on March 15, 2013, 08:13:46 PM
Registration at MASM forum is disabled....

this is the "new" masm32 forum
the one that is disabled is the "old" masm32 forum, which is us   :biggrin:
it is left as is, so that the information there may be accessed using the forum search tool

it helps to break the project into seperate pieces
get each piece to work, and add the pieces together

as for the edit boxes, there are a number of examples in the masm32\examples folder
you can also find many in the (old and new) forum contents by using the search tool
by browsing the forum, you can see what problems others have had, and how they were overcome

you can create buttons and edit boxes by using statements in a resource file or by using CreateWindowEx
i prefer the latter but, for beginners, the resource file may be simpler

the math part (adding and multiplying) is pretty simple x86 code
you can also find examples of these using the search tool, especially multiply (add is easy)

so, what is left is the type conversion part - converting between binary and ASCII decimal formats
this is generally a two-part process - ASCII/binary conversion and base conversion
as Sinsi mentioned, there are pre-written routines and macros in the masm32 package
you can even look in masm32\macros\macros.asm and the masm32\m32lib folder to see how they work
and, again, the forum search tool will find you other examples of type conversion functions

jayanthd

Quote from: sinsi on March 15, 2013, 09:54:55 PM
QuoteThe code should take string input like "12345" from the first textbox and then convert it to integer like 12345
Use something like atodw and store the value
Quoteand then do two operations like number + 12 and number * 12
value+12, store it, value*12, store it
Quoteand convert the results to strings and display it in the other two textboxes.
Use dwtoa on the two stored values, use SetWindowText to display them
QuoteThis should happen when Generate button is clicked.
Handle WM_COMMAND in your wndproc
QuoteWhen Exit button is clicked the program should exit to windows.
Handle WM_COMMAND and send WM_CLOSE
QuoteAlso mention how to change the caption of a label through code.
SetWindowText

Now knock some code together for us to critique  :biggrin: no homework handouts here.


How to get Text from textbox? Should I use GetDlgItemText API? Can you show an example code of using GetDlgItemText(0 and SetWindowText()? I have win32API help file but GetWindowText, GetWindowsTextA is not mentioned in it.

dedndave

it's an "edit control" - often called an "edit box"

http://msdn.microsoft.com/en-us/library/windows/desktop/bb775458%28v=vs.85%29.aspx

on the left of that page, you will see sub-sections
after you have read the description pages, the reference pages will help as a guide
you are interested in the functions and style constants, of course
but, also, much of the work is done with the messages and notifications
we often control the box by using SendMessage to send messages to the edit control
and we often get feedback by writing code to handle the notifications sent to the parent window

dedndave

here is a simple example to get you started.....

masm32\examples\dialogs_later\gettext

type some text into the edit control, then press enter

dedndave

buttons are another form of windows controls

http://msdn.microsoft.com/en-us/library/windows/desktop/bb773169%28v=vs.85%29.aspx

i have been working my way through the list, trying each control type
some are more complex than others,
but understanding the documentation gets a little easier with practice   :P

buttons are some of the simpler control types to learn
until you want to do something special - lol

hutch--

What you need to do when you create a control like an edit control is get its handle, if it is created using CreateWindowEx() then you use its return value as the handle, if its created as a dialog you use the dialog control ID and GetDlgItem() to get its handle. A window handle which is what an edit control produces is a unique ID number within the system which you can then use as a target ID to both get and set text using either Windows messages or Windows API functions.

Now normally you use a GLOBAL value set in the uninitialised data section (.data?) which has global scope and when you get either the return value OR the dialog ID control handle, you copy it into the global variable.



.data?
    hEditControl dd ?
    ........................

.code
    ........................

    mov hEditControl, eax    ; from CreateWindowExd()

    or

    invoke GetDlgItem, hDlg, IDNUM    ; from dialog
    mov hEditControl, eax

jayanthd

Quote from: dedndave on March 15, 2013, 11:05:14 PM
it's an "edit control" - often called an "edit box"

http://msdn.microsoft.com/en-us/library/windows/desktop/bb775458%28v=vs.85%29.aspx



Can you give an example code for Edit_GetText and Edit_SetText?


See image

jayanthd

Quote from: hutch-- on March 16, 2013, 12:17:40 AM
What you need to do when you create a control like an edit control is get its handle, if it is created using CreateWindowEx() then you use its return value as the handle, if its created as a dialog you use the dialog control ID and GetDlgItem() to get its handle. A window handle which is what an edit control produces is a unique ID number within the system which you can then use as a target ID to both get and set text using either Windows messages or Windows API functions.

Now normally you use a GLOBAL value set in the uninitialised data section (.data?) which has global scope and when you get either the return value OR the dialog ID control handle, you copy it into the global variable.



.data?
    hEditControl dd ?
    ........................

.code
    ........................

    mov hEditControl, eax    ; from CreateWindowExd()

    or

    invoke GetDlgItem, hDlg, IDNUM    ; from dialog
    mov hEditControl, eax


@hutch

This is the code of SDI.asm of my SDI project.




.386


.MODEL FLAT,STDCALL


OPTION CASEMAP:NONE


WinMain PROTO :DWORD,:DWORD,:DWORD,:DWORD


Include SDI.inc


.CODE
Start:
   Invoke GetModuleHandle, NULL
   MOV hInstance,EAX
   Invoke GetCommandLine
   MOV CommandLine, EAX
   Invoke WinMain, hInstance, NULL, CommandLine, SW_SHOWDEFAULT
   Invoke ExitProcess,EAX
   
WinMain Proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
Local wc:WNDCLASSEX
Local msg:MSG
Local hwnd:HWND


   MOV wc.cbSize, SizeOf WNDCLASSEX
   MOV wc.style, CS_HREDRAW or CS_VREDRAW
   MOV wc.lpfnWndProc, Offset WndProc
   MOV wc.cbClsExtra,NULL
   MOV wc.cbWndExtra,NULL
   PUSH hInst
   POP wc.hInstance
   MOV wc.hbrBackground, COLOR_WINDOW+1
   MOV wc.lpszMenuName, NULL
   MOV wc.lpszClassName, Offset ClassName
   Invoke LoadIcon, NULL, IDI_APPLICATION
   MOV wc.hIcon,EAX
   MOV wc.hIconSm,0
   Invoke LoadCursor, NULL, IDC_ARROW
   MOV wc.hCursor,EAX
   Invoke RegisterClassEx, addr wc
   Invoke LoadMenu, hInst, Offset MenuName
   MOV hMenu,EAX
   Invoke CreateWindowEx, WS_EX_CLIENTEDGE, ADDR ClassName, ADDR szAppName,\
           WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,\
           CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, hMenu,\
           hInst, NULL
   MOV hwnd,EAX
   Invoke ShowWindow, hwnd, SW_SHOWNORMAL
   Invoke UpdateWindow, hwnd
   .While TRUE
      Invoke GetMessage, ADDR msg, NULL, 0, 0
      .Break .If (!EAX)
      Invoke TranslateMessage, ADDR msg
      Invoke DispatchMessage, ADDR msg
   .EndW
   MOV EAX, msg.wParam
   RET
WinMain endp
WndProc Proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
   .If uMsg==WM_DESTROY
      Invoke PostQuitMessage, NULL
   .ElseIf uMsg==WM_COMMAND
      MOV EAX,wParam
      .If AX==IDM_FILE_NEW
         Invoke MessageBox, NULL, ADDR szFileNew, Offset szAppName, MB_OK
      .ElseIf AX==IDM_FILE_OPEN
         Invoke MessageBox, NULL, ADDR szFileOpen, Offset szAppName, MB_OK
      .ElseIf AX==IDM_HELP_ABOUT
         Invoke MessageBox, NULL,ADDR szHelpAbout, Offset szAppName, MB_OK
      .Else
         Invoke DestroyWindow, hWnd
      .EndIf
   .Else
      Invoke DefWindowProc, hWnd, uMsg, wParam, lParam
      RET
   .EndIf
   XOR EAX,EAX
   RET
WndProc EndP


End Start



This is the code of SDI.inc


Include windows.inc
Include user32.inc
Include kernel32.inc
;-------------------------------------------------------------------
;Needed For Debug Window Only
;Include masm32.inc
;Include debug.inc
;-------------------------------------------------------------------
IncludeLib user32.lib
IncludeLib kernel32.lib
;-------------------------------------------------------------------
;Needed For Debug Window Only
;IncludeLib masm32.lib
;IncludeLib debug.lib


.DATA
ClassName      DB "MainWinClass",0
szAppName      DB "SDI Application",0
MenuName      DB "FirstMenu",0
szFileNew      DB "You selected 'New'.",0
szFileOpen      DB "You selected 'Open'.",0
szHelpAbout      DB "You selected 'About'.",0


.DATA?
hInstance      HINSTANCE ?
CommandLine      LPSTR ?
hMenu         HMENU ?


.CONST
IDM_FILE_NEW   EQU 1
IDM_FILE_OPEN   EQU 2
IDM_HELP_ABOUT   EQU 3
IDM_FILE_EXIT   EQU 4





I have included SDI.inc in main.asm file.




dedndave

Quote from: jayanthd on March 16, 2013, 12:48:26 AM
Can you give an example code for Edit_GetText and Edit_SetText?


you've got text all over the place, there   :P
that is a much more complex set of controls than what you described
i am guessing that window was created with C++, using a set of advanced controls from .NET

i think you are refering to the "title" text that might appear above an edit control
that is usually done with a "static control", probably the simplest of all the windows controls

http://msdn.microsoft.com/en-us/library/windows/desktop/bb760769%28v=vs.85%29.aspx


TouEnMasm


There is a lazy method.Made a search on the masm sample and you find :
masm32\examples\exampl01\resdlg
Who need just littles modifies.
Fa is a musical note to play with CL

jayanthd

Quote from: dedndave on March 16, 2013, 01:36:31 AM
Quote from: jayanthd on March 16, 2013, 12:48:26 AM
Can you give an example code for Edit_GetText and Edit_SetText?

you've got text all over the place, there   :P
that is a much more complex set of controls than what you described
i am guessing that window was created with C++, using a set of advanced controls from .NET

i think you are refering to the "title" text that might appear above an edit control
that is usually done with a "static control", probably the simplest of all the windows controls

http://msdn.microsoft.com/en-us/library/windows/desktop/bb760769%28v=vs.85%29.aspx


I have three edit boxes and 3 labels which give info about the edit boxes and two buttons. The user enters 123456 into one edit box and when generate button is clicked integer value of the string in edit box should be obtained and value + 12 and value * 12 must be performed and the two results should be converted to string and displayed in the other two edit boxes. That is all I need. I used WinASM because it is easy to create GUI.

dedndave

it probably creates the controls in the resource file (.RC)



you want:
(4) static text controls, to show the name of each edit control and "Calc"
(3) edit controls, to show the numbers
(2) button controls

the results could be displayed in simple static text windows
so all you really need is one edit control
but, you can also use an edit control to display results

you could also eliminate the buttons
when they are in the edit control, and press enter, the results can be updated (no Generate button)
the Exit function is taken care of with the close box in the title bar

jayanthd

Quote from: dedndave on March 16, 2013, 01:54:54 AM
it probably creates the controls in the resource file (.RC)



you want:
(4) static text controls, to show the name of each edit control and "Calc"
(3) edit controls, to show the numbers
(2) button controls

the results could be displayed in simple static text windows
so all you really need is one edit control
but, you can also use an edit control to display results


You got it right dedndave. I need the code to set text to Label (Static Text) or Edit box, but if code is given for both it will be helpful. See attached file. If you have WinASM compile it and see. It is not showing the GUI I created it shows some other GUI. I asked the question in WinASM forum but nobody answers.