Ensamblador - Acá MASM32 y el emulador8086

   
Vista:

Acá MASM32 y el emulador8086

Publicado por walter walter_100@hotmail.com (19 intervenciones) el 25/11/2011 05:44:50
Con el ASM en win32 van a poder compilar código como el que sigue.
Después de instalar ven la carpeta masm32/icztutes tiene muy buenos ejemplos.

.386
.model flat,stdcall
option casemap:none
WinMain proto :DWORD,:DWORD,:DWORD,:DWORD
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

.const
IDM_OPEN equ 1
IDM_SAVE equ 2
IDM_EXIT equ 3
MAXSIZE equ 260
MEMSIZE equ 65535

EditID equ 1

.data
ClassName db "Win32ASMEditClass",0
AppName db "Win32 ASM Edit",0
EditClass db "edit",0
MenuName db "FirstMenu",0
ofn OPENFILENAME <>
FilterString db "All Files",0,"*.*",0
db "Text Files",0,"*.txt",0,0
buffer db MAXSIZE dup(0)

.data?
hInstance HINSTANCE ?
CommandLine LPSTR ?
hwndEdit HWND ?
hFile HANDLE ?
hMemory HANDLE ?
pMemory DWORD ?
SizeReadWrite DWORD ?

.code
start:
invoke GetModuleHandle, NULL
mov hInstance,eax
invoke GetCommandLine
invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT
invoke ExitProcess,eax
WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
LOCAL wc:WNDCLASSEX
LOCAL msg:MSG
LOCAL hwnd:HWND
mov wc.cbSize,SIZEOF WNDCLASSEX
mov wc.style, CS_HREDRAW or CS_VREDRAW
mov wc.lpfnWndProc, OFFSET WndProc
mov wc.cbClsExtra,NULL
mov wc.cbWndExtra,NULL
push hInst
pop wc.hInstance
mov wc.hbrBackground,COLOR_WINDOW+1
mov wc.lpszMenuName,OFFSET MenuName
mov wc.lpszClassName,OFFSET ClassName
invoke LoadIcon,NULL,IDI_APPLICATION
mov wc.hIcon,eax
mov wc.hIconSm,eax
invoke LoadCursor,NULL,IDC_ARROW
mov wc.hCursor,eax
invoke RegisterClassEx, addr wc
INVOKE CreateWindowEx,WS_EX_CLIENTEDGE,ADDR ClassName,ADDR AppName,\
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,\
CW_USEDEFAULT,300,200,NULL,NULL,\
hInst,NULL
mov hwnd,eax
INVOKE ShowWindow, hwnd,SW_SHOWNORMAL
INVOKE UpdateWindow, hwnd
.WHILE TRUE
INVOKE GetMessage, ADDR msg,NULL,0,0
.BREAK .IF (!eax)
INVOKE TranslateMessage, ADDR msg
INVOKE DispatchMessage, ADDR msg
.ENDW
mov eax,msg.wParam
ret
WinMain endp
WndProc proc uses ebx hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
.IF uMsg==WM_CREATE
INVOKE CreateWindowEx,NULL,ADDR EditClass,NULL,\
WS_VISIBLE or WS_CHILD or ES_LEFT or ES_MULTILINE or\
ES_AUTOHSCROLL or ES_AUTOVSCROLL,0,\
0,0,0,hWnd,EditID,\
hInstance,NULL
mov hwndEdit,eax
invoke SetFocus,hwndEdit
mov ofn.lStructSize,SIZEOF ofn
push hWnd
pop ofn.hWndOwner
push hInstance
pop ofn.hInstance
mov ofn.lpstrFilter, OFFSET FilterString
mov ofn.lpstrFile, OFFSET buffer
mov ofn.nMaxFile,MAXSIZE
.ELSEIF uMsg==WM_SIZE
mov eax,lParam
mov edx,eax
shr edx,16
and eax,0ffffh
invoke MoveWindow,hwndEdit,0,0,eax,edx,TRUE
.ELSEIF uMsg==WM_DESTROY
invoke PostQuitMessage,NULL
.ELSEIF uMsg==WM_COMMAND
mov eax,wParam
.if lParam==0
.if ax==IDM_OPEN
mov ofn.Flags, OFN_FILEMUSTEXIST or \
OFN_PATHMUSTEXIST or OFN_LONGNAMES or\
OFN_EXPLORER or OFN_HIDEREADONLY
invoke GetOpenFileName, ADDR ofn
.if eax==TRUE
invoke CreateFile,ADDR buffer,\
GENERIC_READ or GENERIC_WRITE ,\
FILE_SHARE_READ or FILE_SHARE_WRITE,\
NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,\
NULL
mov hFile,eax
invoke GlobalAlloc,GMEM_MOVEABLE or GMEM_ZEROINIT,MEMSIZE
mov hMemory,eax
invoke GlobalLock,hMemory
mov pMemory,eax
invoke ReadFile,hFile,pMemory,MEMSIZE-1,ADDR SizeReadWrite,NULL
invoke SendMessage,hwndEdit,WM_SETTEXT,NULL,pMemory
invoke CloseHandle,hFile
invoke GlobalUnlock,pMemory
invoke GlobalFree,hMemory
.endif
invoke SetFocus,hwndEdit
.elseif ax==IDM_SAVE
mov ofn.Flags,OFN_LONGNAMES or\
OFN_EXPLORER or OFN_HIDEREADONLY
invoke GetSaveFileName, ADDR ofn
.if eax==TRUE
invoke CreateFile,ADDR buffer,\
GENERIC_READ or GENERIC_WRITE ,\
FILE_SHARE_READ or FILE_SHARE_WRITE,\
NULL,CREATE_NEW,FILE_ATTRIBUTE_ARCHIVE,\
NULL
mov hFile,eax
invoke GlobalAlloc,GMEM_MOVEABLE or GMEM_ZEROINIT,MEMSIZE
mov hMemory,eax
invoke GlobalLock,hMemory
mov pMemory,eax
invoke SendMessage,hwndEdit,WM_GETTEXT,MEMSIZE-1,pMemory
invoke WriteFile,hFile,pMemory,eax,ADDR SizeReadWrite,NULL
invoke CloseHandle,hFile
invoke GlobalUnlock,pMemory
invoke GlobalFree,hMemory
.endif
invoke SetFocus,hwndEdit
.else
invoke DestroyWindow, hWnd
.endif
.endif
.ELSE
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.ENDIF
xor eax,eax
ret
WndProc endp
end start


Con el emulador8086 van a poder compilar código del tipo que sigue a continuación.


; this example demonstrates the input and output string functions of emu8086.inc
; this code doesnt depend on MS-DOS or any other operating system's interrupts, therefore
; it can easily be used for developing an operating system or a boot record loader.

name "string"

; this macro prints a string that is given as a parameter, example:
; print 'hello world!'
print macro sdat
local next_char, s_dcl, printed, skip_dcl
push ax ; store registers...
push si ;
jmp skip_dcl ; skip declaration.
s_dcl db sdat, 0
skip_dcl:
lea si, s_dcl
next_char:
mov al, cs:[si]
cmp al, 0
jz printed
inc si
mov ah, 0eh ; teletype function.
int 10h
jmp next_char
printed:
pop si ; re-store registers...
pop ax ;
print endm


; this macro prints a string that is given as a parameter, example:
; printn 'hello world!'
; the same as print, but new line is automatically added.
printn macro sdat
local next_char, s_dcl, printed, skip_dcl
push ax ; store registers...
push si ;
jmp skip_dcl ; skip declaration.
s_dcl db sdat, 0Dh, 10, 0
skip_dcl:
lea si, s_dcl
next_char:
mov al, cs:[si]
cmp al, 0
jz printed
inc si
mov ah, 0eh ; teletype function.
int 10h
jmp next_char
printed:
pop si ; re-store registers...
pop ax ;
printn endm


; this macro prints a char in al and advances
; the current cursor position:
putc macro char
push ax
mov al, char
mov ah, 0eh
int 10h
pop ax
putc endm


org 100h

jmp start ; skip over the declarations and data

buffer db "empty buffer --- empty buffer"
size = $ - offset buffer ; declare constant
msg1 db "Enter a string: ", 0

start:
; print a welcome message:
lea si, msg1
call print_string

; get string to ds:di
lea di, buffer ; buffer offset.
mov dx, size ; buffer size.
call get_string

putc 0Dh
putc 10 ; next line.

; print using macro:
print "You've entered: "

; print string in ds:si using procedure:
mov si, di
call print_string

; wait for any key...
mov ax, 0
int 16h

ret

; get a null terminated string from keyboard,
; write it to buffer at ds:di, maximum buffer size is set in dx.
; 'enter' stops the input.
get_string proc near
push ax
push cx
push di
push dx

mov cx, 0 ; char counter.

cmp dx, 1 ; buffer too small?
jbe empty_buffer ;

dec dx ; reserve space for last zero.


;============================
; eternal loop to get
; and processes key presses:

wait_for_key:

mov ah, 0 ; get pressed key.
int 16h

cmp al, 0Dh ; 'return' pressed?
jz exit


cmp al, 8 ; 'backspace' pressed?
jne add_to_buffer
jcxz wait_for_key ; nothing to remove!
dec cx
dec di
putc 8 ; backspace.
putc ' ' ; clear position.
putc 8 ; backspace again.
jmp wait_for_key

add_to_buffer:

cmp cx, dx ; buffer is full?
jae wait_for_key ; if so wait for 'backspace' or 'return'...

mov [di], al
inc di
inc cx

; print the key:
mov ah, 0eh
int 10h

jmp wait_for_key
;============================

exit:

; terminate by null:
mov [di], 0

empty_buffer:

pop dx
pop di
pop cx
pop ax
ret
get_string endp

; print null terminated string at current cursor position,
; raddress of string in ds:si
print_string proc near
push ax ; store registers...
push si ;

next_char:
mov al, [si]
cmp al, 0
jz printed
inc si
mov ah, 0eh ; teletype function.
int 10h
jmp next_char
printed:

pop si ; re-store registers...
pop ax ;

ret
print_string endp

Espero les sirva a mi me sirvio mucho. Saludos Walter
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Va el link MASM32 Y emulador8086

Publicado por walter (19 intervenciones) el 25/11/2011 05:51:09
Les dejo el link que se me paso de ponerlo en el post anterior.

https://skydrive.live.com/redir.aspx?cid=a4d319ad082ba34d&resid=A4D319AD082BA34D!109

Saludos Walter
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar