Sign in to follow this  
Shenjoku

Getting chars in assembly language

Recommended Posts

I've recently started learning assembly language in school and I am stuck on this project I am doing. I am writing a game where you move around the screen and leave an x(wall) behind you from every movement. What I need it to do is check with every move if the current position is where a wall character is by reading it directly from the cursor location. I found a function with interrupt 10 that does it (function 8) but I'm not sure how to write the code I have in 16 bit rather than 32 bit so i don't know if that is possible for use. I would like to know if there is any function for 32 bit that can be used for this. or if not then maybe I could post my code I have and you all could give me tips on how to rewrite in 16 bit cus I have no idea what I would have to change. Thanks in advance :)
TITLE Game		(game.asm)

; Program Description:
; Author:		Justin Grant
; Creation Date: 2/23/07

INCLUDE Irvine32.inc

CHAR = 1
ERASE = 88


.data
	;data for character position
	walkDir BYTE ?
	wall BYTE ?
	score=0
	;data for reading map files
	buffer BYTE 5000 DUP(?)
	bufSize = ($-buffer)
	errMsg BYTE "Cannot open file",0dh,0ah,0
	filename     BYTE "map1.txt",0
	fileHandle   DWORD ?	; handle to output file
	byteCount    DWORD ?    	; number of bytes written

.code
main PROC
	call clrscr
	mov dh, 13  ;set starting position
	mov dl, 40  ;for player.
	mov ecx, 10
	mov al, CHAR
	call GotoXY
	;open map file for reading and drawing
	call ReadMapFile

	LoopMe:
		;draw the character
		call Drawchar
		;read keyboard input for what key pressed
		call readchar							
		mov walkDir,al
		;check if "up" key is pressed
		.IF (walkDir=='w') || (walkDir=='W')
			.IF (dh>1)
			call CheckWall
			call erasechar
			dec dh
			call Gotoxy
			jmp LoopMe
			.ENDIF
		;check if "left" key is pressed
		.ELSEIF (walkDir=='a') || (walkDir=='A')
			.IF (dl>1)
			call CheckWall
			call erasechar
			dec dl
			call Gotoxy
			jmp LoopMe
			.ENDIF
		;check if "down" key is pressed
		.ELSEIF (walkDir=='s') || (walkDir=='S')
			.IF (dh<21)
			call CheckWall
			call erasechar
			inc dh
			call Gotoxy
			jmp LoopMe
			.ENDIF
		;check if "right" key is pressed
		.ELSEIF (walkDir=='d') || (walkDir=='D')
			.IF (dl<78)
			call CheckWall
			call erasechar
			inc dl
			call Gotoxy
			jmp LoopMe
			.ENDIF
		.ENDIF
		jmp LoopMe
		;call MoveChar
	
	call crlf
	call waitmsg
	exit
main ENDP
;*****************************************************
DrawChar PROC
;
; This procedure will draw the character after each move
;
;*****************************************************
	pushad
	mov al, CHAR
	call GotoXY
	call writeChar
	popad
	

	ret
DrawChar endp

;*****************************************************
EraseChar PROC
;
; This procedure will erase previously drawn character
; and replace it with a wall after evey move
;*****************************************************
	pushad
	mov al, ERASE
	call GotoXY
	call writechar
	popad
	

	ret
	EraseChar endp
	
;*********************************************************
ReadMapFile PROC
;
; this procedures reads a txt file and translates the 1,0
; pattern into a playable map
;*********************************************************
	pushad
	INVOKE CreateFile,
	  ADDR filename, GENERIC_READ, DO_NOT_SHARE, NULL,
	  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0

	mov fileHandle,eax		; save file handle
	.IF eax == INVALID_HANDLE_VALUE
	  mov  edx,OFFSET errMsg		; Display error message
	  call WriteString
	  jmp  QuitNow
	.ENDIF

	INVOKE ReadFile,		; write text to file
	    fileHandle,		; file handle
	    ADDR buffer,		; buffer pointer
	    bufSize,		; number of bytes to write
	    ADDR byteCount,		; number of bytes written
	    0		; overlapped execution flag

	INVOKE CloseHandle, fileHandle

	mov esi,byteCount		; insert null terminator
	mov buffer[esi],0		; into buffer
	mov edx,OFFSET buffer		; display the buffer
	call clrscr
	call WriteString

	QuitNow:
		popad
		ret
	ReadMapFile endp
;*********************************************************
CheckWall PROC
; checks whether there is a wall in the players way or not
; and moves them back to previous spot depending on what
; button movement was pressed
;*********************************************************
	pushad
	mov ah,8
	mov bh,0
        ;note:this is the 16 bit function I was trying to use
	int 10h
	mov wall, al
	;check if the player hits a wall
	.IF (wall=='x') || (wall=='X') && (walkDir=='w') || (walkDir=='W')
		inc dh
	.ELSEIF (wall=='x') || (wall=='X') && (walkDir=='a') || (walkDir=='A')
		inc dl
	.ELSEIF (wall=='x') || (wall=='X') && (walkDir=='s') || (walkDir=='S')
		dec dh
	.ELSEIF (wall=='x') || (wall=='X') && (walkDir=='d') || (walkDir=='D')
		dec dl
	.ENDIF
	;check if the player can eat a dot
	.IF (wall=='.')
		score=score+1
	.ENDIF
	popad
	ret
	CheckWall endp
END main

Share this post


Link to post
Share on other sites
The code looks correct to me. That interrupt should work just fine in 32bit code. (It would just use the 16bit low order word of eax)

What assembler are you using? (I never seen this synthax before--Im into NASM)

Share this post


Link to post
Share on other sites
I'm not really sure to be honest. It was installed on the computers before we started this class and they didn't tell us what it was even -_- all I know is that I gotta open dos and then do "make32 game" to compile it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this