5 Replies - 2105 Views - Last Post: 09 October 2012 - 03:06 PM

#1 Hooker  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 29
  • Joined: 17-August 12

Problem with a calculator program

Posted 07 October 2012 - 07:08 AM

I tried to code a small calculator program but its full of bugs, ie,1+1= and +1 gives 5, after inserting previous answer it inserts digits in opposite direction, someone help me.
.386 
.model flat,stdcall 
option casemap:none 

include \masm32\include\windows.inc 
include \masm32\include\user32.inc 
include \masm32\include\kernel32.inc 
include \masm32\include\comdlg32.inc 
includelib \masm32\lib\user32.lib 
includelib \masm32\lib\kernel32.lib 
includelib \masm32\lib\comdlg32.lib
DlgProc             PROTO :DWORD, :DWORD, :DWORD, :DWORD
DlgProcs		PROTO :DWORD,:DWORD,:DWORD,:DWORD
.const
IDD_DLG1						equ 1000
IDC_EDT1						equ 1091
IDC_BTN1						equ 1001
IDC_BTN2						equ 1002
IDC_BTN3						equ 1003
IDC_BTN4						equ 1004
IDC_BTN5						equ 1005
IDC_BTN6						equ 1006
IDC_BTN7						equ 1007
IDC_BTN8						equ 1008
IDC_BTN9						equ 1009
IDC_BTN0						equ 1010
IDC_BTN11						equ 1011
IDC_BTN12						equ 1012
IDC_BUTTON1092 					equ 1092


IDC_STATIC1094 equ 1094
IDC_EDIT1097 equ 1097

IDC_BTN1098 equ 1098

.data
typer1 db "1",0
typer2 db "2",0
typer3 db "3",0
typer4 db "4",0
typer5 db "5",0
typer6 db "6",0
typer7 db "7",0
typer8 db "8",0
typer9 db "9",0
typer0 db "0",0
adder db "+",0
equal db "=",0
digit dd 0
sdigit dd 0

.data?
hInstance HINSTANCE ?
hwndstatic dd ?
hwndedit dd ?

.code
start:
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke  DialogBoxParam, hInstance, 1000, 0, ADDR DlgProc, 0
invoke ExitProcess,eax

DlgProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
	.if uMsg==WM_INITDIALOG
		mov digit,0
		invoke GetDlgItem,hWnd,IDC_STATIC1094
		mov hwndstatic,eax
		invoke GetDlgItem,hWnd,IDC_EDIT1097
		mov hwndedit,eax
		invoke EnableWindow,eax,FALSE
	.elseif uMsg==WM_CLOSE
		invoke EndDialog,hWnd,NULL
	.elseif uMsg==WM_SETICON
		invoke LoadIcon,NULL,2
	.elseif uMsg==WM_COMMAND
		mov eax,wParam
		.if lParam!=0
			.if ax==IDC_BTN1
				invoke SetWindowText,hwndstatic,0
				mov eax,OFFSET typer1
				mov ebx,[eax]
				invoke SendDlgItemMessage,hWnd,IDC_EDT1,WM_CHAR,ebx,0
				invoke SendDlgItemMessage,hWnd,IDC_EDIT1097,WM_CHAR,ebx,0
			.elseif ax==IDC_BTN2
				invoke SetWindowText,hwndstatic,0
				mov eax,OFFSET typer2
				mov ebx,[eax]
				invoke SendDlgItemMessage,hWnd,IDC_EDT1,WM_CHAR,ebx,0
				invoke SendDlgItemMessage,hWnd,IDC_EDIT1097,WM_CHAR,ebx,0
			.elseif ax==IDC_BTN3
				invoke SetWindowText,hwndstatic,0
				mov eax,OFFSET typer3
				mov ebx,[eax]
				invoke SendDlgItemMessage,hWnd,IDC_EDT1,WM_CHAR,ebx,0
				invoke SendDlgItemMessage,hWnd,IDC_EDIT1097,WM_CHAR,ebx,0
			.elseif ax==IDC_BTN4
				invoke SetWindowText,hwndstatic,0
				mov eax,OFFSET typer4
				mov ebx,[eax]
				invoke SendDlgItemMessage,hWnd,IDC_EDT1,WM_CHAR,ebx,0
				invoke SendDlgItemMessage,hWnd,IDC_EDIT1097,WM_CHAR,ebx,0
			.elseif ax==IDC_BTN5
				invoke SetWindowText,hwndstatic,0
				mov eax,OFFSET typer5
				mov ebx,[eax]
				invoke SendDlgItemMessage,hWnd,IDC_EDT1,WM_CHAR,ebx,0
				invoke SendDlgItemMessage,hWnd,IDC_EDIT1097,WM_CHAR,ebx,0
			.elseif ax==IDC_BTN6
				invoke SetWindowText,hwndstatic,0
				mov eax,OFFSET typer6
				mov ebx,[eax]
				invoke SendDlgItemMessage,hWnd,IDC_EDT1,WM_CHAR,ebx,0
				invoke SendDlgItemMessage,hWnd,IDC_EDIT1097,WM_CHAR,ebx,0
			.elseif ax==IDC_BTN7
				invoke SetWindowText,hwndstatic,0
				mov eax,OFFSET typer7
				mov ebx,[eax]
				invoke SendDlgItemMessage,hWnd,IDC_EDT1,WM_CHAR,ebx,0
				invoke SendDlgItemMessage,hWnd,IDC_EDIT1097,WM_CHAR,ebx,0
			.elseif ax==IDC_BTN8
				invoke SetWindowText,hwndstatic,0
				mov eax,OFFSET typer8
				mov ebx,[eax]
				invoke SendDlgItemMessage,hWnd,IDC_EDT1,WM_CHAR,ebx,0
				invoke SendDlgItemMessage,hWnd,IDC_EDIT1097,WM_CHAR,ebx,0
			.elseif ax==IDC_BTN9
				invoke SetWindowText,hwndstatic,0
				mov eax,OFFSET typer9
				mov ebx,[eax]
				invoke SendDlgItemMessage,hWnd,IDC_EDT1,WM_CHAR,ebx,0
				invoke SendDlgItemMessage,hWnd,IDC_EDIT1097,WM_CHAR,ebx,0
			.elseif ax==IDC_BTN0			 ;process 0
				invoke SetWindowText,hwndstatic,0
				mov eax,OFFSET typer0
				mov ebx,[eax]
				invoke SendDlgItemMessage,hWnd,IDC_EDT1,WM_CHAR,ebx,0
				invoke SendDlgItemMessage,hWnd,IDC_EDIT1097,WM_CHAR,ebx,0
			.elseif ax==IDC_BTN11			 ;during addition
				invoke SetWindowText,hwndstatic,addr adder
				invoke GetDlgItemInt,hWnd,IDC_EDT1,0,1
				add digit,eax
				invoke SetDlgItemText,hWnd,IDC_EDT1,0
				xor eax,eax
				mov eax,OFFSET adder
				mov ebx,[eax]
				invoke SendDlgItemMessage,hWnd,IDC_EDIT1097,WM_CHAR,ebx,0
			.elseif ax==IDC_BTN12				;when equal to
				invoke GetDlgItemInt,hWnd,IDC_EDT1,0,1
				add digit,eax
				invoke SetDlgItemInt,hWnd,IDC_EDT1,digit,1
				invoke SetWindowText,hwndedit,0
			.elseif ax==IDC_BUTTON1092 		;when clear
				invoke SetWindowText,hwndstatic,0
				mov digit,0
				invoke SetDlgItemText,hWnd,IDC_EDT1,0
				invoke SetDlgItemText,hWnd,IDC_EDIT1097,0
			.elseif ax==IDC_BTN1098			 ;when ans
				invoke SetDlgItemInt,hWnd,IDC_EDIT1097,digit,0
			.endif
		.endif
	.endif		
	xor eax,eax	
	Ret
DlgProc endp

end start


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with a calculator program

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 876
  • View blog
  • Posts: 2,312
  • Joined: 28-March 11

Re: Problem with a calculator program

Posted 07 October 2012 - 12:29 PM

So, you want us to try and figure out what IDC_BTN1 ... IDC_BTN1098 all do? PLEASE use descriptive Constant names. BTN_ADD, BTN_SUBTRACT, BTN_KILL_PIGEON etc...

Your going to get into trouble NOT preserving the required registers in a windows callback that you use. you use ebx and do not preserve it. A window proc is called sometimes 100s of times a second, the value in ebx will not be what you think it is.
DlgProc proc uses ebx hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
would be the easiest way to do it.

mov eax,OFFSET typer8
mov ebx,[eax]
invoke SendDlgItemMessage,hWnd,IDC_EDT1,WM_CHAR,ebx,0
invoke SendDlgItemMessage,hWnd,IDC_EDIT1097,WM_CHAR,ebx,0

Your moving the address of typerX into eax and then you are moving 4 bytes of the contents at the address in eax to ebx. Is that what you really want to do? all your typerX values are single byte values.
Was This Post Helpful? 1
  • +
  • -

#3 Hooker  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 29
  • Joined: 17-August 12

Re: Problem with a calculator program

Posted 08 October 2012 - 07:02 AM

Thank you for remembering me about register preservation, and
		
	.if ax==IDC_BTN1
				invoke SetWindowText,hwndstatic,0
				mov eax,OFFSET typer1
				mov ebx,DWORD ptr [eax]
				invoke SendDlgItemMessage,hWnd,IDC_EDT1,WM_CHAR,ebx,0
				invoke SendDlgItemMessage,hWnd,IDC_EDIT1097,WM_CHAR,ebx,0
			.elseif



using BYTE ptr[eax] gives me error A2070, and using 8 bit registers also giving errors then how would i move single byte values in registers, Just give me a clue!!!
Was This Post Helpful? 0
  • +
  • -

#4 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 876
  • View blog
  • Posts: 2,312
  • Joined: 28-March 11

Re: Problem with a calculator program

Posted 08 October 2012 - 04:12 PM

What are you trying to do? Set the text of the edit control? there are easier ways of doing it. To keep with your "theme" either one of these will work:
mov     eax, offset typer1
mov     eax, [eax]
invoke  SendDlgItemMessage, hWnd,IDC_EDT1, WM_CHAR, eax, 0 ; or you could use al

mov     eax, offset typer1
movzx   ebx, byte ptr[eax]
invoke  SendDlgItemMessage, hWnd,IDC_EDT1, WM_CHAR, ebx, 0

Was This Post Helpful? 1
  • +
  • -

#5 Hooker  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 29
  • Joined: 17-August 12

Re: Problem with a calculator program

Posted 09 October 2012 - 06:08 AM

Thanks, but after clicking 'Ans' button why does insertion is happening in other direction could you just give me a hand on that.
Was This Post Helpful? 0
  • +
  • -

#6 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 876
  • View blog
  • Posts: 2,312
  • Joined: 28-March 11

Re: Problem with a calculator program

Posted 09 October 2012 - 03:06 PM

Why are you using WM_CHAR to insert text into an Edit control? You should be using WM_SETTEXT. Why is it inserting backwards, is that what you are saying? Because text gets inserted where the caret is, and the caret is probably moving for some reason.

To append text to an edit control, call this:
AppendText proc lpText:DWORD, hControl:DWORD
    invoke  SendMessage, hControl, EM_SETSEL, -1, -1
    invoke  SendMessage, hControl, EM_REPLACESEL, FALSE, lpText
    invoke  SendMessage, hControl, EM_SCROLLCARET,  0, 0    
    ret
AppendText endp

invoke  AppendText,  offset typer1, HANDLE_OF_CONTROL_TO_APPEND_TEXT

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1