[A86] Re: Hex input routine


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

[A86] Re: Hex input routine



Sorry that was written in somewhat of a rush.  Here's a more complete version.

input_hex_word:
  ld d,4
  ld hl,0
input_hex_byte:
  push de
  push hl
input_hex_key:
  halt
  call _getcsc
  or a
  jr z,input_hex_key
  pop hl
  pop de
  cp K_EXIT
  scf
  ret z     ;canceled
  push hl
  ld hl,keytable
  ld bc,16
  cpir
  pop hl
  jr nz,input_hex_key
  add hl,hl
  add hl,hl
  add hl,hl
  add hl,hl    ;x 16
  ld a,c
  or l           ;add new value
  ld l,a
  ld a,c
  cp 10
  jr nc,disp_char
  add a,'0'
disp_char:
  push de
  call _putc
  pop de
  dec d
  jr nz,input_hex_byte
  ret

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


In a message dated 5/4/2001 10:49:17 AM Eastern Daylight Time, 
ComAsYuAre@aol.com writes:


> 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>
> 
> 
> 
> >>
> 
> 




----
Jonah Cohen
<ComAsYuAre@aol.com>
http://jonah.ticalc.org