Re: A86: String input again


[Prev][Next][Index][Thread]

Re: A86: String input again




...
>Can someone tell me what is wrong with this, or comment it a little more thoroughly?  >It crashes the calc pretty good.

Yeah, I posted that as an idea...  It wasn't a very bright one,
obviously, as it crashes my emulator too.  It sure makes some neat
little moving dots on the screen though. :)
Anyway, there were several things wrong, mainly to do with push/pops not
turning up equal for each branch of the program.  After several keys had
been pressed the stack was totally trashed.  Sorry about that...

This variation seems to work: 
(stores input to a zero-terminated string beginning at 'string'.  Should
accept numbers, spaces, capital and lowercase letters.  Exit quits;
Enter tells it that the user is done inputting keypresses.)

KeyString:
	ld hl, string			;load address of string to store to
keyloop:
	call _getkey			;get keypress
	cp kenter			;enter key?
	jp z, key_done			;then we're done
	cp kexit			;exit key?
	ret z				;if so, then quit			
	cp kSpace			;is it space?
	jp z, key_space			;if it is a space
	
	push af				;preserve the keycode
	sub $1C				;k0
	jp m, keyloop			;it's not a known key if negative
	sub $0A				;difference between (k9+1) and k0
	jp m, key_number		;if neg, assumed to be a number key
	sub $02				;difference between kCapA and (k9+1)
	jp m, keyloop			;it's not a known key if negative
	sub $1A				;difference between (kCapZ+1) and kCapA
	jp m, key_capletter		;if negative, then assume capital letter
	sub $1B				;difference between (kz+1) and (kCapZ+1)
	jp m, key_lowletter		;if neg, assume lower case letter
	jp keyloop			;must be an unknown key at this point

key_space:				
	ld a, Lspace			;char code for space
	ld (hl), a			;put into string
	inc hl				;forward pointer
	call _vputmap			;display a space
	jp keyloop			;re-loop
key_number:
	pop af				;recall keypress
	add a, 20			;keycode-->char code
	ld (hl), a			;store
	inc hl				;inc string pointer
	call _vputmap			;display the number
	jp keyloop
key_capletter:
	pop af				;recall keycode
	add a, 25			;keycode-->char code
	ld (hl), a			;store
	inc hl				;increment string pointer
	call _vputmap			;display the letter
	jp keyloop
key_lowletter:
	pop af
	add a, 31			;keycode-->char code
	ld (hl), a
	inc hl
	call _vputmap
	jp keyloop
key_done:
	ld (hl), 0			;zero-terminate string
	ret				;quit

string:
	.db 0,........


References: