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

时间:2016-05-13 23:16:21

标签: easy68k

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

*Equates section
program_start equ $1000   *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  #$00000000,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

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

你的代码可怕地误用陷阱。 trap#15调用d0指示的操作,请参阅帮助手册。你正在寻找陷阱15操作3,

  

"在最小字段中以十进制显示D1.L中的带符号数字。 (见任务15和20)"

在调用陷阱#15时,你的D0是1400美元,其中低位字节是0x00,被解释为

  

"在(A1)显示字符串的n个字符,n为D1.W(在NULL或最大255处停止),CR,LF。 (见任务13)"

当时的A1等于字节的地址"产品"。

它试图将数字解释为c样式字符串,并因此给你带来垃圾。

另外,请记住,当您调用陷阱时,您的d0或d1 /等可能已更改。总是尽量保持d0赋值接近陷阱调用,以避免发生奇怪的事情。首先准备你的信息,然后设置d0,然后调用陷阱。

这是阻止它工作的唯一因素,但无论如何我以一种我更舒服的方式重新格式化它。

;Equates section
program_start equ $1000       ; 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  #$00000000,  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