0

Trying to get some better understanding of working with integer arrays and came across this issue.

Q: When simply printing elements of my array and I don't use ASLX I get unexpected results, but when I use ASLX I get expected results. How does ASLX effect my result?

FOOD:.word   0           
     .word   1           
     .word   1       
     .word   0

main:LDX     3,i         ; i = 3
     STX     i,d

for: CPX     0,i         ; i >= 0
     BRLT    endFor
     DECO    i,d
     CHARO   ' ',i
     ASLX                ; If I remove this I get 256 instead of expected value
     DECO    FOOD,x   ; FOOD[i]
     CHARO   '\n',i
     LDX     i,d
     SUBX    1,i
     STX     i,d
     BR      for

endFor:  stop 
     .end

Thanks

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
J Man
  • 121
  • 1
  • 12

1 Answers1

0

ASL, or arithmetic shift left, shifts all of the bits of a number to the left. Thus 0001 becomes 0010, then 0100, and so on. This is equivalent to multiplying the number by two.

When you use the ASLX command, you double the index register. You often do this when working with an integer array because integers take up two bytes, and the index register is measured in single bytes.

In other words, if you were to simply increment/decrement the index register by 1, you would end up "between" integers, such that you were reading the last byte of one and the first byte of another. That's why you get strange results if you don't ASLX.

Another option that might make it more clear is that you can increment/decrement the index register by two. You won't have to do any shifting, since you will stay aligned with the array of integers.

CrazyMLC
  • 13
  • 4