assembly 如何在Z80汇编语言中顺序地从DEFB中读取元素,然后在每次迭代中使用检索到的值

unftdfkk  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(85)

我一直在学习Z80汇编语言,对此我有点困惑。DEFB中的值表示音高。程序使用A寄存器创建一个顺序循环,从0开始,它应读取DEFB的第 n 个元素,将值传输到HL寄存器,然后发出蜂鸣音。我不知道如何删除我的硬编码并让它工作:

ORG 30000

_MAIN
    LD A, 0         ;Set A to zero

LOOP_POINT:

    LD B, A         ;Load the current value of A into B to loop on
    CP 31           ;Check if the B value is 31
    JP Z, FINISHED      ;If yes, 32 notes have been played, so jump to FINISHED

    PUSH AF         ;Put current A value on the stack, because the "CALL 949" is going to overwrite it

    ;LD HL, (SOUND_BUFFER + A) - THIS DOES NOT WORK, I AM FORCED TO HARD CODE TO 855 FOR THIS EXAMPLE (BELOW)
    LD HL, 855      ;Load the pitch into HL
    LD DE, 24       ;Load the duration  into DE

    CALL 949        ;Will play the pitch in HL for the duration in DE

    POP AF          ;Restore the A value back from the stack

    INC A           ;Add 1

    CALL LOOP_POINT     

FINISHED:
    RET

SOUND_BUFFER                ;32 elements (each is a 1/4 note, so, 4*8 bars = 32 in total)

    DEFB "855,0,0,0,855,855,855,759,759,673,673,673,673,673,673,673,0,0,0,0,855,855,855,759,759,673,673,759,759,855,0,855"

字符串
有谁能帮我一下语法或者给我指出正确的方向吗?

mlnl4t2r

mlnl4t2r1#

你要么必须从A中的index计算指针,要么只保留指针并在遍历数组时更新它。下面是我的变体(基于您的原始代码)-未检查,没有保证。还请注意,我已经将您的DEFB更改为DEFW -我不熟悉您的汇编程序版本,因此这可能是错误的,但由于音高是由16位值表示的,因此我希望使用DEFW(或DW)。
编辑:而且,按照编码的方式,你需要将A与32进行比较,而不是31。否则,最后一个音符将不被演奏。
希望这对你有帮助。

ORG 30000

_MAIN
  LD A, 0         ;Set A to zero
  LD HL,SOUND_BUFFER  ;Store pointer in HL

LOOP_POINT:

  ;;;LD B, A         ;Load the current value of A into B to loop on
  CP 32           ;Check if the A value is 32
  JP Z, FINISHED      ;If yes, 32 notes have been played, so jump to FINISHED

  PUSH AF         ;Put current A value on the stack, because the "CALL 949" is going to overwrite it

  ;;;LD HL, 855      ;Load the pitch into HL
  LD E, (HL)      ;Load least significant byte into E
  INC HL          ;Bump pointer
  LD D,(HL)       ;Load most significant byte into D, DE contains the pitch 
  INC HL          ;Bump pointer
  PUSH HL         ;Save HL (pointing to next pitch)
  EX DE,HL        ;Move pitch from DE to HL
  LD DE, 24       ;Load the duration into DE

  CALL 949        ;Will play the pitch in HL for the duration in DE

  POP HL          ;Restore HL (pointing to next pitch)
  POP AF          ;Restore the A value back from the stack

  INC A           ;Add 1

  ;;;CALL LOOP_POINT     ; why CALL?
  JR LOOP_POINT   ; just jump

 FINISHED:
  RET

SOUND_BUFFER                ;32 elements (each is a 1/4 note, so, 4*8 bars = 32 in total)
  DEFW 855,0,0,0,855,855,855,759,759,673,673,673,673,673,673,673,0,0,0,0,855,855,855,759,759,673,673,759,759,855,0,855

字符串

相关问题