[A86] Re: Hex input routine


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

[A86] Re: Hex input routine




input_hex_word:
  call get_byte
  ld h,a
  call get_byte
  ld l,a
  or a
  ret

get_byte:
  push hl
  call _getcsc
  ld hl,keytable
  ld bc,16
  cpir
  pop hl
  jr nz,canceled
  ld a,15
  sub c
;maybe do some output here?
  ret
canceled:
  pop hl   ;clear calling address
  scf
  ret

keytable:
 .db K_0,K_1,K_2,K_3,K_4,K_5,K_6,K_7
 .db K_8,K_9,K_LOG,K_SIN,K_COS,K_TAN,K_RAISE,K_LN


In a message dated Fri, 4 May 2001 12:55:01 AM Eastern Daylight Time, Appelkore@aol.com writes:

<< 
Can someone tell me what's wrong with this routine?

<tt>

;input_hex_word
; input: user enters two hex bytes using 0-9 and A-F
; output: carry flag set => user aborted
;         no carry => result in hl
; destroyed: a

input_hex_word:
 push bc
 push de
 ld b,4                 ;4 characters to be entered

inhex_key:
 call _getcsc
 push hl                ;because it contains the input
 ld hl,keytable
 ld d,b                 ;save char #
 ld bc,$1000            ;b=counter=16, c=value=0
inhex_key_loop:
 cp (hl)                ;was this the key pressed?
 jr z,inhex_gotdigit    ;if so, we have the value
 inc c                  ;otherwise, next value
 inc hl                 ; and next key
 djnz inhex_key_loop    ;do this for all 16 value keys
 ld b,d                 ;get back char #
 pop hl                 ;get the input back into hl
 cp K_EXIT              ;no DEL handling yet
 jr nz,inhex_key
 pop de
 pop bc
 scf                    ;user aborted
 ret                    ;exit

keytable:
 .db K_0,K_1,K_2,K_3,K_4,K_5,K_6,K_7
 .db K_8,K_9,K_LOG,K_SIN,K_COS,K_TAN,K_RAISE,K_LN

inhex_gotdigit:
 push bc                ;save char # and value
 push hl                ;save input
 ld l,c                 ;
 ld h,0                 ;hl = value
 ld a,b                 ;a = char #
 dec a                  ;0-3, not 1-4
 jr z,after_loop        ;if last char, no shift necessary
 add a,a
 add a,a
 ld b,a                 ;b = # of bit positions to shift
gotdigit_loop:
 add hl,hl              ;shift hl one bit left
 djnz gotdigit_loop     ;the goal here is to move the 4-bit value
after_loop:             ; into one of the 4 nibbles in hl
 ex de,hl               ;save new value
 pop hl
 add hl,de              ;add to value from previously entered chars
 pop bc                 ;get back char # and value
 ld a,'0'
 add a,c
 cp '9'+1
 jr nc,got_char
 add a,'A'-'9'
got_char:
 call _putc
 dec b                  ;next char
 jp nz,inhex_key        ;too far away for djnz
 pop de
 pop bc
 rcf                    ;successful (user entered all 4 chars w/o aborting)
 ret

</tt>



 >>