16/32bit hello world

Continuation of the MASM book. Built 16/32bit exe.

hello16.asm

.286
.model small

end_process macro ret_value
            mov   al, ret_value
            mov   ah, 4ch
            int   21h
            endm

display     macro string
            mov   dx, offset string
            mov   ah, 09h
            int   21h
            endm

; AL <- keycode
read_kbd    macro
            mov   ah, 08h
            int   21h
            endm

.data
MSG         db 'Hello 16bit world.', 0dh, 0ah, '$'
MSG2        db '(hit any key).', 0dh, 0ah, '$'
MSGKEY      db '" " typed.', 0dh, 0ah, '$'

.code
START:
            mov   ax, _DATA
            mov   ds, ax

            display     MSG   ; Display message
            display     MSG2  ; Display message
            read_kbd          ; Wait for input

            mov [MSGKEY+1], AL
            mov BX, offset MSGKEY
            
            display     MSGKEY   ; Display
            end_process 0     ; exit


end START ; end

hello32.asm

; for pentium
.586
.model flat, stdcall
; ExitProcess != exitprocess with the option
option casemap:none

NULL            EQU 0
MB_OK           EQU 0
NUM             EQU 1

wsprintfA       proto c :dword, :dword, :dword, :dword, :dword
MessageBoxA     proto :dword, :dword, :dword, :dword
ExitProcess     proto :dword

.data

TITLE0          DB 'Assembler Test', 0
MESSAGE         DB 'Hello 32bit world!', 0
BUFFER          DB 64 DUP(0)

.code
WinMainCRTStartup   proc
    mov eax, NUM
    mov ecx, 10

MYLOOP:
    mov ebx, eax
    add eax, ebx
    dec ecx
    jnz MYLOOP

    ; offset -> address of the variable
    invoke MessageBoxA, NULL, offset MESSAGE, offset TITLE0, MB_OK
    invoke ExitProcess, 0

    ret
WinMainCRTStartup   endp
end

makefile

HELLO16 = hello16.exe
HELLO32 = hello32.exe

HELLO16OBJS = hello16.obj
HELLO32OBJS = hello32.obj

LDFLAGS = /LIBPATH:"C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib" /LIBPATH:"C:\Program Files\Microsoft Visual Studio 10.0\VC\lib"

hello16 : $(HELLO16OBJS)
	link16 $(HELLO16OBJS),$(HELLO16);

hello32 : $(HELLO32OBJS)
	link /NOLOGO /SUBSYSTEM:WINDOWS $(HELLO32OBJS) $(LDFLAGS) kernel32.lib user32.lib /OUT:$(HELLO32)
	
clean:
	del /f *.obj *.o *.exe *.lst

.SUFFIXES: .a16 .asm .obj

.a16.obj: 
	ml /c /Fl $<

.asm.obj: 
	ml /c /coff /Cp /nologo /Fl $< /Zi

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.