flat assembler
Message board for the users of flat assembler.

Index > Windows > Writing to an Edit Control

Goto page Previous  1, 2, 3, 4  Next
Author
Thread Post new topic Reply to topic
Stancliff



Joined: 30 Jun 2024
Posts: 54
Location: Florida
Stancliff 02 Aug 2024, 01:07
I thought I was clear that my code has it's own processes that have to be outside the msg_loop, otherwise things like mouse actions will slow or stop. The screen isn't my application, it's just the I/O device. The screen won't work if the msg_loop slows down or stops. FASM may be driven by the menu, but my program is driven by the command line.
Therefor my program needs to be mostly independent of the screen program. It can have a menu but can't be particularly menu driven. I do expect to add editor features to the screen to make it more functional, but that's a different issue.

Sorry I didn't think about the code tag...
Post 02 Aug 2024, 01:07
View user's profile Send private message Reply with quote
Stancliff



Joined: 30 Jun 2024
Posts: 54
Location: Florida
Stancliff 02 Aug 2024, 15:42
AsmGuru - It has taken two days of sleeping on it to see how capturing keys can solve my issues. I am conceding that I probably don't need a separate thread. Now I get to spend a couple of days rethinking code and testing.
Post 02 Aug 2024, 15:42
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1617
Location: Toronto, Canada
AsmGuru62 02 Aug 2024, 20:32
I can help if I can understand the nature of your program.

By seeing this "my program is driven by the command line." --- I assume that you have some kind of command line interface.
So, you give commands and then something happens and as a result you have to print some output onto the screen.
I still do not think you need thread.

Here is how I would have done it.

You create a main window for the application and then create two more child controls inside of that main window:
1. Up is a larger area where you create the multi-line edit control (as MiniPad does) and where you will keep your log going.
2. Down is a smaller area (a single line Edit Control) where you type a command, which must be executed.

If you would like I can create a small prototype of this.

Now, if you have some 2nd process, which runs and puts lines into output -- that process should be on a thread, but it can't print
directly from thread -- you must have some kind of communication with main thread, where your main window is running.

Maybe you are writing a game? If you need to act on a separate keys, like ARROWS or Fx keys.
Then yes you need a thread.
But, if your commands are just text completed with <ENTER> key, then, most likely, no threads needed.
Post 02 Aug 2024, 20:32
View user's profile Send private message Send e-mail Reply with quote
Stancliff



Joined: 30 Jun 2024
Posts: 54
Location: Florida
Stancliff 02 Aug 2024, 23:13
I'm OK for now. Working through this is part of my learning. I have a clear enough concept to push through, but it changes a lot of my previous code and I have to deal with it. Your design idea is valid though I am thinking having the command lines alternate with responses creates a running log of the session that can be referred back to, copied from, or saved to disk. There shouldn't be a problem with that working out.

My biggest issue is that nearly every time I make a change to MiniPad it quits running right. I even had it running right yesterday and didn't save it soon enough. The online docs leave me unsure on what's important and what can be zeroed out or ignored. I am aware of object orientation, but never did any. My career was a few years of financial reports for a university followed by about 20 years making and maintaining a medium sized Access database. I am deep into my learning curve at the moment.
Post 02 Aug 2024, 23:13
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1617
Location: Toronto, Canada
AsmGuru62 03 Aug 2024, 01:52
Well, as I mentioned, it would be fun for me to make a prototype, which you can learn from.
It would be easy, since I have a very good IDE.
Post 03 Aug 2024, 01:52
View user's profile Send private message Send e-mail Reply with quote
Stancliff



Joined: 30 Jun 2024
Posts: 54
Location: Florida
Stancliff 03 Aug 2024, 17:09
This is based on the manual's example and sample code, but it refuses to compile. Do you see the error?
IDM_FILE = 100
IDM_NEW = 101
IDM_EXIT = 102
IDM_MSG = 110
IDM_M1 = 111
IDM_M2 = 112
IDM_HELP = 900
IDM_ABOUT = 910

menuitem '&File', IDM_FILE, MFR_POPUP
menuitem '&New', IDM_NEW
menuseparator
menuitem 'E&xit', IDM_EXIT, MFR_END
menuitem '&Msg', IDM_MSG, MFR_POPUP +MFR_END
menuitem 'Msg &1', IDM_M1
menuitem 'Msg &2', IDM_M2, MFR_END
menuitem '&Help', IDM_HELP, MFR_POPUP +MFR_END
menuitem '&About...', IDM_ABOUT, MFR_END
Post 03 Aug 2024, 17:09
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1617
Location: Toronto, Canada
AsmGuru62 03 Aug 2024, 18:43
I can't see the obvious error.
What is the error text?
Post 03 Aug 2024, 18:43
View user's profile Send private message Send e-mail Reply with quote
Stancliff



Joined: 30 Jun 2024
Posts: 54
Location: Florida
Stancliff 03 Aug 2024, 20:04
My work around is... the MFR_END on the msg 2 line is prob a mistake but it seems to work.
menu main_menu
menuitem '&File',IDM_FILE,MFR_POPUP
menuitem '&New',IDM_NEW
menuitem 'Msg &1',IDM_M1
menuitem 'Msg &2',IDM_M2 ;,MFR_END
menuseparator
menuitem 'E&xit',IDM_EXIT,MFR_END
menuitem '&Help',IDM_HELP,MFR_POPUP+MFR_END
menuitem '&About...',IDM_ABOUT,MFR_END

I had tried to make a new button for the msg's and the macro was failing while marking the menu line at the top.
something about size but none of it make sense to me. Debugging macros I don't understand is about impossible.
I just wanted buttons for File, Msg, and Help with items under them.
Post 03 Aug 2024, 20:04
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1617
Location: Toronto, Canada
AsmGuru62 03 Aug 2024, 20:50
I think your menu resource is a little bit off.
See example of full menu bar below:
Code:
menu main_menu
    menuitem '&File',0,MFR_POPUP
        menuitem '&New' _ 'Ctrl+N',IDM_NEW
        menuitem '&Open...' _ 'Ctrl+O',IDM_OPEN
        menuitem '&Save' _ 'Ctrl+S',IDM_SAVE
        menuitem 'Save &as...',IDM_SAVEAS
        menuseparator
        menuitem 'E&xit' _ 'Alt+X',IDM_EXIT,MFR_END
    menuitem '&Search',0,MFR_POPUP
        menuitem '&Position...' _ 'Ctrl+G',IDM_POSITION
        menuseparator
        menuitem '&Find...' _ 'Ctrl+F',IDM_FIND
        menuitem 'Find &next' _ 'F3',IDM_FINDNEXT
        menuitem '&Replace...' _ 'Ctrl+H',IDM_REPLACE,MFR_END
    menuitem '&Help',0,MFR_POPUP + MFR_END      ; <-- LAST POPUP has "+MFR_END"
        menuitem 'Ca&lculator...' _ 'Ctrl+F6',IDM_CALCULATOR
        menuseparator
        menuitem '&About...',IDM_ABOUT,MFR_END
    

Only one popup (the last one) can have "+MFR_END" and I think you have two.

Or, please ignore those underscores in menu text '_' --- they have special definition.
If you put them into your menus, use the definition below.
It will provide a tab character between item text and the accelerator key.
Code:
  _ equ ,09h,
    
Post 03 Aug 2024, 20:50
View user's profile Send private message Send e-mail Reply with quote
Stancliff



Joined: 30 Jun 2024
Posts: 54
Location: Florida
Stancliff 03 Aug 2024, 21:01
AsmGuru "If you are using WINDOWS application as your app --- then I think there is no waiting there. You just process the WM_CHAR messages and when <ENTER> key is pressed -- then you communicate this fact to the MiniPad."
Time to follow up on this...
I test WM_CHAR in windowProc case statement, then in the .wmchar handler I need
.wmchar:
; test char = 'enter'
; no: jmp .defwndproc
; yes:
.cr invoke SendMessage,[edithwnd],EM_GETLINECOUNT,0,0
mov [Buf], 126 ; set input limit
invoke SendMessage,[edithwnd],EM_GETLINE,eax,Buf
mov [Buf +eax],0 ; add null to string
; Test results
invoke SendMessage,[edithwnd],EM_SETSEL,-1,-1
invoke SendMessage,[edithwnd],EM_REPLACESEL,0,Buf
jmp .defwndproc

Can you tell me the key reading part at the top?
Post 03 Aug 2024, 21:01
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1617
Location: Toronto, Canada
AsmGuru62 04 Aug 2024, 12:19
The WPARAM value for the WM_CHAR message is the character code.
For <ENTER> key it will be VK_RETURN:
Code:
cmp <whatever you use as WPARAM>, VK_RETURN
je .Enter_Key_Was_Pressed
    
Post 04 Aug 2024, 12:19
View user's profile Send private message Send e-mail Reply with quote
Stancliff



Joined: 30 Jun 2024
Posts: 54
Location: Florida
Stancliff 05 Aug 2024, 16:36
I am beginning to think I can't keep my program running due to flaws in FASM, especially the 'write failed' error. I can add some features to MiniPad to make it a better editor and then it stops compiling, often for no clear reason. Most of the code for this is in examples in 'windows learn'. This is even more apparent when I try to bring my app into MiniPad to add compiler features. I get almost up to the point of reading a command line for processing and the programs stops compiling. The data defs I imported for my app should have no impact on Windows, though the registers I use possibly might. I am using Push All at the very beginning and Pop All just before the program closes. When I move the return stack for my app it might be affecting Windows, maybe. The change is in the data defs so it should be working before the program starts and use the new stack for everything. Yesterday I spent most of the afternoon wondering why the MiniPad initialization calls were failing, it came down to CreatWindowEXA aborting the entire process just before the parent was created. This is code that should not break, it is tested and proven to work. Today I will stop moving the return stack and only set up the data stack for the compiler, this might prevent one cause for crashes.
Post 05 Aug 2024, 16:36
View user's profile Send private message Reply with quote
Stancliff



Joined: 30 Jun 2024
Posts: 54
Location: Florida
Stancliff 05 Aug 2024, 16:42
Oh, yes, I used the menu def above with some mods and got it compiling but it isn't supported yet in WindowProc. The fact mine wouldn't compile before is another thing I am currently blaming on FASM. Though there is an odd exception in the last tab of a menu def I might have messed up. I am pretty sure I tried it both ways though.
Post 05 Aug 2024, 16:42
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20290
Location: In your JS exploiting you and your system
revolution 05 Aug 2024, 22:28
"write failed" is because Windows locks all executable files when they are running.

There is no "fix" for this, it is a Windows design choice.

If you run Linux then you can "delete" the file that is running, and replace it with a new one of the same name. I put delete in quotes because what really happens is the file is unlinked, but it still exists on disk because there remains an open handle to it while it is running. It will finally get really deleted when the last handle to it is closed, as would happen when the exe stops running.

There is no Windows equivalent for this, you can't delete a running executable file until it has finished running.
Post 05 Aug 2024, 22:28
View user's profile Send private message Visit poster's website Reply with quote
Stancliff



Joined: 30 Jun 2024
Posts: 54
Location: Florida
Stancliff 06 Aug 2024, 01:05
I will accept your answer, but I have tried to find a running task and usually couldn't, thanks.

AsmGuru - This is as far as I have gotten on the editor portion. In create it disappears right after CreateFont.
Code:
; Simple text editor - fasm example program
format PE GUI 4.0
entry start
include 'win32ax.inc'

macro fPush value               ; Push value to TOS (Top Of Stack)
{  add edx, -4                  ; S - 4, new cell for TOS
   mov [edx], dword value       ; move value to TOS
}
macro Msg label                 ; msg string typed to screen
{  fPush label                  ; Push adr of string to TOS
   call Type                    ; Display string on screen
}
section '.data' data readable writeable
; compute adr's for 2 stacks starting from return stack at enter
; == Return Stack =====
; R:      [ESP]                 ; Return Stack ptr, in ESP
R0        DD 0                  ; Ret Stk End, at run = initial [esp]
ToR       DD 0                  ; 1 cell above Top of R, at run =[esp]+64
; == Data Stack ======
; S:      [EDX]                 ; Data Stack ptr, in EDX
S0:       DD 128 Dup ?          ; Data Stack End, 128 cells
ToS:                            ; 1 cell above Top of S Stack
; I wanted to use EBP for the Data Stack but I hear Windows uses it

; Note: many good names from Forth are reserved by assembler, find alt's
NameSz   EQU 256                 ; max name cnt, increases dictionary size
New^     DD Reserved             ; ptr to next unused rec in Name
N^       DD 0                    ; Name Ptr, index for Name, was DP
TIB      DB 128 Dup " "          ; Term Input Buf adr, 128 b
;T^       DD 0                    ; Text Ptr to current source, was IN
;Pad      DB 128 Dup " "          ; Pad adr, 128 b
;P^       DD Pad                  ; Pad ptr (temp work area)
;H^       DD EndKern              ; ptr to current unused cell for code
;Fence    DD Reserved -17         ; adr of last protected dictionary word

; Start Dictionary with major words of kernal core
; Names have 8 char length w/trailing zeros, no cnt byte for string
; Code Adr = 4 byte, Param Adr = 4 byte, Status = 1 byte (0=IMM,1=Defer)
Name:
DB  "Cold",0,0,0,0
 DD Cold,0
 DB 1
DB  "Abort",0,0,0
 DD Abort,0
 DB 1
DB  "Quit",0,0,0,0
 DD Quit,0
 DB 1
DB  "Query",0,0,0
 DD Query,0
 DB 1
Reserved: DB ((NameSz *17) +Name -Reserved) Dup ?  ; clear Name space
; Forth searches the dictionary to find the address of a command.  My bare
; bones kernal will need close to 60 names.  The Forth standard has almost
; 140 names, and with extensions like floating point, a simple assembler,
; and various data structures this can easily go over 200.

; Windows assignments
  _class       TCHAR 'Go4th',0
  _title       TCHAR 'Go4th32',0
  _about_title TCHAR 'About Go4th32',0
  _about_text  TCHAR 'Windows based Forth Compiler',13,10,\
                     'Created with FLAT assembler.',0
  _edit        TCHAR 'EDIT',0
  _error       TCHAR 'Startup failed.',0

client        RECT               ; window box sides
ClsAtom       dd ?
editfont      dd ?
edithwnd      dd ?
hmenu         dd ?
hwnd          dd ?
msg           MSG                ; message struct for Windows

IDR_ICON   =  17
IDR_MENU   =  37
  ; Menu items
IDM_FILE   = 100
IDM_NEW    = 110
IDM_OPEN   = 120
IDM_EXIT   = 130
IDM_SVSEL  = 140
IDM_SAVE   = 150
IDM_SAVEAS = 160

IDM_EDIT   = 200
IDM_UNDO   = 210
IDM_FIND   = 220
IDM_CUT    = 230
IDM_COPY   = 240
IDM_PASTE  = 250
IDM_DEL    = 260

IDM_HELP   = 900
IDM_ABOUT  = 910

wc WNDCLASS 0,WindowProc,0,0,0,0,0,COLOR_BTNFACE+1,0,_class

section '.idata' import data readable writeable
   library    advapi32,          'ADVAPI32.DLL',\
              comctl32,          'CONCTL32.DLL',\
              comdlg32,          'COMDLG32.DLL',\
              gdi32,             'GDI32.DLL',\
              kernel32,          'KERNEL32.DLL',\
              shell32,           'SHELL32.DLL',\
              user32,            'USER32.DLL',\
              wsock32,           'WSOCK32.DLL'
   include    'api\advapi32.inc'
   include    'api\comctl32.inc'
   include    'api\comdlg32.inc'
   include    'api\gdi32.inc'
   include    'api\kernel32.inc'
   include    'api\shell32.inc'
   include    'api\user32.inc'
   include    'api\wsock32.inc'

section '.rsrc' resource data readable
directory     RT_MENU, menus,  RT_VERSION, versions,  RT_ICON, icons,\
              RT_GROUP_ICON, group_icons

resource  menus,       IDR_MENU, LANG_ENGLISH +SUBLANG_DEFAULT, main_menu
resource  versions,    1, LANG_NEUTRAL, version
resource  group_icons, IDR_ICON, LANG_NEUTRAL, main_icon
resource  icons,       1, LANG_NEUTRAL, icon_data

  versioninfo  version, VOS__WINDOWS32, VFT_APP, VFT2_UNKNOWN,\
          LANG_ENGLISH +SUBLANG_DEFAULT, 0,\
          'FileDescription', 'MiniPad - example program',\
          'LegalCopyright', 'No rights reserved.',\
          'FileVersion', '1.0',\
          'ProductVersion', '1.0',\
          'OriginalFilename', 'MINIPAD.EXE'
  icon    main_icon, icon_data, 'Go4th.ico'

menu main_menu
    menuitem '&File',IDM_FILE, MFR_POPUP
          menuitem '&New',IDM_NEW
          menuitem '&Open',IDM_OPEN
          menuitem 'E&xit',IDM_EXIT
          menuseparator
          menuitem '&SvSelect',IDM_SVSEL
          menuitem 'Sa&ve',IDM_SAVE
          menuitem 'Save &As',IDM_SAVEAS, MFR_END
    menuitem '&Edit',IDM_EDIT, MFR_POPUP
          menuitem '&Undo',IDM_UNDO
          menuitem '&Find',IDM_FIND
          menuseparator
          menuitem '&Cut',IDM_CUT
          menuitem 'Co&py',IDM_COPY
          menuitem '&Paste',IDM_PASTE
          menuitem '&Delete',IDM_DEL, MFR_END
    menuitem '&Help',IDM_HELP, MFR_POPUP + MFR_END ; LAST POPUP - MFR_END"
          menuitem '&About', IDM_ABOUT, MFR_END

section '.code' code readable executable writeable
start:    PushAD
          mov     [R0],esp      ; set old stack point to new stack start
          mov     [ToR],esp
          add     [ToR],256     ; allow 64 cells for local stack

          invoke  GetModuleHandle,0
          mov     [wc.hInstance],eax
          invoke  LoadIcon,[wc.hInstance],IDR_ICON
          mov     [wc.hIcon],eax
          invoke  LoadCursor,0,IDC_ARROW
          mov     [wc.hCursor],eax
          invoke  LoadMenu,[wc.hInstance],IDR_MENU
          mov     [hmenu],eax
          invoke  RegisterClass,wc
          mov     [ClsAtom],eax
          invoke  CreateWindowEx,0,_class,_title,WS_TILEDWINDOW+\
                  WS_VISIBLE,200,100,900,600,0,[hmenu],[wc.hInstance],0
          mov     [hwnd],eax

msg_loop: invoke  GetMessage,msg,0,0,0
          cmp     eax,1
          jb      end_loop
          je      msg_loop
          invoke  TranslateMessage,msg
          invoke  DispatchMessage,msg
          jmp     msg_loop
error:    invoke  MessageBox,NULL,_error,NULL,MB_ICONERROR+MB_OK
end_loop: invoke  ExitProcess,[msg.wParam]
          PopAD

; Add most new code here

; Cold:  full restart, reset bufs, reset N^ and New^, call Abort
Cold:     mov     eax, [Name]
          mov     [N^], eax
;          mov     eax, [Fence +17]
          mov     [New^], eax

; Abort:  reset data stack, set base, print banner, call Quit
Abort:    mov  edx, S0          ; clear data stack S
;          mov  [Base], 10

; Quit:  set term input, reset return stack, query & interp, print prompt
Quit:     mov  esp, R0          ; Begin: clear return stk R
          ret

; Query:  Move a null terminated string into Terminal Input Buffer
Query:    mov     [TIB], 123              ; set input limit
          invoke  SendMessage,[edithwnd],EM_GETLINECOUNT,0,0
          invoke  SendMessage,[edithwnd],EM_GETLINE,eax,dword[TIB]
          mov     [TIB +eax],0            ; add null to string
          invoke  SendMessage,[edithwnd],EM_REPLACESEL,0,NewLine
; Test results - Write input line back out
          invoke  SendMessage,[edithwnd],EM_SETSEL,-1,-1
          invoke  SendMessage,[edithwnd],EM_REPLACESEL,0,dword[TIB]

Banner:   DB "Go4th: The Windows Forth Compiler",13,10,0
NewLine:  DB 13,10,0
Prompt:   DB "4th?",0
proc WindowProc hwnd,wmsg,wparam,lparam
          mov     eax,[wmsg]
          cmp     eax,WM_CREATE
          je      .wmcreate
          cmp     eax,WM_SIZE
          je      .wmsize
          cmp     eax,WM_SETFOCUS
          je      .wmsetfocus
          cmp     eax,WM_CHAR
          je      .wmchar
          cmp     eax,WM_COMMAND
          je      .wmcommand
          cmp     eax,WM_DESTROY
          je      .wmdestroy
  .defwndproc:
          invoke  DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]
          jmp     .finish
  .wmcreate:
          invoke  GetClientRect,[hwnd],client
          invoke  CreateWindowEx,WS_EX_CLIENTEDGE,_edit,0,WS_VISIBLE+\
          WS_CHILD+WS_HSCROLL+WS_VSCROLL+ES_AUTOHSCROLL+ES_AUTOVSCROLL+\
          ES_MULTILINE+ES_WANTRETURN,[client.left],[client.top],\
          [client.right],[client.bottom],[hwnd],0,[wc.hInstance],NULL
          mov     [edithwnd],eax
          cmp     eax,0
          je      .failed
          invoke  CreateFont,16,0,0,0,0,FALSE,FALSE,FALSE,ANSI_CHARSET,\
          OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,\
          FIXED_PITCH+FF_MODERN,"Courier New"
invoke  MessageBox,[hwnd],"After Font",0,MB_OK
          mov     [editfont],eax
          cmp     eax,0
          je      error
;          je      .failed
          invoke  SendMessage,[edithwnd],WM_SETFONT,eax,FALSE
          invoke  SendMessage,[edithwnd],EM_SETSEL,-1,-1
          invoke  SendMessage,[edithwnd],EM_REPLACESEL,0,Banner
          invoke  SendMessage,[edithwnd],EM_SETSEL,-1,-1
          invoke  SendMessage,[edithwnd],EM_REPLACESEL,0,Prompt
          call    Cold         ; Initialize compiler
;          Msg     Banner
;          Msg     Prompt
          jmp     .finish
  .failed:
          or      eax,-1
          jmp     .finish
  .wmsize:
          invoke  GetClientRect,[hwnd],client
          invoke  MoveWindow,[edithwnd],[client.left],[client.top],\
          [client.right],[client.bottom],TRUE
          xor     eax,eax
          jmp     .finish
  .wmsetfocus:
          invoke  SetFocus,[edithwnd]
          xor     eax,eax
          jmp     .finish
  .wmchar:
          invoke  SendMessage,[edithwnd],WM_CHAR,ebx,ecx
          cmp     ebx,VK_RETURN
          jne     .defwndproc
          call    Query                  ; return a null terminated string
;          call    Interpret             ; parse and execute commands
          jmp     .defwndproc
  .wmcommand:
          mov     eax,[wparam]
          and     eax,0FFFFh
          cmp     eax, IDM_NEW
          je      .new
          cmp     eax, IDM_OPEN
          je      .open
          cmp     eax, IDM_SVSEL
          je      .svSel
          cmp     eax, IDM_SAVE
          je      .save
          cmp     eax, IDM_SAVEAS
          je      .saveAs
          cmp     eax, IDM_UNDO
          je      .undo
          cmp     eax, IDM_FIND
          je      .find
          cmp     eax, IDM_CUT
          je      .cut
          cmp     eax, IDM_COPY
          je      .copy
          cmp     eax, IDM_PASTE
          je      .paste
          cmp     eax, IDM_DEL
          je      .del
          cmp     eax, IDM_ABOUT
          je      .about
           cmp     eax, IDM_EXIT
          je      .wmdestroy
          jmp     .defwndproc

   .new:  invoke  SendMessage,[edithwnd],WM_SETTEXT,0,0
          jmp     .finish
   .open:                       ; Load file to Edit Control

          jmp     .finish
   .svSel:invoke  SendMessage,[edithwnd],EM_GETSEL,0,0
                                ; Write Selected to a File
          jmp     .finish
   .save:                       ; Write Session to a File

          jmp     .finish
   .saveAs:                     ; Write Session to a different File name

          jmp     .finish
   .undo: invoke  SendMessage,[edithwnd],EM_UNDO,0,0
          jmp     .finish
   .find:                       ; Get Search str and try to Find it in text

          jmp     .finish
   .cut:  invoke  SendMessage,[edithwnd],WM_CUT,0,0
          jmp     .finish
   .copy: invoke  SendMessage,[edithwnd],WM_COPY,0,0
          jmp     .finish
   .paste:invoke  SendMessage,[edithwnd],WM_PASTE,0,0
          jmp     .finish
   .del:  invoke  SendMessage,[edithwnd],WM_CLEAR,0,0
          jmp     .finish
  .about: invoke  MessageBox,[hwnd],_about_text,_about_title,MB_OK
          jmp     .finish
  .wmdestroy:
          invoke  DeleteObject,[editfont]
          PopAD
          invoke  PostQuitMessage,0
          xor     eax,eax
  .finish:
          ret
endp
    

Oops, made some changes. Will return to it tomorrow.


Last edited by Stancliff on 06 Aug 2024, 02:02; edited 1 time in total
Post 06 Aug 2024, 01:05
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20290
Location: In your JS exploiting you and your system
revolution 06 Aug 2024, 01:46
You can check with task manager to make sure there are not other copies of the exe still running.
Post 06 Aug 2024, 01:46
View user's profile Send private message Visit poster's website Reply with quote
Stancliff



Joined: 30 Jun 2024
Posts: 54
Location: Florida
Stancliff 06 Aug 2024, 02:03
revolution wrote:
You can check with task manager to make sure there are not other copies of the exe still running.

I did, that's why it bothers me.
Post 06 Aug 2024, 02:03
View user's profile Send private message Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 712
Ali.Z 06 Aug 2024, 02:43
Stancliff wrote:
revolution wrote:
You can check with task manager to make sure there are not other copies of the exe still running.

I did, that's why it bothers me.


make sure to close the debugee when using olly or x64 debugger, i.e. make sure no code is showing in cpu window, just press the x toolbar button or alt+f2.

_________________
Asm For Wise Humans
Post 06 Aug 2024, 02:43
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1617
Location: Toronto, Canada
AsmGuru62 06 Aug 2024, 13:52
A few issues with the posted code.

1. There is a statement to define window procedure:
Code:
proc WindowProc hwnd,wmsg,wparam,lparam
          mov     eax,[wmsg]
          cmp     eax,WM_CREATE
          je      .wmcreate
    

There is also a definition of a global variable:
Code:
client        RECT               ; window box sides
ClsAtom       dd ?
editfont      dd ?
edithwnd      dd ?
hmenu         dd ?
hwnd          dd ?  ; <---------
msg           MSG                ; message struct for Windows
    

As you can see --- it is a same name: "hwnd".
In OllyDbg I can see that inside the window procedure FASM encodes [hwnd] as the global and not as the parameter:
Code:
004051FA   > /FF75 14       PUSH    DWORD PTR SS:[EBP+14]            ; /lParam
004051FD   . |FF75 10       PUSH    DWORD PTR SS:[EBP+10]            ; |wParam
00405200   . |FF75 0C       PUSH    DWORD PTR SS:[EBP+C]             ; |Message
00405203   . |FF75 08       PUSH    DWORD PTR SS:[EBP+8]             ; |hWnd
00405206   . |FF15 2C314000 CALL    DWORD PTR DS:[<&USER32.DefWindow>; \DefWindowProcA
0040520C   . |E9 27030000   JMP     MiniPadH.00405538
00405211   > |CC            INT3                                     ; |
00405212   . |68 FC234000   PUSH    MiniPadH.004023FC                ; |hWnd = 004023FC  <------ GLOBAL!
00405217   . |FF75 08       PUSH    DWORD PTR SS:[EBP+8]             ; |                 <------ PARAMETER
0040521A   . |FF15 34314000 CALL    DWORD PTR DS:[<&USER32.GetClient>; \GetClientRect
    

It is an interesting find --- maybe FASM should warn about the case of local/parameter name matching the global name.
I never seen this before, because all my globals have a prefix "glb_" in the name: "glb_MainWindow", "glb_Allocator", etc.
I have renamed the parameter, so it will not conflict with a global name.

2. There is an issue with handling of WM_CREATE. When the parameter "hwnd" (now renamed to "hwndproc") is received, you have to
copy it into global "hwnd" right away, before you use it in the rest of the code.
Code:
  .defwndproc:
          invoke  DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]
          jmp     .finish
  .wmcreate:
          mov     eax,[hwndproc]   ; <----
          mov     [hwnd],eax       ; <----

          invoke  GetClientRect,[hwnd],client
          invoke  CreateWindowEx,WS_EX_CLIENTEDGE,_edit,0,WS_VISIBLE+\
    

3. There is a ".finish" label issue. When you process any Windows message you have to return the proper value in EAX.
Sometimes these values are ignored by Windows, but we still have to follow the contract.
In most cases the EAX = 0 to be returned from WindowProc, exception is the case of DefWindowProc.
After call to DefWindowProc just do RET to return to Windows whatever DefWindowProc returned.
I moved the XOR EAX,EAX to fix that:
Code:
  .wmdestroy:
          invoke  DeleteObject,[editfont]
          PopAD
          invoke  PostQuitMessage,0
  .finish:
          xor     eax,eax
          ret
endp
    

I am still unclear what is it your program does.
Are you communicating with any other process?
Why PUSHAD/POPAD?

P.S. Your program still does not run. OllyDbg has some kind of exception and points to some strange place. I will get back to you.
Post 06 Aug 2024, 13:52
View user's profile Send private message Send e-mail Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1617
Location: Toronto, Canada
AsmGuru62 06 Aug 2024, 16:26
For some reason FASM does not like the font name "Courier New" passed as a parameter, so I had to declare it properly:
Code:
; Windows assignments
  _class       TCHAR 'Go4th',0
  _title       TCHAR 'Go4th32',0
  _about_title TCHAR 'About Go4th32',0
  _about_text  TCHAR 'Windows based Forth Compiler',13,10,\
                     'Created with FLAT assembler.',0
  _edit        TCHAR 'EDIT',0
  _error       TCHAR 'Startup failed.',0
  _fontface    TCHAR 'Courier New',0        ; <------------
...
          invoke  CreateFont,16,0,0,0,0,FALSE,FALSE,FALSE,ANSI_CHARSET,\
          OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,\
          FIXED_PITCH+FF_MODERN,_fontface
    

But that is not what kills your program.
That below line does:
Code:
          invoke  SendMessage,[edithwnd],WM_SETFONT,eax,FALSE
          invoke  SendMessage,[edithwnd],EM_SETSEL,-1,-1
          invoke  SendMessage,[edithwnd],EM_REPLACESEL,0,Banner
          invoke  SendMessage,[edithwnd],EM_SETSEL,-1,-1
          invoke  SendMessage,[edithwnd],EM_REPLACESEL,0,Prompt

; --- THIS ONE --->  call    Cold         ; Initialize compiler

;          Msg     Banner
;          Msg     Prompt
          jmp     .finish
    

And the procedure "Cold" is defined as follows:
Code:
; Cold:  full restart, reset bufs, reset N^ and New^, call Abort
Cold:     mov     eax, [Name]
          mov     [N^], eax
;          mov     eax, [Fence +17]
          mov     [New^], eax

; Abort:  reset data stack, set base, print banner, call Quit
Abort:    mov  edx, S0          ; clear data stack S
;          mov  [Base], 10

; Quit:  set term input, reset return stack, query & interp, print prompt
Quit:     mov  esp, R0          ; Begin: clear return stk R
          ret
    

I guess this code is not yet complete, but why touch ESP?
You set ESP and return --- which is a cause of the exception.

If you are coding a compiler, and you need a stack for it -- probably, cannot use ESP directly.

When I comment the call to "Cold" --- the MiniPad window appears and it looks OK.
Post 06 Aug 2024, 16:26
View user's profile Send private message Send e-mail Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2, 3, 4  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.