I'm working on making a simple calculator (+,-,*,/) in assembler.
My task is to make it in the form of a graphics program.
At this stage, I created the layout. Right now I have 4 buttons and two input fields for numbers.
I am trying to do it like this:
The user enters two numbers into the text fields.
Next, you need to click on the arithmetic operation on the button.
And in the end, the result should appear somewhere on the screen.
I don't understand how to make the event handler correctly and add the appropriate action button.
.586P
.MODEL flat, STDCALL
include grafika.inc
CSstyle EQU CS_HREDRAW+CS_VREDRAW+CS_GLOBALCLASS
BSstyle EQU BS_PUSHBUTTON+WS_VISIBLE+WS_CHILD+WS_TABSTOP
WNDstyle EQU WS_CLIPCHILDREN OR WS_OVERLAPPEDWINDOW OR \
WS_HSCROLL OR WS_VSCROLL
EDTstyle EQU WS_VISIBLE+WS_CHILD+WS_TABSTOP+WS_BORDER
kolor EQU 000000FFh ; czerwony ; kolory: G B R
_DATA SEGMENT
hwnd DD 0
hinst DD 0
hdc DD 0
hbutt DD 0
hedt DD 0
hbrush DD 0
holdbrush DD 0
msg MSGSTRUCT <?>
wndc WNDCLASS <?>
naglow DB "Simple calculator",0
rozmN DD $ - naglow
ALIGN 4
puste DB "_____________________" ;
rozmP DD $ - puste
ALIGN 4
tytul DB "App",0
ALIGN 4
cname DB "MainClass", 0
ALIGN 4
MES1 DB "Lewy, myszy",0
ALIGN 4
tbutt DB "BUTTON", 0
ALIGN 4
tbutt2 DB "BUTTON", 0
ALIGN 4
tbutt3 DB "BUTTON", 0
ALIGN 4
tbutt4 DB "BUTTON", 0
ALIGN 4
tstart DB "add", 0
ALIGN 4
tstart2 DB "sub", 0
ALIGN 4
tstart3 DB "mul", 0
ALIGN 4
tstart4 DB "div", 0
ALIGN 4
tedt DB "EDIT", 0
ALIGN 4
tedt2 DB "EDIT", 0
ALIGN 4
tnazwaedt DB " ", 0
ALIGN 4
ttekst DB "text", 0
ALIGN 4
nagl DB "text", 0
ALIGN 4
terr DB "error!", 0
ALIGN 4
terr2 DB "error!", 0
ALIGN 4
bufor DB 128 dup(' ')
rbuf DD 128
znaczn DD 0
rt RECT <120,50,210,90>
rr DD 0
_DATA ENDS
_TEXT SEGMENT
WndProc PROC
; DWORD PTR [EBP+14h] - parametr LPARAM komunikatu
; DWORD PTR [EBP+10h] - parametr WPARAM komunikatu
; DWORD PTR [EBP+0Ch] - ID = identyfikator komunikatu
; DWORD PTR [EBP+08h] - HWND = deskryptor okna
;------------------------------------
push EBP
mov EBP, ESP
push EBX
push ESI
push EDI
cmp DWORD PTR [EBP+0Ch], WM_CREATE
jne @F
jmp wmCREATE
@@:
cmp DWORD PTR [EBP+0Ch], WM_DESTROY
jne @F
jmp wmDESTROY
@@:
cmp DWORD PTR [EBP+0CH], WM_COMMAND
jne @F
jmp wmCOMMAND
@@:
cmp DWORD PTR [EBP+0CH], WM_LBUTTONDOWN
jne @F
jmp wmLBUTTON
@@:
cmp DWORD PTR [EBP+0CH], WM_RBUTTONDOWN
jne @F
jmp wmRBUTTON
@@:
INVOKE DefWindowProcA, DWORD PTR [EBP+08h], \
DWORD PTR [EBP+0Ch], DWORD PTR [EBP+10h], \
DWORD PTR [EBP+14h]
jmp konWNDPROC
wmCREATE:
INVOKE CreateWindowExA, 0, OFFSET tbutt, OFFSET tstart, \
BSstyle, 10, 50, 100, 40, DWORD PTR [EBP+08h], 0, hinst, 0
mov hbutt, EAX
INVOKE CreateWindowExA, 0, OFFSET tbutt2, OFFSET tstart2, \
BSstyle, 110, 50, 100, 40, DWORD PTR [EBP+08h], 0, hinst, 0
mov hbutt, EAX
INVOKE CreateWindowExA, 0, OFFSET tbutt3, OFFSET tstart3, \
BSstyle, 210, 50, 100, 40, DWORD PTR [EBP+08h], 0, hinst, 0
mov hbutt, EAX
INVOKE CreateWindowExA, 0, OFFSET tbutt4, OFFSET tstart4, \
BSstyle, 310, 50, 100, 40, DWORD PTR [EBP+08h], 0, hinst, 0
mov hbutt, EAX
INVOKE CreateWindowExA, 0, OFFSET tedt, OFFSET tnazwaedt, EDTstyle, 110, 100, 100, 40, DWORD PTR [EBP+08h], 0, hinst, 0
mov hedt, EAX
INVOKE CreateWindowExA, 0, OFFSET tedt2, OFFSET tnazwaedt, EDTstyle, 210, 100, 100, 40, DWORD PTR [EBP+08h], 0, hinst, 0
mov hedt, EAX
INVOKE CreateSolidBrush,kolor
mov hbrush,EAX
mov EAX,0
jmp konWNDPROC
wmDESTROY:
INVOKE DeleteObject, hbrush
INVOKE PostQuitMessage, 0
mov EAX, 0
jmp konWNDPROC
wmCOMMAND:
mov EAX,hbutt
cmp EAX,DWORD PTR [EBP+14h]
je @F
jmp et1
@@:
cmp znaczn,1
je @F
mov znaczn,1
INVOKE SelectObject,hDC,hbrush
mov holdbrush,EAX
INVOKE FillRect,hDC,OFFSET rt,hbrush
mov EAX, 0
jmp konWNDPROC
@@:
mov znaczn,0
INVOKE SelectObject,hDC,holdbrush
mov hbrush,EAX
INVOKE FillRect,hDC,OFFSET rt,holdbrush
mov EAX, 0
jmp konWNDPROC
et1:
jmp konWNDPROC
wmRBUTTON:
jmp wmDESTROY
wmLBUTTON:
INVOKE SendMessageA,hedt,WM_GETTEXT,128,OFFSET bufor
mov rr,EAX
INVOKE TextOutA,hDC,110,120,OFFSET bufor,rr
mov EAX,0
jmp konWNDPROC
konWNDPROC:
pop EDI
pop ESI
pop EBX
mov ESP,EBP
pop EBP
ret 16
WndProc ENDP
main:
INVOKE GetModuleHandleA, 0
mov hinst, EAX
mov EAX, hinst
mov [wndc.clsHInstance], EAX
mov [wndc.clsStyle], CSstyle
mov [wndc.clsLpfnWndProc], OFFSET WndProc
mov [wndc.clsCbClsExtra], 0
mov [wndc.clsCbWndExtra], 0
INVOKE LoadIconA, 0, IDI_APPLICATION ; ikona
mov [wndc.clsHIcon], EAX
INVOKE LoadCursorA, 0, IDC_ARROW ; kursor
mov [wndc.clsHCursor], EAX
INVOKE GetStockObject, WHITE_BRUSH ; tło
mov [wndc.clsHbrBackground], EAX
mov [wndc.clsLpszMenuName], 0
mov DWORD PTR [wndc.clsLpszClassName], OFFSET cname
INVOKE RegisterClassA, OFFSET wndc
cmp EAX, 0
jne @F
jmp err0
@@:
INVOKE CreateWindowExA, 0, OFFSET cname, OFFSET tytul, \
WNDstyle, 50, 50, 455, 400, 0, 0, hinst, 0
cmp EAX, 0
jne @F
jmp err2
@@:
mov hwnd, EAX
INVOKE ShowWindow, hwnd, SW_SHOWNORMAL
INVOKE GetDC,hwnd
mov hdc,EAX
INVOKE lstrlenA,OFFSET naglow
mov rozmN,EAX
INVOKE TextOutA,hDC,10,20,OFFSET naglow,rozmN
INVOKE UpdateWindow, hwnd
msgloop:
INVOKE GetMessageA, OFFSET msg, 0, 0, 0
cmp EAX, 0
jne @F
jmp etkon
@@:
cmp EAX, -1
jne @F
jmp err0
@@:
INVOKE TranslateMessage, OFFSET msg
INVOKE DispatchMessageA, OFFSET msg
jmp msgloop
err0:
INVOKE MessageBoxA,0,OFFSET terr,OFFSET nagl,0
jmp etkon
err2:
INVOKE MessageBoxA,0,OFFSET terr2,OFFSET nagl,0
jmp etkon
etkon:
INVOKE ExitProcess, [msg.msWPARAM]
_TEXT ENDS
END main
A few more notes on my code:
- I am using Visual Studio 2019 compiler
- To compile the code, you need to consider the following factors:
a) For graphic assembler, you need a file in which there will be data with graphics
b) In order for the program to start in the settings, you need to set the entry point -> main
c) In the project settings, select (Build Customization -> masm(.targets, .props))