Need help understanding this code (Z80 Assembler)

761 views Asked by At

I tried going through the documentation I have but it's really confusing, I need to understand this for an exam but I'm having lots of troubles.

  aseg
  org  100h
start:  ld    ix, vector 
        ld    B, amount
        ld    A, 0
cycle:  add   A, (IX)
        jp    PE, fail
        inc   IX
        djnz  cycle
        ld    (resp), A
        jp    fin
fail:   ld    A, 1
        ld    (error), A
fin:    rst   38h
vector: db    12,7,9,21
amount  equ   $ - vector
resp    ds    1
error:  db    0
        end   start

I understand what most of the 'functions' (ld, add, jp, inc) do separately, what I don't understand is:

1) What value is loaded into IX in the first line? (the variable?) vector has 4 values on it, I tried this in a z80 simulator and it says that IX gets the value 0019, but I don't see where this is coming from...

2) Am I understanding correctly that "vector: db 12,7,9,21" creates an array with the values 12,7,9,21?

3)What does the line "end start" do?

4)What value is "amount" holding?

1

There are 1 answers

0
David Hoelzer On BEST ANSWER

Let's take these one at a time:

1) What value is loaded into IX in the first line? (the variable?) vector has 4 values on it, I tried this in a z80 simulator and it says that IX gets the value 0019, but I don't see where this is coming from...

The line ld ix, vector loads the memory address for vector into IX. When you see 0019 show up here in your simulator you are looking at the byte offset from the start of the program. This is essentially being used as a pointer to the first element in that "array."

2) Am I understanding correctly that "vector: db 12,7,9,21" creates an array with the values 12,7,9,21?

Well, you could view it that way. All that it's really doing is defining four arbitrary bytes in RAM and providing a convenient label to figure out where they are. How the data is interpreted is what determines if it's an array, four characters, a two byte integer or a four byte integer, etc.

3)What does the line "end start" do?

This is just a directive to the assembler. It actually does nothing with regard to the assembled code. It lets the assembler know that there should not be any more code coming.

4)What value is "amount" holding?

Amount is a defined value (rather than allocated memory) that is calculated at compile time. The $ in an assembler typically refers to the current address. Therefore, Amount is defined as the difference between the current address and the address where vector starts. In this case, since there are four bytes defined, this will work out to a value of 4.