Skip to content

Instantly share code, notes, and snippets.

@yan12125
Last active December 11, 2015 07:18
Show Gist options
  • Save yan12125/4564837 to your computer and use it in GitHub Desktop.
Save yan12125/4564837 to your computer and use it in GitHub Desktop.
Win32 Assembly example
ml /c winapi.asm
link winapi.asm /subsystem:console
.386
.model flat, stdcall
COORD STRUC
X WORD ?
Y WORD ?
COORD ENDS
GetStdHandle PROTO STDCALL :DWORD
SetConsoleTextAttribute PROTO STDCALL :DWORD,:DWORD
SetConsoleCursorPosition PROTO STDCALL :DWORD,:COORD
CloseHandle PROTO STDCALL :DWORD
GetTickCount PROTO STDCALL
ExitProcess PROTO STDCALL :DWORD
printf PROTO C :VARARG
system PROTO C :DWORD
rand PROTO C
time PROTO C :DWORD
srand PROTO C :DWORD
memset PROTO C :DWORD,:DWORD,:DWORD
_getch PROTO C
; my own functions
printGrid PROTO C n:DWORD
getArrowKey PROTO C
moveCursor PROTO C X:WORD,Y:WORD
includelib kernel32.lib
includelib msvcrt.lib
.data
format1 db "%d",10,0
format2 db "%c",0
newline db 10,0
cmd_pause db "pause",0
cmd_cls db "cls",0
hStdOut DWORD ?
STD_OUTPUT_HANDLE equ -11
BOARD_SIZE equ 16
last_char BYTE ?
dir BYTE ?
.code
main PROC
LOCAL curX:WORD,curY:WORD,oldX:WORD,oldY:WORD
invoke system,offset cmd_cls
invoke GetTickCount
invoke srand,eax
invoke GetStdHandle, STD_OUTPUT_HANDLE
mov hStdOut, eax
invoke SetConsoleTextAttribute,hStdOut,38 ; some random color code
invoke rand
invoke printf,offset format1,eax
invoke SetConsoleTextAttribute,hStdOut,7 ; restore default color
invoke system,offset cmd_pause
invoke system,offset cmd_cls
invoke printGrid,BOARD_SIZE
invoke moveCursor,0,0
mov curX,0
mov curY,0
GO:
push curX
pop oldX
push curY
pop oldY
invoke _getch
cmp al,-32
jne OTHER_KEYS
invoke getArrowKey
cmp ax,0
je GO
; al is deltaX, ah is deltaY
mov bx,curX
mov cx,curY
add bl,al
add cl,ah
mov curX,bx
mov curY,cx
cmp curX,BOARD_SIZE
jge OUT_OF_RANGE
cmp curY,BOARD_SIZE
jge OUT_OF_RANGE
invoke moveCursor,curX,curY
jmp GO
OTHER_KEYS:
cmp al,'q'
je EXIT
cmp al,' '
je PUT_BOMB
jmp GO
PUT_BOMB:
invoke printf,offset format2,'O'
invoke moveCursor,curX,curY
jmp GO
OUT_OF_RANGE:
push oldX
pop curX
push oldY
pop curY
jmp GO
EXIT:
invoke system,offset cmd_cls
invoke CloseHandle,hStdOut
invoke ExitProcess,0
main ENDP
printGrid PROC C n:DWORD
LOCAL i:DWORD,j:DWORD
mov i,1
LOOP1:
mov j,1
LOOP2:
invoke printf,offset format2,'X'
inc j
mov eax,j
cmp eax,n
jle LOOP2
invoke printf,offset newline
inc i
mov eax,i
cmp eax,n
jle LOOP1
ret
printGrid ENDP
getArrowKey PROC C
mov eax,0
invoke _getch
cmp al,75
je LEFT
cmp al,77
je RIGHT
cmp al,72
je UP
cmp al,80
je DOWN
jmp NOT_ARROW_KEY
UP:
mov al,0
mov ah,-1
ret
LEFT:
mov al,-1
mov ah,0
ret
RIGHT:
mov al,1
mov ah,0
ret
DOWN:
mov al,0
mov ah,1
ret
NOT_ARROW_KEY:
mov eax,0
ret
getArrowKey ENDP
moveCursor PROC C X:WORD,Y:WORD
LOCAL pos:COORD
mov ax,X
mov pos.X,ax
mov ax,Y
mov pos.Y,ax
invoke SetConsoleCurSorPosition,hStdOut,pos
ret
moveCursor ENDP
END main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment