调用 trap 命令时容易出现 68k 错误

Easy 68k error when trap command is called

当我尝试使用陷阱任务 17 显示寄存器的内容时​​,出现了一些奇怪的错误。 这是我的代码:

*Equates section
program_start equ 00   *Start Location of program
timesToAdd    equ 10     *Number to multiply numToMultiply by
numToMultiply equ 512    *Number to multiply through cumulative sum

ORG    program_start
START:                  ; first instruction of program

* Put program code here
    MOVE.L  #[=10=]000000,D0  *Initially set value in D0 to 0
    MOVE.B  #timesToAdd,D2  *Store times left to add in D2

loop    CMP.B    #0,D2         *Check if we are finished adding
        BEQ  loop_end          *exit loop if we are done
        SUB.B #1,D2            *decrement timesToAdd by 1
        ADDI.L #numToMultiply,D0 *Add numToMultiply to value in D0
        BCC skipSet
        MOVE.B #1,D1           *Set D1 to 1 if carry bit is set 
skipSet BRA loop
loop_end     
     MOVE.L D0,D2
     MOVE.L #17,D0

     CMP.B #0,D1    *Check if there was a carry
     BEQ skipCarry
     LEA Carry,A1
     Trap #15       *Print Carry: with carry bit
skipCarry
     MOVE.L D2,D1
     LEA Product,A1
     Trap #15

     SIMHALT             ; halt simulator

Carry DC.B 'Carry: '
Product DC.B 'Product= '
    END    START        ; last line of source

当我运行这个时,我得到这个输出: Output

陷阱调用前的寄存器状态: Before Trap

如有任何帮助,我们将不胜感激。

您的代码严重误用了 trap。陷阱 #15 调用 d0 指示的操作,请参阅帮助手册。您正在寻找陷阱 15 操作 3,

"Display signed number in D1.L in decimal in smallest field. (see task 15 & 20)"

在trap #15被调用时,你的D0是$1400,其中低字节是0x00,解释为

"Display n characters of string at (A1), n is D1.W (stops on NULL or max 255) with CR, LF. (see task 13)"

此时的A1等于"Product"字节的地址。

它试图将数字解释为 c 风格的字符串,结果给你垃圾。

此外,请记住,在您调用 trap 时,您的 d0 或 d1/etc 可能已经更改。始终尝试使 d0 分配尽可能接近 trap 调用,以避免发生奇怪的事情。先准备好你的信息,然后设置d0,再调用trap。

这主要是阻止它工作的唯一原因,但我还是以一种我更喜欢的方式重新格式化了它。

;Equates section
program_start equ 00       ; Start Location of program
timesToAdd    equ 10          ; Number to multiply numToMultiply by
numToMultiply equ 512         ; Number to multiply through cumulative sum

    org    program_start
start:                        ; first instruction of program
    move.l  #[=10=]000000,  D0   ; Initially set value in D0 to 0
    move.b  #timesToAdd, D2   ; Store times left to add in D2

loop:   
    cmp.b  #0, d2             ; Check if we are finished adding
    beq    loop_end           ; exit loop if we are done
    sub.b  #1, d2             ; decrement timesToAdd by 1
    addi.l #numToMultiply, d0 ; Add numToMultiply to value in D0
    bcc    skipSet
    move.b #1, d1             ; Set D1 to 1 if carry bit is set 

skipSet:
    bra loop

loop_end:
    ; Check if there was a carry
    cmp.b #0, d1    
    beq   skipCarry
    lea   Carry, a1

    ; Print Carry: with carry bit
    move.l #17, d0
    move.l d0,  d2
    trap   #15       

skipCarry:
    move.l d2, d1
    lea Product, a1

    move.l d0, d1
    move.l #3, d0
    trap #15

    simhalt

Carry   dc.b 'Carry: '
Product dc.b 'Product= '
    end start