A86: Re: Need help w/ inputs & arrays


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

A86: Re: Need help w/ inputs & arrays



> Firstly, i need a routine where the user can type in a number between 1
and 99, and it
> will be saved to the 'a' register.  Preferably in the small '_vputs' text,
put regular
> text is fine.

See the attached file.  It includes the routine and an example.

> Secondly, is there any way to make an array in an asm program, or maybe
manipulate a
> list like i can in a basic program?

You make arrays exactly the same way as in C (it's actually the opposite,
but anyway).  Just set aside a block of memory, and that's your array.  Hmm,
a tutorial on basic arrays and stuff in asm would be good.

Ok, for starters, let's say you need a 100 byte array of characters, or
bytes.  Try this:

Data = $8000     ; [100]

The comment isn't necessary, but it helps push the point, and it makes your
code much clearer to you and anyone reading it.  To access individual
elements at absolute or immediate addresses, you can do it directly using
the assembler's preprocessor:

 ld a,(Data+53)      ; load Data[53], or the 54th byte
 ld hl,Data+9        ; point to Data[9], the 10th byte
 ld a,(hl)           ; get the 10th byte

You can also use the index registers, but it's best not to use them unless
there's a good reason for it.  Your code will almost always be smaller if
you arrange your data so you can go through it either forwards or backwards
using HL as a pointer.  But an example:

 ld ix,Data          ; point to the array
 ld a,(ix+54)        ; get Data[54], 55th byte

Now, say it isn't a fixed address.  You need pointer arithmetic:

; get byte at position A -- Data[A]

 ld hl,Data          ; point to array
 ld d,0              ; clear high byte
 ld e,a              ; load A into lower byte for 16 bit add
 add hl,de           ; calculate correct pointer
 ld a,(hl)           ; load Data[A]

Now, if you are doing a lot of this, it might be wise to use HL for the
addition and DE to store Data, that way you don't have to reload the value
of Data everytime.  That's about all there is to arrays!

promptnum.asm


References: