Hi I am a newbie to Intel ASM but have done 3270 ASM for a few years now. I've dabbled with the WIN32 API (written a dialog based App in C) but decided I wanted to learn the nuts and bolts of ASM for PC's. So I am writing a simple console APP to test my skills, here is what I have so far.
.486
.MODEL flat, stdcall
OPTION CASEMAP:NONE ;Case sensitive
Include windows.inc
Include kernel32.inc
Include masm32.inc
IncludeLib kernel32.lib
IncludeLib masm32.lib
;Declare WIN32 API Functions
GetStdHandle PROTO :DWORD
SetConsoleMode PROTO :DWORD, :DWORD
ReadConsoleInput PROTO :DWORD, :DWORD, :DWORD, :DWORD
WriteFile PROTO :DWORD, :DWORD, :DWORD, :DWORD, :DWORD
ExitProcess PROTO :DWORD
.data
hConsoleOut dd 0h
hConsoleIn dd 0h
textAddr dd 0h
bytesIn dd 0h
bytesOut dd 0h
recdsIn dd 0h
msgOut db "Eureka! It works",10,0
promptIn db"Press any key to exit",10,0
msgIn db" ",0
Event dd 0h
.code
Main:
;+++++++++++++++++++++++++++++++++++++++++++
; Based on StdOut in C:\m32lib\stdout.asm +
;+++++++++++++++++++++++++++++++++++++++++++
push STD_INPUT_HANDLE ;Standard Input Handle
call GetStdHandle ;Call WIN32 API for Handle
mov hConsoleIn,eax ;Save STD_INPUT_HANDLE
lea eax,msgOut
push eax
call putOut
push ENABLE_PROCESSED_INPUT
push ENABLE_ECHO_INPUT
push ENABLE_LINE_INPUT
push hConsoleIn
call SetConsoleMode
lea eax,recdsIn
push eax
push 1
lea eax,Event
push eax
push hConsoleIn
call ReadConsoleInput
push 0 ;Add RC to parmlist
call ExitProcess ;Exit Routine
putOut :
pop textAddr ;Address of Text
push STD_OUTPUT_HANDLE ;Standard Output Handle
call GetStdHandle ;Call WIN32 API for Handle
mov hConsoleOut,eax ;Save STD_OUTPUT_HANDLE
; Ok, I need to figure out the length of the string and then call WriteFile
mov ebx,textAddr ;ebx -> textAddr
mov ecx,0 ;save start of textAddr
putOut_Count:
mov eax,[ebx]
cmp eax,0h ;Is this = 0?
je putOut_Count_exit ;Yes,
add ebx,1 ; ecx = ecx +1;
add ecx,1
jmp putOut_Count ;Top of loop
putOut_Count_exit:
sub ebx,ecx
push 0h ; NULL Parameter
lea eax,bytesOut ;eax = Addr of bytesOut
push eax ;Add to parmlist
mov eax,18 ;Move length to eax
push eax
lea eax,msgOut ;eax = Addr of msgOut
push eax ;Add to parmlist
push hConsoleOut ;Std Output Handle
call WriteFile ;Write Output
mov eax, bytesOut ;Save # bytes written
Ret
When I exec it I get:
WINCONSOLE caused an invalid page fault in
module WINCONSOLE.EXE at 015f:0040308d.
Registers:
EAX=00000012 CS=015f EIP=0040308d EFLGS=00010202
EBX=00401018 SS=0167 ESP=0063fe3c EBP=0063ff79
ECX=00403028 DS=0167 ESI=816c69b8 FS=120f
EDX=bffc9490 ES=0167 EDI=00000000 GS=0000
Bytes at CS:EIP:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Stack dump:
bff8b537 00000000 816c69b8 00530000 636e6957 6f736e6f 4500656c 00004558 436e6957 6f736e6f 7800656c 4f444e65 535c5357 0063feb0 000000b4 00000050
The one thing I found in the campus that looked similar was that this error could occur if the ExitProcess API was not being called prior to program termination. I don't think that is the case with this problem, I am pretty sure it is being invoked. Any ideas are appreciated, sorry for the length of the post, I was not sure what I should include or exclude.
Thanks in advance!
asmman,
When you call putOut, CALL pushes EIP onto the stack. In putOut you pop EIP into textAddr. When you return from putOut the return address has been popped and the return goes to the address of msgOut.
asmman,
This is equivalent to yours but it uses invoke instead of call, it also uses the masm32 StrLen function.
It exits with any key pressed.
dsouza123
; Build this with the "Project" menu using "Console Assemble and Link"
.486
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\masm32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\kernel32.lib
.data
szMsgOut db "Eureka! It works",13,10,0
szPrompt db "Press any key to exit",0
szMsgIn db 128 dup (0)
hOutPut dd 0
bWritten dd 0
sl dd 0
hInput dd 0
bRead dd 0
bLen dd 0
cCnt dd 1
.code
start:
invoke GetStdHandle,STD_OUTPUT_HANDLE
mov hOutPut, eax
invoke StrLen,ADDR szMsgOut
mov sl, eax
invoke WriteFile,hOutPut,ADDR szMsgOut,sl,ADDR bWritten,NULL
invoke StrLen,ADDR szPrompt
mov sl, eax
invoke WriteFile,hOutPut,ADDR szPrompt,sl,ADDR bWritten,NULL
invoke GetStdHandle,STD_INPUT_HANDLE
mov hInput, eax
invoke SetConsoleMode,hInput,ENABLE_PROCESSED_INPUT
invoke ReadConsole,hInput,ADDR szMsgIn,cCnt,ADDR bRead,NULL
invoke ExitProcess, 0
end start
[attachment deleted by admin]