flat assembler
Message board for the users of flat assembler.

Index > Windows > Mini calculator Dialog

Author
Thread Post new topic Reply to topic
frankobach26



Joined: 10 Apr 2026
Posts: 20
Location: Kassel
frankobach26 11 Apr 2026, 16:52
Hello all. Made my First example with fASM. I have got No result. Should Pop-up a.messagebox.. thanks for Help

Code:
; Mini Calculator - 3. korrigierte Version (April 2026)
format PE GUI 4.0
entry start

include 'win32a.inc'

; ------------------- Konstanten -------------------
ID_EDIT_A   = 1011
ID_EDIT_B   = 1012

ID_ADD      = 201
ID_SUB      = 202
ID_MUL      = 203
ID_DIV      = 204

; ------------------- Code -------------------
section '.text' code readable executable

start:
    invoke  GetModuleHandle, 0
    invoke  DialogBoxParam, eax, 37, HWND_DESKTOP, DialogProc, 0
    invoke  ExitProcess, 0


proc DialogProc hwnddlg, msg, wparam, lparam
    push    ebx esi edi

    cmp     [msg], WM_COMMAND
    je      .wmcommand

    cmp     [msg], WM_CLOSE
    je      .wmclose

    xor     eax, eax
    jmp     .finish

.wmcommand:
    ; ------------------- TEST: Bei jedem Button-Klick -------------------
    ; invoke  MessageBox, [hwnddlg], "Button geklickt!", "DEBUG", MB_OK   ; <- zum Testen einkommentieren

    ; Korrekte Auswertung (empfohlene Methode aus fASM-Beispielen)
    cmp     [wparam], BN_CLICKED shl 16 + IDCANCEL
    je      .wmclose

    cmp     [wparam], BN_CLICKED shl 16 + ID_ADD
    je      .do_add
    cmp     [wparam], BN_CLICKED shl 16 + ID_SUB
    je      .do_sub
    cmp     [wparam], BN_CLICKED shl 16 + ID_MUL
    je      .do_mul
    cmp     [wparam], BN_CLICKED shl 16 + ID_DIV
    je      .do_div
    cmp     [wparam], BN_CLICKED shl 16 + IDOK
    je      .do_add                     ; Calculate Button = Standard-Addition

    ; Alternative sichere Methode (falls oben nicht greift)
    mov     eax, [wparam]
    shr     eax, 16
    cmp     ax, BN_CLICKED
    jne     .processed

    movzx   eax, word [wparam]          ; Control-ID
    cmp     eax, ID_ADD
    je      .do_add
    cmp     eax, ID_SUB
    je      .do_sub
    cmp     eax, ID_MUL
    je      .do_mul
    cmp     eax, ID_DIV
    je      .do_div
    cmp     eax, IDOK
    je      .do_add

    jmp     .processed

; ------------------- Operationen -------------------
.do_add:
    mov     byte [op], '+'
    jmp     .calculate

.do_sub:
    mov     byte [op], '-'
    jmp     .calculate

.do_mul:
    mov     byte [op], '*'
    jmp     .calculate

.do_div:
    mov     byte [op], '/'
    jmp     .calculate

.calculate:
    call    .get_values

    cmp     byte [op], '+'
    je      .op_add
    cmp     byte [op], '-'
    je      .op_sub
    cmp     byte [op], '*'
    je      .op_mul
    cmp     byte [op], '/'
    je      .op_div
    jmp     .show_result

.op_add:
    mov     eax, [a]
    add     eax, [b]
    mov     [result], eax
    jmp     .show_result

.op_sub:
    mov     eax, [a]
    sub     eax, [b]
    mov     [result], eax
    jmp     .show_result

.op_mul:
    mov     eax, [a]
    imul    eax, [b]
    mov     [result], eax
    jmp     .show_result

.op_div:
    mov     eax, [b]
    test    eax, eax
    jz      .div_by_zero

    mov     eax, [a]
    cdq
    idiv    dword [b]
    mov     [result], eax
    jmp     .show_result

.div_by_zero:
    invoke  MessageBox, [hwnddlg], "Division durch Null nicht möglich!", "Fehler", MB_OK + MB_ICONERROR
    jmp     .processed

; ------------------- Hilfsprozeduren -------------------
.get_values:
    invoke  GetDlgItemText, [hwnddlg], ID_EDIT_A, caption, 64
    lea     esi, [caption]
    call    atoi
    mov     [a], eax

    invoke  GetDlgItemText, [hwnddlg], ID_EDIT_B, message, 64
    lea     esi, [message]
    call    atoi
    mov     [b], eax
    ret

.show_result:
    lea     edi, [outbuf]               ; immer neu starten!

    mov     eax, [a]
    call    itoa

    mov     byte [edi], ' '
    inc     edi
    mov     al, [op]
    mov     [edi], al
    inc     edi
    mov     byte [edi], ' '
    inc     edi

    mov     eax, [b]
    call    itoa

    mov     byte [edi], ' '
    inc     edi
    mov     byte [edi], '='
    inc     edi
    mov     byte [edi], ' '
    inc     edi

    mov     eax, [result]
    call    itoa

    mov     byte [edi], 0               ; Null-Terminator – extrem wichtig!

    invoke  MessageBox, [hwnddlg], outbuf, title, MB_OK
    jmp     .processed

.processed:
    mov     eax, 1
    jmp     .finish

.wmclose:
    invoke  EndDialog, [hwnddlg], 0

.finish:
    pop     edi esi ebx
    ret
endp


; ------------------- atoi -------------------
atoi:
    xor     eax, eax
.next:
    mov     bl, [esi]
    cmp     bl, 0
    je      .done
    cmp     bl, '0'
    jb      .done
    cmp     bl, '9'
    ja      .done
    sub     bl, '0'
    imul    eax, eax, 10
    add     eax, ebx
    inc     esi
    jmp     .next
.done:
    ret


; ------------------- itoa (mit Vorzeichen) -------------------
itoa:
    push    ebx ecx edx

    test    eax, eax
    jns     .positive
    mov     byte [edi], '-'
    inc     edi
    neg     eax

.positive:
    mov     ecx, 10
    xor     ebx, ebx

.convert:
    xor     edx, edx
    div     ecx
    add     dl, '0'
    push    dx
    inc     ebx
    test    eax, eax
    jnz     .convert

.write:
    pop     ax
    mov     [edi], al
    inc     edi
    dec     ebx
    jnz     .write

    pop     edx ecx ebx
    ret


; ------------------- Daten -------------------
section '.data' data readable writeable
    title   db 'Mini Calculator', 0

section '.bss' readable writeable
    a       dd ?
    b       dd ?
    result  dd ?
    op      db ?

    caption rb 64
    message rb 64
    outbuf  rb 128


; ------------------- Import -------------------
section '.idata' import data readable writeable

  library kernel32, 'KERNEL32.DLL', \
          user32,   'USER32.DLL'

  import kernel32, \
         GetModuleHandle, 'GetModuleHandleA', \
         ExitProcess,     'ExitProcess'

  import user32, \
         DialogBoxParam,  'DialogBoxParamA', \
         GetDlgItemText,  'GetDlgItemTextA', \
         MessageBox,      'MessageBoxA', \
         EndDialog,       'EndDialog'


; ------------------- Dialog Resource -------------------
section '.rsrc' resource data readable

directory RT_DIALOG, dialogs

resource dialogs, \
    37, LANG_ENGLISH + SUBLANG_DEFAULT, democalc

dialog democalc, 'Mini Calculator', 70, 70, 240, 170, \
       WS_CAPTION + WS_POPUP + WS_SYSMENU + DS_MODALFRAME + DS_CENTER

    dialogitem 'STATIC', 'Zahl A:', -1, 10, 15, 80, 10, WS_VISIBLE
    dialogitem 'EDIT',   '', ID_EDIT_A, 10, 30, 200, 14, WS_VISIBLE + WS_BORDER + WS_TABSTOP + ES_NUMBER

    dialogitem 'STATIC', 'Zahl B:', -1, 10, 55, 80, 10, WS_VISIBLE
    dialogitem 'EDIT',   '', ID_EDIT_B, 10, 70, 200, 14, WS_VISIBLE + WS_BORDER + WS_TABSTOP + ES_NUMBER

    dialogitem 'BUTTON', '+', ID_ADD,  15, 100, 40, 20, WS_VISIBLE + WS_TABSTOP
    dialogitem 'BUTTON', '-', ID_SUB,  65, 100, 40, 20, WS_VISIBLE + WS_TABSTOP
    dialogitem 'BUTTON', '*', ID_MUL, 115, 100, 40, 20, WS_VISIBLE + WS_TABSTOP
    dialogitem 'BUTTON', '/', ID_DIV, 165, 100, 40, 20, WS_VISIBLE + WS_TABSTOP

    dialogitem 'BUTTON', 'Calculate', IDOK, 20, 135, 60, 20, \
               WS_VISIBLE + WS_TABSTOP + BS_DEFPUSHBUTTON

    dialogitem 'BUTTON', 'Schließen', IDCANCEL, 100, 135, 80, 20, WS_VISIBLE + WS_TABSTOP

enddialog    
Post 11 Apr 2026, 16:52
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4686
Location: vpcmpistri
bitRAKE 12 Apr 2026, 03:28
Code:
atoi:
    xor     eax, eax
.next:
    mov     bl, [esi]
    cmp     bl, 0
    je      .done
    cmp     bl, '0'
    jb      .done
    cmp     bl, '9'
    ja      .done
    sub     bl, '0'
    imul    eax, eax, 10
    add     eax, ebx
    inc     esi
    jmp     .next
.done:
    ret    
Code adds "random" high bytes of EBX.

_________________
¯\(°_o)/¯ AI understands the way 2+2 understands 4.
Post 12 Apr 2026, 03:28
View user's profile Send private message Visit poster's website Reply with quote
frankobach26



Joined: 10 Apr 2026
Posts: 20
Location: Kassel
frankobach26 14 Apr 2026, 17:46
Thanks bitrake. But I didnt need your Code for my calculator. Perhaps later. I have fixed my calculator Dialog cause in wm_command I Made a mistake. Heres my correct Version:

Code:
; 

; go dialog box, simple calculator, 14-04-2026, frankbach
; operators *,-,/,+ fixed ID_Calc to add
;
format PE GUI 4.0
entry start

include 'win32a.inc'

; IDs
ID_EDIT_A = 101
ID_EDIT_B = 102

ID_ADD = 201
ID_SUB = 202
ID_MUL = 203
ID_DIV = 204
ID_CALC = 205

section '.text' code readable executable

start:
    invoke GetModuleHandle, 0
    invoke DialogBoxParam, eax, 1, 0, DialogProc, 0
    invoke ExitProcess, 0


proc DialogProc hwnd, msg, wparam, lparam

    cmp [msg], WM_INITDIALOG
    je .init

    cmp [msg], WM_COMMAND
    je .wmcommand

    cmp [msg], WM_CLOSE
    je .close

    xor eax, eax
    ret

.init:
    mov byte [op], '+'
    mov eax, 1
    ret


.wmcommand:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    mov eax, [wparam]
    mov edx, eax
    and edx, 0FFFFh        ; ID
    shr eax, 16            ; Notification
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    cmp eax, BN_CLICKED
    jne .done

    cmp edx, ID_ADD
    je .set_add

    cmp edx, ID_SUB
    je .set_sub

    cmp edx, ID_MUL
    je .set_mul

    cmp edx, ID_DIV
    je .set_div

    cmp edx, ID_CALC
    je .calculate

    cmp edx, IDCANCEL
    je .close

    jmp .done


.set_add:
    mov byte [op], '+'
    jmp .done

.set_sub:
    mov byte [op], '-'
    jmp .done

.set_mul:
    mov byte [op], '*'
    jmp .done

.set_div:
    mov byte [op], '/'
    jmp .done


.calculate:

    ; --- A lesen ---
    invoke GetDlgItemText, [hwnd], ID_EDIT_A, bufA, 32
    lea esi, [bufA]
    call atoi
    mov [a], eax

    ; --- B lesen ---
    invoke GetDlgItemText, [hwnd], ID_EDIT_B, bufB, 32
    lea esi, [bufB]
    call atoi
    mov [b], eax

    ; --- rechnen ---
    mov eax, [a]

    cmp byte [op], '+'
    je .add

    cmp byte [op], '-'
    je .sub

    cmp byte [op], '*'
    je .mul

    cmp byte [op], '/'
    je .div

.add:
    add eax, [b]
    jmp .store

.sub:
    sub eax, [b]
    jmp .store

.mul:
    imul eax, [b]
    jmp .store

.div:
    cmp dword [b], 0
    je .divzero
    cdq
    idiv dword [b]
    jmp .store

.divzero:
    invoke MessageBox, [hwnd], "Division durch 0!", "Fehler", MB_OK
    jmp .done

.store:
    mov [res], eax

    ; --- Ausgabe ---
    lea edi, [outbuf]

    mov eax, [res]
    call itoa

    mov byte [edi], 0

    invoke MessageBox, [hwnd], outbuf, "Ergebnis", MB_OK

    jmp .done


.close:
    invoke EndDialog, [hwnd], 0

.done:
    mov eax, 1
    ret

endp


; ---------------- atoi ----------------
;atoi:
;    xor eax, eax
;.next:
;    mov bl, [esi]
;    cmp bl, 0
;    je .done
;    sub bl, '0'
;    imul eax, eax, 10
;    add eax, ebx
;    inc esi
;    jmp .next
;.done:
;    ret

atoi:
    xor     eax, eax
.next:
    mov     bl, [esi]
    cmp     bl, 0
    je      .done
    cmp     bl, '0'
    jb      .done
    cmp     bl, '9'
    ja      .done
    sub     bl, '0'
    imul    eax, eax, 10
    add     eax, ebx
    inc     esi
    jmp     .next
.done:
    ret
    ; Code adds "random" high bytes of EBX.


; ---------------- itoa ----------------
itoa:
    push ebx ecx edx

    mov ecx, 10
    xor ebx, ebx

.convert:
    xor edx, edx
    div ecx
    add dl, '0'
    push dx
    inc ebx
    test eax, eax
    jnz .convert

.write:
    pop ax
    mov [edi], al
    inc edi
    dec ebx
    jnz .write

    pop edx ecx ebx
    ret


section '.data' data readable writeable
    a dd 0
    b dd 0
    res dd 0
    op db '+'

section '.bss' readable writeable
    bufA rb 32
    bufB rb 32
    outbuf rb 64


section '.idata' import data readable writeable

library kernel32,'KERNEL32.DLL',\
        user32,'USER32.DLL'

import kernel32,\
       GetModuleHandle,'GetModuleHandleA',\
       ExitProcess,'ExitProcess'

import user32,\
       DialogBoxParam,'DialogBoxParamA',\
       GetDlgItemText,'GetDlgItemTextA',\
       MessageBox,'MessageBoxA',\
       EndDialog,'EndDialog'


section '.rsrc' resource data readable

directory RT_DIALOG, dialogs

resource dialogs,\
    1, LANG_NEUTRAL, demo

dialog demo, 'Mini Calculator', 100,100,220,160,\
       WS_CAPTION+WS_POPUP+WS_SYSMENU+DS_MODALFRAME

    dialogitem 'EDIT','',ID_EDIT_A,20,20,160,14,WS_VISIBLE+WS_BORDER
    dialogitem 'EDIT','',ID_EDIT_B,20,45,160,14,WS_VISIBLE+WS_BORDER

    dialogitem 'BUTTON','+',ID_ADD,20,70,40,20,WS_VISIBLE
    dialogitem 'BUTTON','-',ID_SUB,65,70,40,20,WS_VISIBLE
    dialogitem 'BUTTON','*',ID_MUL,110,70,40,20,WS_VISIBLE
    dialogitem 'BUTTON','/',ID_DIV,155,70,40,20,WS_VISIBLE

    dialogitem 'BUTTON','Calculate',ID_CALC,40,105,60,20,WS_VISIBLE
    dialogitem 'BUTTON','Exit',IDCANCEL,120,105,60,20,WS_VISIBLE

enddialog
 ; ends    
Post 14 Apr 2026, 17:46
View user's profile Send private message Reply with quote
MаtQuasar



Joined: 22 Jun 2026
Posts: 16
MаtQuasar 22 Jun 2026, 17:51
I also am doing a much simpler one, only add and subtract and integers allowed.
Currently it can press 0-9 digits but nothing more than that.
I was thinking of using itoa and atoi function API from ntdll.dll. But I might borrow your code in the integers conversion routine.


Description:
Filesize: 111.76 KB
Viewed: 1699 Time(s)

IMG-20260604-WA0000.jpg


Post 22 Jun 2026, 17:51
View user's profile Send private message Reply with quote
MаtQuasar



Joined: 22 Jun 2026
Posts: 16
MаtQuasar 24 Jun 2026, 12:49
frankobach26 wrote:
Thanks bitrake. But I didnt need your Code for my calculator.


If I understand correctly, bitRAKE hadn't changed your code, he merely stated that your "add eax, ebx" add random 24- bit high order bytes of ebx, because in the upper part, you only referenced it to bl register.
Post 24 Jun 2026, 12:49
View user's profile Send private message Reply with quote
frankobach26



Joined: 10 Apr 2026
Posts: 20
Location: Kassel
frankobach26 29 Nov 2026, 17:42
Here is a Mini calculator in 64-bit Modus. Hope Code is ok for you and ready for Testing .. this Code uses winapi Modus.

Code:
: mini calculator, fasm 64-bit gui , frankobach26, 29-11-2026
format PE64 GUI 6.0
entry start

include 'win64a.inc'

ID_EDIT_A = 1011
ID_EDIT_B = 1012
ID_RESULT = 1013
ID_ADD    = 201
ID_SUB    = 202
ID_MUL    = 203
ID_DIV    = 204

section '.text' code readable executable

start:
    invoke GetModuleHandleA, 0
    mov    [hInstance], rax

    sub    rsp, 28h            ; Shadow Space + Alignment
    mov    rcx, [hInstance]
    call   WinMain
    add    rsp, 28h

    invoke ExitProcess, 0


proc WinMain hInst:QWORD
    mov    [hInst], rcx        ; WICHTIG: 1. Parameter kommt in RCX, Slot selbst fuellen

    ; --- Fensterklasse fuellen ---
    mov    [wc.cbSize], sizeof.WNDCLASSEX
    mov    [wc.style], CS_HREDRAW or CS_VREDRAW
    mov    [wc.lpfnWndProc], WndProc
    mov    [wc.cbClsExtra], 0
    mov    [wc.cbWndExtra], 0

    mov    rax, [hInst]
    mov    [wc.hInstance], rax

    mov    [wc.hIcon], 0
    mov    [wc.hIconSm], 0
    mov    [wc.hbrBackground], COLOR_BTNFACE + 1
    mov    [wc.lpszMenuName], 0
    mov    [wc.lpszClassName], className

    invoke LoadCursorA, 0, IDC_ARROW
    mov    [wc.hCursor], rax

    ; --- Klasse registrieren ---
    invoke RegisterClassExA, wc
    test   rax, rax
    jnz    .class_ok

    invoke MessageBoxA, 0, errClass, windowTitle, MB_OK or MB_ICONERROR
    xor    eax, eax
    ret

.class_ok:

    ; --- Hauptfenster erzeugen ---
    invoke CreateWindowExA, 0, className, windowTitle,\
           WS_OVERLAPPEDWINDOW,\
           100, 100, 340, 260,\
           0, 0, [hInst], 0

    test   rax, rax
    jnz    .window_ok

    invoke MessageBoxA, 0, errWindow, windowTitle, MB_OK or MB_ICONERROR
    xor    eax, eax
    ret

.window_ok:
    mov    [hMain], rax

    invoke ShowWindow, [hMain], SW_SHOWNORMAL
    invoke UpdateWindow, [hMain]

  .message_loop:
    invoke GetMessageA, msg, 0, 0, 0
    cmp    eax, 0
    jle    .exit_loop

    invoke TranslateMessage, msg
    invoke DispatchMessageA, msg
    jmp    .message_loop

  .exit_loop:
    mov    rax, [msg.wParam]
    ret
endp


proc WndProc hwnd:QWORD, uMsg:QWORD, wParam:QWORD, lParam:QWORD
    mov    [hwnd], rcx         ; WICHTIG: Windows uebergibt in RCX/RDX/R8/R9
    mov    [uMsg], rdx
    mov    [wParam], r8
    mov    [lParam], r9

    cmp    dword [uMsg], WM_CREATE
    je     .wm_create

    cmp    dword [uMsg], WM_COMMAND
    je     .wm_command

    cmp    dword [uMsg], WM_DESTROY
    je     .wm_destroy

    invoke DefWindowProcA, [hwnd], [uMsg], [wParam], [lParam]
    ret

  .wm_create:
    ; Edit A
    invoke CreateWindowExA, WS_EX_CLIENTEDGE, editClass, emptyText,\
           WS_CHILD or WS_VISIBLE or WS_TABSTOP or ES_AUTOHSCROLL,\
           20, 25, 120, 25, [hwnd], ID_EDIT_A, [hInstance], 0
    mov    [hEditA], rax

    ; Edit B
    invoke CreateWindowExA, WS_EX_CLIENTEDGE, editClass, emptyText,\
           WS_CHILD or WS_VISIBLE or WS_TABSTOP or ES_AUTOHSCROLL,\
           170, 25, 120, 25, [hwnd], ID_EDIT_B, [hInstance], 0
    mov    [hEditB], rax

    ; Buttons
    invoke CreateWindowExA, 0, buttonClass, plusText,\
           WS_CHILD or WS_VISIBLE or BS_PUSHBUTTON,\
           20, 70, 55, 30, [hwnd], ID_ADD, [hInstance], 0

    invoke CreateWindowExA, 0, buttonClass, minusText,\
           WS_CHILD or WS_VISIBLE or BS_PUSHBUTTON,\
           85, 70, 55, 30, [hwnd], ID_SUB, [hInstance], 0

    invoke CreateWindowExA, 0, buttonClass, mulText,\
           WS_CHILD or WS_VISIBLE or BS_PUSHBUTTON,\
           150, 70, 55, 30, [hwnd], ID_MUL, [hInstance], 0

    invoke CreateWindowExA, 0, buttonClass, divText,\
           WS_CHILD or WS_VISIBLE or BS_PUSHBUTTON,\
           215, 70, 55, 30, [hwnd], ID_DIV, [hInstance], 0

    ; Ergebnis
    invoke CreateWindowExA, WS_EX_CLIENTEDGE, staticClass, resultText,\
           WS_CHILD or WS_VISIBLE or SS_LEFT,\
           20, 125, 270, 30, [hwnd], ID_RESULT, [hInstance], 0
    mov    [hResult], rax

    xor    eax, eax
    ret

  .wm_command:
    mov    rax, [wParam]
    and    eax, 0FFFFh

    cmp    eax, ID_ADD
    je     .do_add
    cmp    eax, ID_SUB
    je     .do_sub
    cmp    eax, ID_MUL
    je     .do_mul
    cmp    eax, ID_DIV
    je     .do_div

    xor    eax, eax
    ret

  .do_add:
    mov    dword [op], '+'
    jmp    .calculate
  .do_sub:
    mov    dword [op], '-'
    jmp    .calculate
  .do_mul:
    mov    dword [op], '*'
    jmp    .calculate
  .do_div:
    mov    dword [op], '/'
    jmp    .calculate

  .calculate:
    invoke GetWindowTextA, [hEditA], bufferA, 64
    sub    rsp, 28h            ; Shadow Space + Alignment
    lea    rcx, [bufferA]
    call   ParseInt
    add    rsp, 28h
    mov    [a], eax

    invoke GetWindowTextA, [hEditB], bufferB, 64
    sub    rsp, 28h
    lea    rcx, [bufferB]
    call   ParseInt
    add    rsp, 28h
    mov    [b], eax

    cmp    dword [op], '+'
    je     .add
    cmp    dword [op], '-'
    je     .sub
    cmp    dword [op], '*'
    je     .mul
    cmp    dword [op], '/'
    je     .div
    jmp    .done

  .add:
    mov    eax, [a]
    add    eax, [b]
    mov    [result], eax
    jmp    .show_result

  .sub:
    mov    eax, [a]
    sub    eax, [b]
    mov    [result], eax
    jmp    .show_result

  .mul:
    mov    eax, [a]
    imul   eax, [b]
    mov    [result], eax
    jmp    .show_result

  .div:
    cmp    dword [b], 0
    je     .division_error
    mov    eax, [a]
    cdq
    idiv   dword [b]
    mov    [result], eax
    jmp    .show_result

  .division_error:
    invoke MessageBoxA, [hwnd], divError, windowTitle, MB_OK or MB_ICONERROR
    jmp    .done

  .show_result:
    invoke wsprintfA, outbuf, resultFormat, [a], [op], [b], [result]
    invoke SetWindowTextA, [hResult], outbuf

  .done:
    mov    eax, 1
    ret

  .wm_destroy:
    invoke PostQuitMessage, 0
    xor    eax, eax
    ret
endp


proc ParseInt uses rbx rsi, pText:QWORD
    mov    [pText], rcx       
    mov    rsi, [pText]
    xor    eax, eax
    xor    ebx, ebx

    mov    bl, [rsi]
    cmp    bl, '-'
    jne    .positive
    inc    rsi
    mov    ebx, 1

  .positive:
    xor    eax, eax

  .next:
    movzx  ecx, byte [rsi]
    cmp    ecx, '0'
    jb     .finish
    cmp    ecx, '9'
    ja     .finish
    sub    ecx, '0'
    imul   eax, eax, 10
    add    eax, ecx
    inc    rsi
    jmp    .next

  .finish:
    test   ebx, ebx
    jz     .return
    neg    eax
  .return:
    ret
endp


section '.data' data readable writeable

    className   db 'Calc64Window', 0
    windowTitle db 'Mini Calculator x64', 0

    editClass   db 'EDIT', 0
    buttonClass db 'BUTTON', 0
    staticClass db 'STATIC', 0

    emptyText   db 0
    plusText    db '+', 0
    minusText   db '-', 0
    mulText     db '*', 0
    divText     db '/', 0
    resultText  db 'Ergebnis:', 0
    divError    db 'Division durch Null nicht moeglich!', 0

    resultFormat db '%d %c %d = %d', 0

    errClass    db 'RegisterClassExA ist fehlgeschlagen!', 0
    errWindow   db 'CreateWindowExA ist fehlgeschlagen!', 0

    hInstance   dq 0
    hMain       dq 0
    hEditA      dq 0
    hEditB      dq 0
    hResult     dq 0

    a           dd 0
    b           dd 0
    result      dd 0
    op          dd 0

    bufferA     rb 64
    bufferB     rb 64
    outbuf      rb 128


section '.bss' readable writeable
    wc  WNDCLASSEX
    msg MSG


section '.idata' import data readable writeable

    library kernel32, 'KERNEL32.DLL',\
            user32,   'USER32.DLL'

    import kernel32,\
        GetModuleHandleA, 'GetModuleHandleA',\
        ExitProcess,      'ExitProcess'

    import user32,\
        RegisterClassExA, 'RegisterClassExA',\
        CreateWindowExA,  'CreateWindowExA',\
        DefWindowProcA,   'DefWindowProcA',\
        ShowWindow,       'ShowWindow',\
        UpdateWindow,     'UpdateWindow',\
        GetMessageA,      'GetMessageA',\
        TranslateMessage, 'TranslateMessage',\
        DispatchMessageA, 'DispatchMessageA',\
        PostQuitMessage,  'PostQuitMessage',\
        GetWindowTextA,   'GetWindowTextA',\
        SetWindowTextA,   'SetWindowTextA',\
        MessageBoxA,      'MessageBoxA',\
        LoadCursorA,      'LoadCursorA',\
        wsprintfA,        'wsprintfA'
    
Post 29 Nov 2026, 17:42
View user's profile Send private message Reply with quote
Core i7



Joined: 14 Nov 2024
Posts: 191
Location: Socket on motherboard
Core i7 01 Dec 2026, 19:31
Calculator buttons are rarely used these days, and numbers are entered directly on the numeric keypad, where the operation type [+ - * /] is also selected. Here's my implementation of such a calculator, whose interface has only two fields: one for entering a number and one for the result. By default, float is selected, so both integers and fractional numbers can be calculated.

The idea is to use any of the +-*/ symbols as a number end marker, after which the arithmetic operation from the input field is applied to the value in the first field. This allows for an infinitely long sequence of operations without having to confirm each time with "Enter." To intercept the operation type, the Edit element is subclassed using the SetWindowLongPtr() function. "Enter" stops one sequence, "Space" starts a new one from a blank page, and "Esc" exits the application. This is simply a demonstration of the capabilities, not a ready-made tool, so there is no error handling.

Code:
format  pe64 gui 6.0
include 'win64ax.inc'
entry   start
;//-------
section '.data' data readable writeable

opcode    dq  0
curValue  dq  0
oldValue  dq  0
result    dq  0

hdlg      dq  0
hMain     dq  0
OldEdit   dq  0

hEcho     dq  0
hSpace    dq  0
hOpt      dq  0

buff      db    0
;//-------
section '.code' code readable executable
start:    sub     rsp,8
          invoke  GetModuleHandle,0
          invoke  DialogBoxParam, eax, 100, 0, DialogProc, 0
          invoke  ExitProcess, 0

proc  DialogProc  hwnd, msg, wparam, lparam
          mov     [hwnd],rcx
          mov     [hdlg],rcx
          mov     [msg], rdx
          mov     [wparam],r8
          mov     [lparam],r9

          cmp     rdx,WM_COMMAND
          je      @cmd
          cmp     rdx,WM_INITDIALOG
          je      @init
          xor     eax,eax
          ret

;//**************************************
@init:    invoke  GetDlgItem,[hwnd],ID_Echo
          mov     [hEcho],rax
          invoke  GetDlgItem,[hwnd],ID_Opt
          mov     [hOpt],rax

          invoke  GetDlgItem,[hwnd],ID_Space
          mov     [hSpace],rax
          invoke  EnableWindow,rax,0

          invoke  GetDlgItem,[hwnd],ID_Main
          mov     [hMain],rax
          invoke  SendMessage,rax,EM_LIMITTEXT,17,0

          invoke  SetWindowLongPtrA,[hMain],GWL_WNDPROC,EditSubclassProc
          mov     [OldEdit],rax

          xor     eax,eax
          ret

@cmd:     cmp     [wparam],BN_CLICKED shl 16 + IDCANCEL
          jne     @next
@close:   invoke  EndDialog,[hwnd],0
@next:    xor     eax,eax
          ret
endp

;//*******************************************
;//********** Sub-Classing *******************
;//*******************************************
align 8
proc EditSubclassProc  hwnd, msg, wparam, lparam
          mov     [hwnd],rcx
          mov     [msg], rdx
          mov     [wparam],r8
          mov     [lparam],r9

          cmp     rdx,WM_GETDLGCODE
          jne     @f
          mov     eax, DLGC_WANTALLKEYS
          ret

@@:       mov     rax,[wparam]
          cmp     rdx,WM_CHAR
          jne     @skip

          cmp     al,VK_RETURN
          jne     @f
          invoke  EnableWindow,[hSpace],1
          jmp     @checkOperation

@@:       cmp     al,VK_ESCAPE
          jne     @f
          invoke  EndDialog,[hdlg],0

;// Insert dot
@@:       cmp     al,'.'
          jne     @f
          invoke  SendMessage,[hMain],EM_REPLACESEL,0,<'.',0>
          xor     eax,eax
          ret

@@:       cmp     al,' '   ; Space = clear
          jne     @f
          invoke  EnableWindow,[hMain],1
          mov     [curValue],0
          mov     [oldValue],0
          mov     [opcode],0
          invoke  SetDlgItemText,[hdlg],ID_Main,<' ',0>
          invoke  SetDlgItemText,[hdlg],ID_Echo,<'0.00',0>
          invoke  EnableWindow,[hSpace],0
          xor     eax,eax
          ret
;-----------------------
@@:       cmp     al,'+'
          je      @checkOperation
          cmp     al,'-'
          je      @checkOperation
          cmp     al,'*'
          je      @checkOperation
          cmp     al,'/'
          je      @checkOperation

@skip:    invoke  CallWindowProc,[OldEdit],\
                          [hwnd],[msg],[wparam],[lparam]
          ret
;//-----------------
@checkOperation:
          cmp     [opcode],0
          jne     @f
          invoke  GetDlgItemText,[hdlg],ID_Main,buff,256
          invoke  SetDlgItemText,[hdlg],ID_Echo,buff
          invoke  SetDlgItemText,[hdlg],ID_Main,' '
          mov     rax,[wparam]
          mov     [opcode],rax
          invoke  SetDlgItemText,[hdlg],ID_Opt,opcode
          xor     eax,eax
          ret

@@:       invoke  GetDlgItemText,[hdlg],ID_Main,buff,256
         cinvoke  atof,buff
          movsd   [curValue],xmm0
          invoke  GetDlgItemText,[hdlg],ID_Echo,buff,256
         cinvoke  atof,buff
          movsd   [oldValue],xmm0
          invoke  SetDlgItemText,[hdlg],ID_Main,<' ',0>

          movsd   xmm0,[oldValue]
          movsd   xmm1,[curValue]

          cmp     [opcode],'+'
          jne     @f
          addsd   xmm0,xmm1
          movsd   [result],xmm0
          mov     rax,[wparam]
          mov     [opcode],rax
          jmp     @done

@@:       cmp     [opcode],'-'
          jne     @f
          subsd   xmm0,xmm1
          movsd   [result],xmm0
          mov     rax,[wparam]
          mov     [opcode],rax
          jmp     @done

@@:       cmp     [opcode],'*'
          jne     @f
          mulsd   xmm0,xmm1
          movsd   [result],xmm0
          mov     rax,[wparam]
          mov     [opcode],rax
          jmp     @done

@@:       divsd   xmm0,xmm1
          movsd   [result],xmm0
          mov     rax,[wparam]
          mov     [opcode],rax

@done:
          invoke  SetDlgItemText,[hdlg],ID_Opt,opcode
         cinvoke  sprintf,buff,<'%.2f',0>,[result]
          invoke  SetDlgItemText,[hdlg],ID_Echo,buff
          xor     eax,eax
          ret
endp

;//--------------------
section '.idata' import data readable writeable
library  kernel32,'kernel32.dll',user32,'user32.dll',msvcrt,'msvcrt.dll'
include  'api\kernel32.inc'
include  'api\Comctl32.inc'
include  'api\user32.inc'
import   msvcrt,atof,'atof',sprintf,'sprintf'

;//------------------
section '.rsrc' data resource readable
directory  RT_DIALOG,dialogs
resource   dialogs, 100, LANG_ENGLISH + SUBLANG_DEFAULT, mainform

dialog mainform,' Arithmetic abacus',\
                 0,0,140,080, WS_CAPTION+WS_SYSMENU+DS_CENTER,,,'Verdana',8

   dialogitem 'Edit',  '',   ID_Main, 010,036,120,009,WS_VISIBLE+ES_RIGHT+ES_NUMBER
   dialogitem 'Static','',   ID_Opt,  122,026,008,008,WS_VISIBLE
   dialogitem 'Static','0.0',ID_Echo, 010,015,120,010,WS_VISIBLE+SS_RIGHT+SS_SUNKEN

   dialogitem 'Static','',-1,000,060,140,002,WS_VISIBLE+SS_CENTER+SS_SUNKEN
   dialogitem 'Static','Press "Space" for new operation.',\
                          ID_Space,017,065,120,010,WS_VISIBLE
enddialog

ID_Main   =  1000
ID_Echo   =  1001
ID_Space  =  1002
ID_Opt    =  1003

GWL_WNDPROC  =  -4
    


Description:
Filesize: 9.71 KB
Viewed: 94 Time(s)

Pic123.jpg


Post 01 Dec 2026, 19:31
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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-2026, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.