0

当我尝试使用陷阱任务 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

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

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

任何帮助,将不胜感激。

4

1 回答 1

1

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

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

在调用陷阱#15 时,您的 D0 为 $1400,其中低字节为 0x00,这被解释为

“在 (A1) 处显示 n 个字符串字符,n 是 D1.W(在 NULL 或最大 255 处停止),带有 CR、LF。(参见任务 13)”

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

它试图将数字解释为 ac 样式字符串,结果给你垃圾。

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

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

;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
于 2016-05-13T23:27:38.580 回答